Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

INHERITANCE: reason behind that??

inheritance

  • Please log in to reply
3 replies to this topic

#1 himanshu

himanshu

    CC Lurker

  • Just Joined
  • Pip
  • 8 posts

Posted 17 January 2010 - 08:25 AM

public class Testinheritance
{
public void instancemethod(String...a)
{
System.out.println("instance parent");
}
}
class Testinheritance2 extends Testinheritance
{
public void instancemethod(String a)
{
System.out.println("instance child");
}
public static void main(String args[])
{
Testinheritance obj=new Testinheritance2();
Testinheritance2 obj2=(Testinheritance2)obj;
obj.instancemethod("abc");
obj2.instancemethod("abc");
}
}

output:
instance parent
instance child



please tell me the reason why instance of parent is called in first case
  • 0

#2 ZekeDragon

ZekeDragon

    CC Leader

  • Retired Mod
  • PipPipPipPipPipPipPip
  • 1263 posts

Posted 17 January 2010 - 08:55 AM

The difference may be difficult to spot, but the reason for this is actually quite simple, you're not overriding the function, you're overloading it!

public void instancemethod(String... a) and public void instancemethod(String a) are two different functions, so what the Java compiler will do instead of overriding the function and letting the inherited method take over is it'll produce a "candidate list" of each possible function and try and find the first match. When the object is considered a Testinheritance object, the candidate list starts from the parent object and moves down to the child objects, however when it starts at the child class it goes down the inheritance tree, then circles back and goes to the parent. This means that each reference to the same object will, during compilation, result in two separate candidate lists looking like this:
Testinheritance:
    public void instancemethod(String... a);
    public void instancemethod(String a);

Testinheritance2:
    public void instancemethod(String a);
    public void instancemethod(String... a);
So in each case the result for the Java Runtime will be to go with the first possible match, which in the case of the parent is (String... a) and in the case of the child is (String a). To rectify the problem, simply change the parameters so that they match correctly, and polymorphism will take over.
  • 0
If you enjoy reading this discussion and are thinking about commenting, why not click here to register and start participating in under a minute?

#3 CallinWire

CallinWire

    CC Lurker

  • Just Joined
  • Pip
  • 8 posts

Posted 17 January 2010 - 09:01 AM

The answer is due to a messy combination of polymorphism, as well as overloading the method instead of overriding it - both of which can accept one String as a parameter.

EDIT: Ah, so that's why.
  • 0

#4 himanshu

himanshu

    CC Lurker

  • Just Joined
  • Pip
  • 8 posts

Posted 17 January 2010 - 09:15 AM

thnx Zeke
  • 0





Also tagged with one or more of these keywords: inheritance