Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Inheritance question

 
Jamal Hasanov
Ranch Hand
Posts: 411
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 249
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 411
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 249
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In above context the answer is no. add() of base will not be called
Regards,
Bhushan
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic