aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Please help me....... 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 "Please help me......." Watch "Please help me......." New topic
Author

Please help me.......

Supriya
Greenhorn

Joined: Oct 27, 2000
Posts: 9
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

Joined: Oct 10, 2000
Posts: 229
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

Joined: Oct 22, 2000
Posts: 7
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

Joined: Oct 27, 2000
Posts: 9
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Please help me.......