Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

overriding private methods

807597Nov 1 2005 — edited Nov 1 2005
Is it possible to override private methods??

class A {
private void print() {
System.out.println("Hello Super");
}
}

class B extends A{
public void print() {
System.out.println("Hello Sub");
}
}

class C {
public static void main(String args[]) {
A a=new B();
a.print();
}
}

According to overriding rules the sub class method should not be more restrictive than the super class method. SO we are making it public (From private).
So the above code is an example of overriding but when compiled it gives error.
Please explain why??
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Nov 29 2005
Added on Nov 1 2005
4 comments
272 views