• 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

Object Overloading question

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
pravin kumar
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
excellent patrick..thnks very much indeed
 
pravin kumar
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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());
}
}

 
Ranch Hand
Posts: 502
jQuery Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
answer will be "30". Because static methods cannot be overidden.
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

what is the logic behind "static methods cannot be overidden." ?

I checked K&B, there i saw reference that final methods can't be overriden(quite logical) but not mentioned about static methods..

Any pointers ??
Thanks
Gagan.
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"Pravin"-
Welcome to the JavaRanch! Please adjust your displayed name to meet the

JavaRanch Naming Policy.

You can change it

here.

Thanks! and welcome to the JavaRanch!

Mark
 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Parveen,

Thanks a lot, i understand....

Thanks
Gagan.
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Author
Posts: 986
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ]
 
praveen Shangunathan
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hope the following example helps...

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();
}
}

//OUTPUT is
Static in A.
Non-static in B.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic