Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can this object access methods of it's parent class?

This question is taken from an AP Computer Science practice test.

public class Bird
{
    public void act()
    {
        System.out.print("fly"); 
        makeNoise(); 
    }
    public void makeNoise()
    {
        System.out.print("chirp"); 
    }
}

public class Dove extends Bird
{
    public void act()
    {
        super.act(); 
        System.out.print("waddle"); 
    }
    public void makeNoise()
    {
        super.makeNoise(); 
        System.out.print("coo"); 
    }
}

Suppose the following declaration appears in a class other than Bird or Dove:

Bird pigeon = new Dove(); 

What is printed as a result of the call pigeon.act()?

I thought the answer would be "fly chirp", but the textbook says that the answer is "fly chirp coo waddle". I thought that 'pigeon' could only access methods available in Bird? I was under the impression that, if the user wanted to access methods in Dove, 'pigeon' would have to be cast to Dove.

Would Bird pigeon = new Bird(); give the same output? How about Dove pigeon = new Dove();?

like image 968
Caroline Yi Avatar asked Jan 05 '23 02:01

Caroline Yi


2 Answers

Long story short, when you access act method of pigeon, its override from Dove is called.

I thought that 'pigeon' could only access methods available in Bird?

That is certainly true, at least, for situations when no casting is applied. However, method act is available on the class Bird, the statically known type of pigeon, so the call compiles fine.

However, accessing methods is only about being able to call them. What methods do when you call them is decided at runtime based on the dynamic type of pigeon. This is where method overriding comes into play, because Dove overrides Bird's methods, but in addition it also calls Bird's methods. That is why the code hits all four printouts.

Would Bird pigeon = new Bird(); give the same output?

No, the output would be different, because in this case both dynamic and static types of pigeon would be the same, i.e. Bird, so only Bird's implementations would be invoked.

like image 180
Sergey Kalinichenko Avatar answered Jan 06 '23 15:01

Sergey Kalinichenko


  • the class Dove does override the methods act and makeNoise of class Bird. Overriding means changing the behavior of a method visible to the sub-class, (in your case Dove). Methods are visible to a sub-class if they have a public or protected access modifier or if they have a package private access modifier and the sub-class belongs to the same package as the super-class does.
  • pigeon is an instance of Dove.
  • calling pigeon.act() results in calling Dove.act.
  • Dove.act calls super.act which is Bird.act.
  • Bird.act prints fly and calls makeNoise on pigeon resulting in calling Dove.makeNoise.
  • Dove.makeNoise calls super.makeNoise which is Bird.makeNoise.
  • Bird.makeNoise print chirp.
  • Dove.makeNoise prints coo after calling super.makeNoice
like image 24
Harmlezz Avatar answered Jan 06 '23 16:01

Harmlezz