• Post Reply Bookmark Topic Watch Topic
  • New Topic

Dynamic Method Look up  RSS feed

 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to run Person class and got "I'm saying : Hello". Instead I expected that "I am the director" would be the output.
Can you please let me know why is DML not taking place here.

//Person.java
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");
}
}

//Director.java
public class Director extends Person {
public void say(String s){
System.out.println("I'm the director");
}
}
 
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
8.4.8 Inheritance, Overriding, and Hiding:
A class C inherits from its direct superclass and direct superinterfaces all non-private methods (whether abstract or not) of the superclass and superinterfaces that are public, protected or declared with default access in the same package as C and are neither overridden (�8.4.8.1) nor hidden (�8.4.8.2) by a declaration in the class.
[ May 10, 2006: Message edited by: wise owen ]
 
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The method say(String s) is not overridden here. Do you see why?
 
Pramila Chinguru
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, got it. private method, hence is not overriden and results in calling the says method of Person class.
Thanks wise owen and marc
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
final, static and private methods are not overridden by a subclass. However, a static method can be hidden by another static method in a subclass with the same signature. Also, a private method can be hidden by another method in the subclass with the same signature. However, DML cannot be used in these cases.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!