• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

method overriding

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
here 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());
}
}

answer is 30..plz anyone explain me how

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The answer is 30. Static methods cannot be overridden.If done then methods are hidden.
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When new Derived() is called, the superclass' constructor is executed which is Base class, which in turn executes addValue() of Base class that makes value = 10, only then the Derived constructor is executed which calls addValue() of Derived class, that makes value = 30 (10+20).
 
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Pravin

In main method
An object of Derived class is created.By default, a subclass constructor first invokes the superclass (default)constructor.
so, implementation of Derived class is like this,
Derived()
{
super();
addValue();
}
In Base class constructor invokes addValue method.
Initial value of 'value ' is zero.
Afterthe execution of addValue ( Base class) , value of 'value' is 10
----execution of super class constructor is completed----
now the statements in subclass constructor will be started.
addValue method of 'Derived' class is invoked
Current value of 'value' is 10
after the execution of addValue method(Derived class), value of 'value' is 30.
S.o.p (b.getValue()) prints the current value of 'value'
Answer is 30.
Hope this helps u

Regards
Naresh
 
Naresh Gunda
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
for better understanding try the following code ...just added s.op statements

class Base{
static int value = 0;
Base(){
System.out.println("base class constructor");
addValue();
}
static void addValue()
{
System.out.println("base class addvalue method");
value += 10;
}
int getValue(){
return value;
}
}
class Derived extends Base{
Derived(){
System.out.println("Derived class constructor");
addValue();
}
static void addValue(){
System.out.println("base class addvalue method");
value += 20;
}
}
public class Test {
public static void main(String[] args){
Base b = new Derived();
System.out.println(b.getValue());
}
}

Output 30

To understand overriding concept - static method, just remove 'static' keyword for both the methods and execute
then output will be 40

Regards
Naresh
 
reply
    Bookmark Topic Watch Topic
  • New Topic