• Post Reply Bookmark Topic Watch Topic
  • New Topic

Basic Inheritance and static method calls  RSS feed

 
Ciaran Reid
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, this is something i cant get my head around, if someone can help me, I think its a standard concept but just not getting it. (plus i cant figure out how to put my code into a code box, sorry!)
i have 3 classes: 1) Base class 2) Derived class, which extends base 3) TestClass


So if i run this, my results are:

Display method in Base
display method in Derived
display method in Derived

Thats cool, no problems there, but if i change my Display method in Base and Derived to static methods then the results become:

Display method in Base
display method in Derived
Display method in Base


So why oh why does obj3.display() now print "Display method in Base"?

(and yes I know the calls should be static calls, but for sake of arguments -- or is that the whole problem, that technically obj3.display() is an incorrect call as it can only be Base.display() or Derived.display() when display() is a static method ).
 
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 are not polymorphic, like instance methods are. That means that for static methods, Java looks at the type of the variable, not at the type of the actual object that the variable refers to, to determine which method to call.

Note that static methods are class-level methods. It's unfortunate that Java allows you to call static methods on variables that refer to objects - it looks as if you're calling the method on an object, while in reality that is not what's happening. Because it's confusing, you should avoid calling static methods on objects. Call them directly on the class name instead. For example:

In my opinion the Java language specification and the compiler should have disallowed calling static methods on instances, but unfortunately this feature has been in Java since the beginning and it can't be changed without breaking backward compatibility.

For a more detailed explanation of what 'static' means, see this page in Oracle's Java tutorials: Understanding Class Members
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To prove you cannot call static methods on an object, start by trying to create an object of the Math class. You should find it impossible. So, you can't have Math objects. But you can get a sensible output from this sort of crappy thingAnd remember null isn't an object. You are calling a static method on not‑an‑object.
Another thing worth reading is No 15-16 in our basic FAQ.
 
Ciaran Reid
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Thank you indeed
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!