Quantcast

Got Homework?

Connect with other students for help. It's a free community.

  • across
    MIT Grad Student
    Online now
  • laura*
    Helped 1,000 students
    Online now
  • Hero
    College Math Guru
    Online now

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

bintang

code in java: why this is error? public class Parent { protected void sayHi() { System.out.print("Hi"); } class Child extends Parent { public void sayHello() { System.out.print("Hello"); } } public static void main(String[]args){ new Child().sayHi(); } }

  • one year ago
  • one year ago

  • This Question is Closed
  1. KonradZuse
    Best Response
    You've already chosen the best response.
    Medals 0

    what is your error? I'm assuming because you did protected void sayHI() the protected rights might not be significant for a child.

    • one year ago
  2. hedjm
    Best Response
    You've already chosen the best response.
    Medals 0

    See the code below: // this is the Parent class public class Parent { protected void sayHi() { System.out.print("Hi"); } // this is an innerclass ok and it inherits from the outerclass Parent class Child extends Parent { public void sayHello() { System.out.print("Hello"); } } } the main method will be like this: public static void main(String[] args) { Parent x = new Parent(); // instantiating a Parent object x Parent.Child y = x.new Child(); // then we instantiating a Child object y using x y.sayHi(); // calling the sayHi() method } I hope this was useful if I'm wrong correct me. thanks.

    • one year ago
  3. bintang
    Best Response
    You've already chosen the best response.
    Medals 0

    thanks ... that's really helpful :)

    • one year ago
  4. KonradZuse
    Best Response
    You've already chosen the best response.
    Medals 0

    Hmmm never seen it that way before.

    • one year ago
  5. KonradZuse
    Best Response
    You've already chosen the best response.
    Medals 0

    I know extending the Rectangle class you will get it's methods; however I also know that you cannot access protected methods from other classes. Change protected. I have done Rectangle2D.Float = new Rectangle2D.Float before.

    • one year ago
  6. bintang
    Best Response
    You've already chosen the best response.
    Medals 0

    i see

    • one year ago
  7. KonradZuse
    Best Response
    You've already chosen the best response.
    Medals 0

    http://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html hmm apparently it should work though... Maybe it was because mine wasn't a subclass.... HMm not too sure then.

    • one year ago
  8. KonradZuse
    Best Response
    You've already chosen the best response.
    Medals 0

    Okay so I plugged in the code into my IDE and I got a static reference issue. This code works: import java.util.Scanner; /** * * @author Konrad */ public class JavaApplication12 { protected void sayHi() { System.out.print("Hi"); } static class Child extends JavaApplication12 { public void sayHello() { System.out.print("Hello"); } } public static void main(String[]args) { Child c = new Child(); c.sayHi(); } }

    • one year ago
  9. KonradZuse
    Best Response
    You've already chosen the best response.
    Medals 0

    The issue was that the child class wasn't static... Not 100% sure on how and where statics are used, but I've never seen this issue... never really did nested classes much either though.

    • one year ago
    • Attachments:

See more questions >>>

Your question is ready. Sign up for free to start getting answers.

spraguer (Moderator)
5 → View Detailed Profile

is replying to Can someone tell me what button the professor is hitting...

23

  • Teamwork 19 Teammate
  • Problem Solving 19 Hero
  • You have blocked this person.
  • ✔ You're a fan Checking fan status...

Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.

This is the testimonial you wrote.
You haven't written a testimonial for Owlfred.