This week's book giveaways are in the Scala and Android forums.
We're giving away four copies each of Machine Learning Systems: Designs that scale and Xamarin in Action: Creating native cross-platform mobile apps and have the authors on-line!
See this thread and this one for details.
Win a copy of Machine Learning Systems: Designs that scale this week in the Scala forum
or Xamarin in Action: Creating native cross-platform mobile apps in the Android forum!
  • 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.
 
Won't you please? Please won't you be my neighbor? - Fred Rogers. Tiny ad:
Rocket Oven Kickstarter - from the trailboss
https://coderanch.com/t/695773/Rocket-Oven-Kickstarter-trailboss
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!