Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Confused with inheritance.

Status
Not open for further replies.

Tiamose

Programmer
Nov 13, 2007
10
VN
Hi all,

I wrote a class Point and a class SubPoint which extends the class Point (see the code below). Then I declared a variable sp of type SubPoint and I called sp.tell(). Could anyone tell me why the method speak() of the class SubPoint is invoked although I already call super.tell() within the methode tell() of SubPoint?

Thank you very much.

==========================================================


class Point {

public void speak(){
System.out.println("Point.speak()");
}
public void tell(){
System.out.println("Point.tell()");
speak();
}
}

class SubPoint extends Point{
public void speak(){
System.out.println("SubPoint.speak()");
}

public void tell(){
System.out.println("SubPoint.tell()");
super.tell();

}
}

public class Test{
public static void main(String[] args){
SubPoint sp = new SubPoint();
sp.tell();
}
}
 
OK, the first thing that strikes me here, is that you didn't exactly EXTEND anything...instead you REMADE the Point class with your SubPoint class.
Try the following classes instead, see if this answers your question:
Code:
public class Test 
{


    public static void main(String[] args)
    {
        SubPoint sp = new SubPoint();
        sp.tell();
        sp.speak();
    }

}
Code:
public class Point
{

    public void speak()
    {
        System.out.println("Point.speak()");
    }

}
Code:
public class SubPoint extends Point
{

    public void tell()
    {
        System.out.println("SubPoint.tell()");
    }

}
 
Yes, I know that I did not extend anything. But the my purpose is just to verify some characteristics of polymorphism. I think that this is quite strange that the methode tell() of SubPoint in invoked.
 
Remember that System.out is not synchronized in any way. You can not reliably predict in what order the messages are processed.
Adding a Thread.Sleep(200) could make the order stable.
 
OK, first off, it looks like I was more tires last night than I thought....totally missed the super.tell() line in your code :-/

Anywise, I recopied and ran your classes, and got this output from it:
Code:
SubPoint.tell()
Point.tell()
SubPoint.speak()

Assuming this is what you're getting as well, then I'm going to have to go with the explanation that your SubPoint.speak() (and SubPoint.tell() for that matter) are still overriding your Point.speak() (and Point.tell()) methods, so the JVM/compiler is looking at the object type (SubPoint) and calling the SubPoint routines whenever there isn't an explicit reference to the super.method().
Therefore when you call super.tell() it sees the explicit reference, and runs the Point.tell() method, but when that method runs tell() with no explicit pointer as to which one, the JVM opts for the one in SubPoint, as that's the class of your object.

-Hope I clarified more than I muddled;

I hope this helps;
robherc
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top