This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes I am really confused regarding overriding Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "I am really confused regarding overriding" Watch "I am really confused regarding overriding" New topic
Author

I am really confused regarding overriding

prarthana reddy
Ranch Hand

Joined: Jul 24, 2006
Posts: 48
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");
}
}
Radoslaw Sztando
Ranch Hand

Joined: Mar 11, 2004
Posts: 40
you're running private say() method from class where it is defined (class Person) - it's perfectly correct.


Regards,
Radek Sztando
prarthana reddy
Ranch Hand

Joined: Jul 24, 2006
Posts: 48
Thankyou for your reply ..i dint notice that
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: I am really confused regarding overriding
 
Similar Threads
Dynamic Method Look up
Method Overriding
jtips 1, q26
Doubt in Mocktest
which method to call?