File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Overloading wih static methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Overloading wih static methods" Watch "Overloading wih static methods" New topic
Author

Overloading wih static methods

ram shah
Greenhorn

Joined: Jun 02, 2007
Posts: 28
what will be the output?


1. 10
2. 20
3. 30
4. 40

ANS : 3
----------------------------------------
[ June 05, 2007: Message edited by: Barry Gaunt ]
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Hi,


Polymorphism does not apply to static method. In the Base class constructor
Base version of the method will be called even if the object created in main
is of Derived class. Same happens in the case of Derived class constructor,
derived class version of the static method is called. Resulting value 30.

If you remove static from both the result will be 40. Because both the times
Derived class overridden method will be called.


Thanks,


cmbhatt
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
The subject says overloading it should be overriding.
chaitanya gopal
Greenhorn

Joined: May 22, 2007
Posts: 13
Hi Chandra Bhatt,

I understand how it works when using static...
but i didn't get how it works when we delete static....

bcoz when derived class constructor calls the base class constructor,at that time it calls the derived class addValue() method instead of the base class addValue() method...why...will you explain it more clearly....

Thanks,
Chaitanya.
Brian Spindler
Greenhorn

Joined: May 17, 2007
Posts: 29
Originally posted by Anupam Sinha:
The subject says overloading it should be overriding.


this is actually a redefinition, you cannot override static method which is why it works the way it does.

When you remove the static definition the addValue() method is then overridden (in which case it is the only to be called).
Padma Asrani
Ranch Hand

Joined: Mar 22, 2007
Posts: 111
Hi All,

I still couldn't get why the output is 30. I was thinking that the output should be 20.

class Base{
static int value = 0;
Base(){
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());
}
}

When we are calling addValue() inside Derived class constructor then why it doesn't call the addValue() of Base class. I expected to call addValue of Base class n I refered the 2nd Chapter of K& B book in which I saw following example

class Animal {
static void dostuff() {
System.out.print("a ");
}
}
class Dog extends Animal {
static void dostuff() { // it's a redefinition,
// not an override
System.out.print("d ");
}
public static void main (String [] args) {
Animal [] a = {new Animal(), new Dog(), new Animal()};
for (int x = 0; x < a.length; x++)
a[x].doStuff(); // invoke the static method
}
}

Running this code produces the output:

a a a

Regards
Padma
Brian Spindler
Greenhorn

Joined: May 17, 2007
Posts: 29
Ahh... now I see where the confusion comes from ( I think)

In the preceding example from K&B the a[x].doStuff() is a method call on the Animal reference array. being defined as:



The compiler can only check that Dog() IS-A animal and since this succeeds the code succeeds, HOWEVER since static does not adhere to polymorphism there is no dynamic linking during runtime of the new Dog().doStuff(), it executes Animal.doStuff().

In the preceding example you are implicitly executing the class method (static) in each constructor.
rajesh baba
Greenhorn

Joined: May 19, 2007
Posts: 25
class Base{
static int value = 0;
Base(){
addValue();
}
static void addValue(){
value += 10;
}
int getValue(){
return value;
}
}
class Derived extends Base{
Derived(){
super();
addValue();
}
static void addValue(){
value += 20;
}
}
public class Test {
public static void main(String[] args){
Base b = new Derived();
System.out.println(b.getValue());
}
}
the complier inserts super() method in the derived class so once you called new derived ()this method call base() method that calls addvalue method in the base class so value=10 now and control returns to derived() methos this again calls addvalue() method so value=30 that the output
Padma Asrani
Ranch Hand

Joined: Mar 22, 2007
Posts: 111
Hi

My doubt still remains. How does it matters if the static method is called from the constructor. The behaviour should be same even it is called from the main method. In the K & B example, it says that calling the static method depends upon the type of the variable, So if the reference type is animal, it will call the dostuff method defined in animal, now if we consider's another example in which static called from constructor then I guess the reference type is still the base class, so the addValue should be called from the base class twice. One by super() keyword inside derived class constructor and secondly explicit call to addValue() method.

Regards
Padma
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Things to keep in mind:


#1- Static methods are not overridden but that can be redefined that is
happening in original post.
#2- Non static methods can be overridden provided they are not final and
visible to the subclass (not private of course).

Scenario with the original question:
3- When you create subclass object from the main method, you see in both
the constructors there is call to the method. See point #1, so from the
parent class constructor its own addValue() method will be called. And from
the Derived class, its own addValue() method, resulting value 30.

Scenario when you remover static from both the methods:
In one word "POLYMORPHISM"
4- You created object of Derived class, then from the Base class addValue()
will be the overridden version not of the Base class; this is called
"run time method selection". From the Derived class the method be obviously
its own, resulting result 40.

Note: If you don't explicitly insert call to super() or this() in the
constructor the compiler automatically inserts super();



Thanks,
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Overloading wih static methods