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 Object Overloading question 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 "Object Overloading question" Watch "Object Overloading question" New topic
Author

Object Overloading question

pravin kumar
Ranch Hand

Joined: Nov 03, 2005
Posts: 30
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
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.


Patrick<br /> <br />SCJP/SCWCD/SCBCD/SCDJWS/SCEA<br /> <br />Currently working on:<br />SCJP 6
pravin kumar
Ranch Hand

Joined: Nov 03, 2005
Posts: 30
excellent patrick..thnks very much indeed
pravin kumar
Ranch Hand

Joined: Nov 03, 2005
Posts: 30
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());
}
}
Prabhu Venkatachalam
Ranch Hand

Joined: Nov 16, 2005
Posts: 502

answer will be "30". Because static methods cannot be overidden.


Prabhu Venkatachalam<br />SCJP 1.4,SCWCD 1.4<br />prabhu.venkatachalam@gmail.com
Gagan Deep
Ranch Hand

Joined: Aug 23, 2005
Posts: 47
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.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17256
    
    6

"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


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
praveen Shangunathan
Ranch Hand

Joined: Sep 06, 2005
Posts: 65
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
Parveen,

Thanks a lot, i understand....

Thanks
Gagan.
Gyanesh Sharma
Ranch Hand

Joined: Nov 27, 2005
Posts: 47
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: 862
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 ]

bitguru blog
praveen Shangunathan
Ranch Hand

Joined: Sep 06, 2005
Posts: 65
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Object Overloading question