This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Inheritance question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Inheritance question" Watch "Inheritance question" New topic
Author

Inheritance question

Jamal Hasanov
Ranch Hand

Joined: Jan 08, 2002
Posts: 411
Source code:
************************************************
class Base {
int i;
Base() {
add(1);
}
void add(int v) {
i += v;
}
void print() {
System.out.println(i);
}
}
class Extension extends Base {
Extension() {
add(2);
}
void add(int v) {
i += v*2;
}
}
public class Qd073 {
public static void main(String args[]) {
bogo(new Extension());
}
static void bogo(Base b) {
b.add(8);
b.print();
}
}
************************************************
Result : 22
Very strange behaviour:
1. main creates new Extension() and
a) Base() called as a parent constructor
b) Base calls add() in Extension ??? (why)
c) Extension() called as a constructor
d) constructor calls add() method in Extension
2. Base b = new Extension() happens
a) add(8) method of Extension is called
b) result printed
Question :
Why Base() constructor (in step 1.b) calls add() method of Extension, not Base?

Thanx,
Bhushan Jawle
Ranch Hand

Joined: Nov 22, 2001
Posts: 249
Hi Jamal,
This is an example(bit confusing :-) ) of overriding.
As the variable is of type base class(static type), but the actual type of object(runtitme type) is of derived type. In this a case Java always choses the overriding method. Pls. let me know if this answers your question.
Rgds.,
Bhushan
Jamal Hasanov
Ranch Hand

Joined: Jan 08, 2002
Posts: 411
Thanx a lot for explanation.
I new that, this behaviour is a result of overriding. But 1 question : due to overriding add() method of Base class will never be called? (It has overriden,new version in Extension?)
I know, if i construct a Base class
Base b = new Extension();
then all methods will be called from Extension, and all variables from Base.
But in this situation, Base class is created as a result of constructing Extension. (inheritance rule, Extension()=Base() and Extension() )
Bhushan Jawle
Ranch Hand

Joined: Nov 22, 2001
Posts: 249
In above context the answer is no. add() of base will not be called
Regards,
Bhushan
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
If you call
Base b = new Base();
b.add(5)
then the add() method of Base will be called, because at runtime the actual object *instance* in the variable "b" is a Base object.
When you call
Base b = new Extension();
b.add(5)
then the add() method of Extension is called, because the actual instance in variable b is an Extension object.
Rob


Rob
SCJP 1.4
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
hi all,
to make it little bit more complicated try to put,
int i in class Extension
and after trying that override print() method in Extension (just printing var i) and see the effect.
regards
maulin.
Wagner Danda
Greenhorn

Joined: Jul 10, 2001
Posts: 26
When you put 'int i' on the Extension class, you can see that it prints '0'... This is because you shadow the variable 'i', and when you call the print() method (from Base) it shows the value of 'i' on the Base class.
Then, if you put the print() method on the Extension class, you can see printed '22'... So, now you are using the 'i' variable of the Extension class...
Wagner Danda


Wagner Danda<br />Sun Certified Programmer for Java 2 Platform
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Inheritance question
 
Similar Threads
Overriding and overloading
Another question
Stranger super()
Constructors
How to read this program?