Java Polymophism interview question.

Today I had an interview with a company, and one question was (I don't remember clearly, pardon me if it is confusing):

Class Instrument {
void play();
}

class Percussion extends Instrument
{
void play();
}

(Instrument) I = new Percussion();


If I use I.play(); which will be called? In Instrument or in Percussion?

I replied Percussion, but i was wrong, i was told in Java, method is virtual in default or something. I didn't catach it clearly and I didn't have the chance to ask him. Can anyone explain this to me a little bit? Thanks a lot.
heyday2004Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

objectsCommented:
Percussion is correct.
0
objectsCommented:
run this to verify:

class Instrument
{
    void play()
    {
        System.out.println("Instrument");
    }
}

public class Percussion extends Instrument
{
    void play()
    {
        System.out.println("Percussion");
    }
   
    public static void main(String[] args)
    {
        Instrument i = new Percussion();
        i.play();
    }
}

0
heyday2004Author Commented:
hey, thanks for the reply. I really can't remember the exact code. In what case it can be calling play() in the Instrument? I just know my answer was wrong but I can't remember the exact code. Thanks a lot.
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

vikraman_bCommented:
hi,

when u create a (sub class)object of type Base class
e.g  Instrument i = new Percussion();
always methods in subclass is called.

where as when u put variables instead of methods...
then when u invoke the variables always base class variables is called..
u can note down in the below program.
e.g
Instrument i = new Percussion();
i.count //base class is called.

      class Instrument
      {
            int count = 5;
            void play()
            {
                  System.out.println("Instrument");
            }
      }

      public class Percussion extends Instrument
      {
            int count = 7;
            void play()
            {
                  System.out.println("Percussion");
            }
   
            public static void main(String[] args)
            {
                  Instrument i = new Percussion();
                  i.play();
                  Instrument i2 = new Instrument();
                  i2.play();
                  Percussion p = new Percussion();
                  p.play();
                  System.out.println("Count :"+i.count);
            }
      }

//output

Percussion
Instrument
Percussion
Count :5
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
krakatoaCommented:
>>where as when u put variables instead of methods...
then when u invoke the variables always base class variables is called.. <<

Not true of course.

You only get "Count:5" printed because you call for "i.count". If you called for count or p.count, you would get "7". So you have accepted a wrong answer here.
0
vikraman_bCommented:
hi krakotoa,
u didn't understand the answer
i am mainly talking about
Instrument i = new Percussion();
i.count of percussion reference of Instrument type.

not like Instrument i = new Instrument();
i.count
and not Percussion p = new Percussion();
p.count

Is it clear ?
0
krakatoaCommented:
>> Is it clear ?

No, and it doesn't make it right either.

You might be getting the right output from the class variables, but that has nothing to do with how you explained they work.

>> then when u invoke the variables always base class variables is called.. <<

This is the part that is incorrect.
In fact it is completely opposite to the way it works.
0
vikraman_bCommented:
may be i misstyped..
>> then when u invoke the variables always base class variables is called.. <<

0
baboo_Commented:
>> then when u invoke the variables always base class variables is called.. <<

No - when you invoke a method using an object reference, the method of that name and signature that's lowest on the OBJECT's inheritance tree gets called.  The way I remember this is that if I were to make a reference variable of type Object for, say, an object of type Percussion:

Object o = new Percussion();

I could still call:

o.play();

even though Object has no play method.  But the reference is of type object.  So, in your example, when you created the following objects:

Instrument i = new Percussion();
Percussion p = new Percussion();

i.play();

and

p.play();

Call the same play() method.  But:

i.i

and

p.i

are different, because you're accessing two different variables - not just 2 different instances of the same variable.
Because you declared an i variable in both classes, both classes have their own separate i variable.  Here's an example of what I mean - using the above references:

(i.i == 5)                               TRUE
((Percussion) i.i == 7)            TRUE

If, instead, you only created an i in the Instrument class, and then initialized it differently in the constructors, *then* and only then would

(i.i == 7)

be true.

Sorry - only being nitpicky to help the questioner...  

baboo_
0
baboo_Commented:
OOps - I meant to write "count" instead of "i" for the name of the variables...
0
vikraman_bCommented:
Thanks for ur comment baboo
0
krakatoaCommented:
heyday2004 : you should really ask CS to reopen this question and reallocate the points, since it is not good practice to have a question like this flagged as correctly answered when it is evidently not.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.