class Base{ int value = 0; Base(){ addValue(); } void addValue(){ value += 10; } int getValue(){ return value; } } class Derived extends Base{ Derived(){ addValue(); } void addValue(){ value += 20; } } public class Test { public static void main(String[] args){ Base b = new Derived(); System.out.println(b.getValue()); } }
output of this class is 40..plz explain how does it executes to get that value
Patrick Williams
Ranch Hand
Joined: Apr 03, 2005
Posts: 213
posted
0
The code that you've listed above created a Derived object with a Base reference. When the Derived object is created, it calls the Derived constructor. Derived is a subclass of Base so the super() constructor is implicitely called from the Derived constructor. The Base constructor calls addValue(), but because Derived overrides the addValue() function, the addValue() function from Derived is called from the Base constructor. When the code returns from the Base constructor, the Derived constructor calls its own addValue(). Both times the Derived addValue() is called, it adds 20. 20 + 20 = 40. I hope that this explanation helps.
Almost the same code as in the previous question. The only difference is the methods are static now. What will it print now?
class Base{ static int value = 0; Base(){ addValue(); } static void addValue(){ value += 10; } int getValue(){ return value; } } class Derived extends Base{ Derived(){ addValue(); } static void addValue(){ value += 20; } } public class Test { public static void main(String[] args){ Base b = new Derived(); System.out.println(b.getValue()); } }
overriding uses runtime polymorphism. so references are resolved at runtime to check which object(parent ot child) should be used for the method in question. since "static" is not tied to any object in particular but to a whole class, we dont wait till runtime to get the method. so for static methods there is no runtime polymorphism, thereby there is no override. altho u can (what loooks liek) override static methods , it will not work like the way you will expect an overridden method to behave.
Gagan Deep
Ranch Hand
Joined: Aug 23, 2005
Posts: 47
posted
0
Parveen,
Thanks a lot, i understand....
Thanks Gagan.
Gyanesh Sharma
Ranch Hand
Joined: Nov 27, 2005
Posts: 47
posted
0
If static methods can't be overridden and it does not give a complier error, then it seems to me that an attempt to override a static method results in a new method that shadows the inherited method.
Is there any way the sub class can call the inherited method? I am sure the super class can not call the sub class version of the static overridden (not technically) method.
Please respond.
Brian Cole
Author
Ranch Hand
Joined: Sep 20, 2005
Posts: 852
posted
0
Originally posted by Gyanesh Sharma: If static methods can't be overridden and it does not give a complier error, then it seems to me that an attempt to override a static method results in a new method that shadows the inherited method.
correct
Is there any way the sub class can call the inherited method?
One way Derived can call the shadowed method is: super.addValue()
I am sure the super class can not call the sub class version of the static overridden (not technically) method.
Well any class (including Base) can call Derived's addValue() method if it is static: Derived.addValue()
(OK, since it is declared package private, only classes in the same package can call it, not any class.)
Along these lines, the other way Derived can call the shadowed method is: Base.addValue() [ December 06, 2005: Message edited by: Brian Cole ]
class A { public static void staticMethod() { System.out.println("Static in A."); } public void nonStaticMethod() { System.out.println("Non-static in A."); } }
class B extends A { public static void staticMethod() { System.out.println("Static in B."); } public void nonStaticMethod() { System.out.println("Non-static in B."); } }
class StaticHide { public static void main(String[] args) { A myA = new B(); myA.staticMethod();//can also do just A.staticMethod(); myA.nonStaticMethod(); } }