• Post Reply Bookmark Topic Watch Topic
  • New Topic

Question related to overriding  RSS feed

 
Vibhor Chander
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thats not overriding. Its called Hiding
Read more about it here: https://docs.oracle.com/javase/tutorial/java/IandI/override.html
Yes. Thats because method is declared static
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Static methods cannot be overridden.

When you call a static method on an instance, as you are doing here, then the compiler decides which method to call at compile time, by looking at the type of the variable.

Since t is a Tenor, t.sing() means that the sing() method in class Tenor is called.
Since s is a Singer, s.sing() means that the sing() method in class Singer is called.

Note that this is different from non-static methods, where the method to call is decided at runtime, by looking at the type of the actual object that the variable refers to.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!