• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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...
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic