• 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

base class object problem

 
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,

I have a program which is printing 22, but i am not getting how?
i think it is calling Extension object so it should print 16...

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

please explain how this program will work

Sheetal
 
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the main method, you call new Extension(). This creates an Extension object and calls its constructor, Extension().

The first implicit statement in the Extension() constructor is a call to super(), which calls the constructor of the superclass, Base(). The first implicit statement in Base() is a call to its superclass's constructor, Object(), which does nothing much.

After the call to Object() completes, the variable i defined in Base gets set to 0 for this object.

The code in Base() consists of one statement: add(1);

Here's the tricky part: Because the object is really an Extension object, not a Base object, and since the add method in Extension is accessible to the Base() constructor, the call add(1) invokes the overriding method add in the Extension class, not the add method of the Base class.

The add method in the Extension class multiplies the argument (1) by 2, and then increases the value of i by the result. 0 + 1*2 results in 2, so i is now set to 2.

The Base() constructor finishes execution, and control goes back to the Extension() constructor.

The Extension() constructor calls add(2), which still uses the overriding Extension add method. In add, v is set to 2, so the statement becomes
i += 2*2, or i += 4. Since i was already 2, and 2 + 4 = 6, i is now set to 6.

The Extension() constructor completes its execution. The object has been constructed with an i value of 6.

The bogo method takes the object, and calls the add method on it again, this time with an argument of 8.
i += 8*2 evaluates to i += 16, and i was already 6. 6 + 16 = 22, so i is set to 22.

b.print() invokes the inherited print method from Base, which prints the value of i, which is 22.

Is that clear? It's a very complicated problem.
[ May 06, 2005: Message edited by: Joe Sanowitz ]
 
Ranch Hand
Posts: 300
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You may want to read this comprehensive article on polymorphism, especially the 2nd and 3rd pages - Behold polymorphism from a type-oriented point of view
 
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here new Extension() creates a subclass object and assigned to super class reference Base b. Method add () available in superclass and subclass also. So add() method is overriding.

Well. New Extension() call the subclass constructor Extension(). But here internally call a super class constructor super(). So base class constructor called add(1). Here add() method is overriding. It is called from derived class constructor. So it invokes derived class add() method.

add(1){ I+=v*2) // here I value is 2. Then it called the subclass constructor add(2)
add(2){I+=v*2)//here I value is 6 then finally it called the b.add(8)
add(8){I+=v*8)// her I value 22.

 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
An overridable method should never (yes I am a strong supporter of dogmatism and fundamental truths) be called from a constructor on a 'this' reference. I have strong doubts that the exam will contain such content, and if it does, someone needs to be shot.

Here's something related:
http://qa.jtiger.org/GetQAndA.action?qids=10
 
Ranch Hand
Posts: 485
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Tony i am sorry to ask why the constructor should not call the overriddable method. can u explain me why not??
 
Ranch Hand
Posts: 817
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi can u tell me in what case
that in constructor

this () can only call static method ...
its written somewhere in K&B but i m confused with this...

and what will heppan in above case...when
add(int) is declared in a class that is declared private

thanx

pls answer both queries
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic