| Author |
Doubt Regarding static reference
|
ansuman mohapatra
Greenhorn
Joined: Mar 04, 2008
Posts: 27
|
|
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
|
Cleared SCJP.....
|
 |
sridhar row
Ranch Hand
Joined: Jan 16, 2008
Posts: 162
|
|
|
I have the same doubt..can someone explain please
|
 |
agilemanoj kumar
Ranch Hand
Joined: Mar 07, 2008
Posts: 70
|
|
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...
|
Manoj Kumar
|
 |
Joshua Antony
Ranch Hand
Joined: Jun 05, 2006
Posts: 254
|
|
|
In short, static methods cannot be overridden.
|
SCJP,SCWCD, Into ATG now!
|
 |
Prasad Maddipatla
Greenhorn
Joined: Apr 20, 2007
Posts: 24
|
|
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
Joined: Mar 21, 2007
Posts: 71
|
|
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 .
|
SCJP 5 ๑۩۞۩๑♥~~ My Life is My Creation ~~♥๑۩۞۩๑
|
 |
ansuman mohapatra
Greenhorn
Joined: Mar 04, 2008
Posts: 27
|
|
|
yes that helped...thanks
|
 |
Ram Manoj
Ranch Hand
Joined: Jan 12, 2008
Posts: 52
|
|
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.
|
 |
 |
|
|
subject: Doubt Regarding static reference
|
|
|