[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 818
  • Last Modified:

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.
0
heyday2004
Asked:
heyday2004
  • 4
  • 3
  • 2
  • +2
2 Solutions
 
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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
 
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

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 4
  • 3
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now