class a{ private void show(){ System.out.println("parent"); } }
public class override extends a{ private void show(){
}
public static void main(String[] args) { a a = new override(); a.show(); } } i have just over ridden the parent class method in sub class. but program is not compiled. please explain me reason for this.
Actually I think this is a case of hiding rather than overriding since private methods aren't inherited. [ May 01, 2006: Message edited by: Keith Lynn ]
class a{ private void show(){ System.out.println("parent"); } }
public class override extends a{ private void show(){
}
public static void main(String[] args) { a a = new override(); a.show(); } }
1)show() method in class a is private to class a cannot be accessed outside class a.
2)show() method(is encapsulated in class a)is only visible in the class in which it is defined.
3)private methods(show() in this case) are not inherited by the subclass.However a method with same prototype in the subclass is an all together different method not overriden method.
4)You are getting compile time error because you are trying to access private method of class a in subclass.
Let me know if this helps...
What are you doing? You are supposed to be reading this tiny ad!
Gift giving made easy with the permaculture playing cards