• 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
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Please explain this question from mock exam.

 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class Base
{
int i = 99;
public void amethod()
{
System.out.println("Base.amethod()");
}
Base()//line A
{
amethod();
}

}//end of class base

public class Derived extends Base
{
int i = -1;
public static void main(String argv[])
{
Base b = new Derived();//line 1
System.out.println(b.i);//line 2
b.amethod();//line 3
}

public void amethod()
{
System.out.println("Derived.amethod()");//line B
}
} //end of Derived class


OPTION A:

Derived.amethod()
-1
Derived.amethod()

OPTION B:

Derived.amethod()
99

OPTION C:

Derived.amethod()
-99

OPTION D:

Derived.amethod()

OPTION E:
Compile time error


The correct answer given is option B


But How this can Be?

I traced it as follows:

First when line 1 is executed control transfers to line A which calls "amethod".

"amethod" in derived class is called.SO line B iis executed first,thus printing "derived.amethod()".

Then line 2 is executed and o/p should be "-1" right?How it can be "99"?

Then line 3 is executed,and should print "derived.amethod()".

So on the whole the o/p I guessed is

Derived.amethod()
-1
Derived.amethod()

Anybody please explain this.
 
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

polymorphism applies only to instance methods not variables.The answer i guess is :
derived.amethod
99
derived.amethod.

But in option B there is no third statement.pls correct me if I am wrong.
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes..the answer for this should be:
Derived.amethod()
99
Derived.amethod()

But I thought that it'll print like this:
Base.amethod()
99
Derived.amethod()

becoz, when we are instantiating the Derived class, the default Derived constructor will be called, which calls the super (Base) constructor.
Since Base constructor is calling the amethod(), it should call amethod() of class Base and should print "Base.amethod()". But it is calling "Derived.amethod()"............can any one clarify this to me?
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Howdy, "Musu Musu"!

can any one clarify this to me?


The calls from constructors simply behave also polymorphically.
When you call in line 1:
Base b = new Derived();

the constructor calls the overridden method of the object.

By the way,
thanks for one of your first contributions to this forum and...

Welcome to the Ranch!




Hope you'll enjoy.




Only one small issue: The Java Ranch follows a certain policy regarding user names.
The main reasons why and a link how to change yours you'll find here:
http://www.javaranch.com/name.jsp


So, could you please change your user name before your next posting?
It will not affect anything you've already posted here. Just your user name will update.


I'm posting this because I am one of the moderators of this forum.


Yours,
Bu.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic