• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Overloading wih static methods

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what will be the output?


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

ANS : 3
----------------------------------------
[ June 05, 2007: Message edited by: Barry Gaunt ]
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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,
 
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The subject says overloading it should be overriding.
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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).
 
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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,
 
yeah, but ... what would PIE do? Especially concerning this tiny ad:
New web page for Paul's Rocket Mass Heaters movies
https://coderanch.com/t/785239/web-page-Paul-Rocket-Mass
reply
    Bookmark Topic Watch Topic
  • New Topic