anonymous
  • anonymous
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(); } }
Computer Science
schrodinger
  • schrodinger
See more answers at brainly.com
At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.

Get this expert

answer on brainly

SEE EXPERT ANSWER

Get your free account and access expert answers to this
and thousands of other questions

KonradZuse
  • KonradZuse
what is your error? I'm assuming because you did protected void sayHI() the protected rights might not be significant for a child.
anonymous
  • anonymous
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.
anonymous
  • anonymous
thanks ... that's really helpful :)

Looking for something else?

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

More answers

KonradZuse
  • KonradZuse
Hmmm never seen it that way before.
KonradZuse
  • KonradZuse
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.
anonymous
  • anonymous
i see
KonradZuse
  • KonradZuse
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.
KonradZuse
  • KonradZuse
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(); } }
KonradZuse
  • KonradZuse
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.

Looking for something else?

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