• 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
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

I am really confused regarding overriding

 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI,
We know that private methods are not overriden . If we have a method with same name in super class and sub class but declared as private in superclass and public in subclass and if we try to invoke subclass method with a superclass reference, it should give compile error . But the following code is giving me the output:I am saying hello. How is this possible ?
public class Person {
private void say(String s){
System.out.println("I'm saying: " + s);
}
public static void main(String[] args) {
Person p = new Director();
p.say("Hello");
}
}
class Director extends Person {
public void say(String s){
System.out.println("I'm the director");
}
}
 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you're running private say() method from class where it is defined (class Person) - it's perfectly correct.
 
prarthana reddy
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thankyou for your reply ..i dint notice that
 
reply
    Bookmark Topic Watch Topic
  • New Topic