• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt Regarding static reference

 
ansuman mohapatra
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class Fub {
public static void m(){ System.out.println("fub"); }
}
public class Foo extends Fub {

public static void m(){
System.out.println("foo");

}
public static void main(String[] args){
Fub f = new Foo();//line 1
f.m(); // line 2
m();
}
}


The above program gives me doubt at line 1 and 2. when you contain the reference of subclass in the superclass reference variable then the variable should point to the method of the subclass as done in method overriding. But here its pointing to the superclass method. So I doubt whether it has anything to do with the declaration of methods as static. Please clarify
 
sridhar row
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have the same doubt..can someone explain please
 
agilemanoj kumar
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Here, you have declared static method means, you should always think about reference not object...
Now, in Fub f = new Foo(); //line 1 , you have created an object of type Foo but reference is of super class... Just told, think about reference... f.m(); will call super class static method...
Next is m(); ... no reference... so overridden static method will be called...
 
Joshua Antony
Ranch Hand
Posts: 254
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In short, static methods cannot be overridden.
 
Prasad Maddipatla
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
While it comes to Static methods static methods are never get inherited. So that mean you cannot override it, then what it comes to defining it in the subclass is termed it as "REDIFING".

So the Dynamic Method Dispatch wont work when i comes to Static Methods.

And it depends on the type of instance rather than the type of Object.

Hope it clears a bit.
 
Madhukar Ojha
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Static methods are not inherited .
Method to be invoked is resolved at compile time .
Method is invoked according to the actual reference type
not as actual object type .
 
ansuman mohapatra
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes that helped...thanks
 
Ram Manoj
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ronaldo,Prasad,Madhukar

I want to clarify/"get clarified" on the topic applicability of Inheritance, Polymorphism to static methods/variables.

You say static methods cannot be inherited thereby cannot be overridden.
I suppose this is not the case. Just compile and run the following code.


Inheritance applies for Static methods/variables.
When it comes to Polymorphism, here is a snippet from K & B book, page 98

Polymorphic method invocations apply only to instance methods. You can
always refer to an object with a more general reference variable type (a superclass
or interface), but at runtime, the ONLY things that are dynamically selected
based on the actual object (rather than the reference type) are instance methods.
Not static methods. Not variables. Only overridden instance methods are
dynamically invoked based on the real object's type.


I suppose you agree with me.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic