Quantcast

A community for students. Sign up today!

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

bintang

  • one year ago

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(); } }

  • This Question is Closed
  1. KonradZuse
    • one year ago
    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.

  2. hedjm
    • one year ago
    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.

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

    thanks ... that's really helpful :)

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

    Hmmm never seen it that way before.

  5. KonradZuse
    • one year ago
    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.

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

    i see

  7. KonradZuse
    • one year ago
    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.

  8. KonradZuse
    • one year ago
    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(); } }

  9. KonradZuse
    • one year ago
    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.

  10. Not the answer you are looking for?
    Search for more explanations.

    • Attachments:

Ask your own question

Ask a Question
Find more explanations on OpenStudy

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.