• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

overridding doubt

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic