Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Please help me.......

 
Supriya
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this question is from mock exam #36 on Maha's site:
Question 13
Object Construction II
Determine the result of attempting to compile and run the following code:
class A {
public int Avar;
public A() {
System.out.println("AAA");
doSomething();
}
public void doSomething() {
Avar = 1111;
System.out.println("A.doSomething()");
}
}
public class B extends A {
public int Bvar = 2222;
public B() {
System.out.println("BBB");
doSomething();
System.out.println("Avar=" + Avar);
}
public void doSomething() {
System.out.println("Bvar=" + Bvar);
}
public static void main(String[] args) {
new B();
}
}
the explanation says that when the doSomething() method is invoked from within A's constructor, it is actually B's version of doSomething() that is being invoked. How is this possible?

 
Harpal Singh
Ranch Hand
Posts: 229
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Supriya:
this question is from mock exam #36 on Maha's site:
Question 13
Object Construction II
Determine the result of attempting to compile and run the following code:
class A {
public int Avar;
public A() {
System.out.println("AAA");
doSomething();
}
public void doSomething() {
Avar = 1111;
System.out.println("A.doSomething()");
}
}
public class B extends A {
public int Bvar = 2222;
public B() {
System.out.println("BBB");
doSomething();
System.out.println("Avar=" + Avar);
}
public void doSomething() {
System.out.println("Bvar=" + Bvar);
}
public static void main(String[] args) {
new B();
}
}
the explanation says that when the doSomething() method is invoked from within A's constructor, it is actually B's version of doSomething() that is being invoked. How is this possible?

Supriya,
in your main..the new object B is created ...so there is a call to B() constructer..Since B is extending from A..there will be a call to A's default Constructor..In A() there is a call to method doSomething()..which has been overridden in class B...Hence the call is made to B's doSomething() method...this is concept of Overridding.......
Hope it is clear...
Harpal..
 
midrisi
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Supriya,
As you have seen that B is being inherited thru A class and thru B main() method there is creation of B's class reference using new B().
This will invoke B() default custructor method as usual and after entering B() function it will call A() default constructor because as rule in java that parent class constructor should be intialised first then inherited class..so in this case compiler insert one statement called super(); just before System.out.println("BBB");
because u r not calling it explicitly...so it will implicitly insert it.
Hence due to it control goes to A class default constructor display a string "AAA" and after doSomething() of class A will be complied but remember in this case it is already overridden but u have a invoked it thru a object which is an instance of B class so "this" point to doSomething of B class only not A class method.
Hope u will get the point.May be the language would be confusing...sorry fot it.
If I would be get wrong pl correct me.
Thanks alot,
Sharif
code:


[This message has been edited by midrisi (edited October 27, 2000).]
 
Supriya
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Harpal and Midrisi,
I was confused abt. how B's doSomething() method is invoked from within A's constructor.overlooked that its invoked thru an object which is an instance of B class.
thanks guys for clearing the confusion.
Supriya
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic