JATMON
JATMON

Reputation: 1040

Concepts in Inheriting a Static method

I have 2 classes as below

public class statictest {

public void print()
{
    System.out.println("first one");    
}
  }

public class newer extends statictest
 {

public void print()
{

    System.out.println("second one");

}   
  }

and in the main function I do

statictest temp = new newer();
newer temp2 = new newer();

temp.print();
temp2.print();  

Output is :

second one
second one

But When I make these 2 methods static the output is

firstone
secondone

what happened to late binding in this case?? Can anyone explain

Upvotes: 0

Views: 120

Answers (5)

varma
varma

Reputation: 11

static methods can not overridden. you created object for newer class by using statictest class reference variable temp2 u hold that object by using super class reference variable. At the time of compilation compiler just checks syntax weather that method is available in the statictest class or not.if it is available it complies fine otherwise error will come.your code you declared static print method so it is availble in statictest class compilation done fine but your method is static it can't be override. now coming main method u declared statictest temp = new newer(); now temp object is created with statictest class features only. it won't contains newer class methods or variables object is created based on the referenced class properties only it won't contains subclass properties(newer class) if super class(statictest) contains any non static values same as sub class(newer class) just it will overrides super class properties. why it overrides? because with the class it will not allow to declare same varibles or same methods

Upvotes: 0

Jayamohan
Jayamohan

Reputation: 12924

It is because static methods are not polymorphic. Static methods will not be Overridden.

Upvotes: 1

Ankit
Ankit

Reputation: 6622

static methods can not be overridden, they remains hidden if redefined in subclasses.

Ps: they do take part in inheritance. you can access static methods, from subclass name.

Upvotes: 1

Maximin
Maximin

Reputation: 1685

Do search on Dynamic method dispatch

Static methods can't be overridden, that is why after making it static you are getting output like this.

Upvotes: 0

Abimaran Kugathasan
Abimaran Kugathasan

Reputation: 32468

This is called dynamic method invocation. You can look on this JLS. It states,

The strategy for method lookup depends on the invocation mode.

If the invocation mode is static, no target reference is needed and overriding is not allowed. Method m of class T is the one to be invoked.

Otherwise, an instance method is to be invoked and there is a target reference. If the target reference is null, a NullPointerException is thrown at this point. Otherwise, the target reference is said to refer to a target object and will be used as the value of the keyword this in the invoked method. The other four possibilities for the invocation mode are then considered.

Upvotes: 1

Related Questions