Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Inheritance, overriding methods

 
Natalie Ap
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Animal a = new Cat();

Can someone describe wat this means in simple terms? If we need to call methods on the object a, all those methods in class Animal and those that are overrriden in class Cat can be called without error. But the VERSION of the methods called are the one within Cat.

For e.g.

-Animal
* eat()

-Cat
* eat()
* meow()


1) Only eat() can be called on object a since it exists in Animal and Cat. meow() cannot be called.
I noticed that the method eat() that is called is the one inside Cat class.

2) Can someone please explain, if the object a has been initialized using the Cat type, why is it that the extra method in Cat (meow()) not be called ??
 
Vijitha Kumara
Bartender
Posts: 3913
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
N Pats wrote:1) Only eat() can be called on object a since it exists in Animal and Cat. meow() cannot be called.


Yes, you are right. Reference is of type Animal so it knows only the methods defined in Animal class.

2) Can someone please explain, if the object a has been initialized using the Cat type, why is it that the extra method in Cat (meow()) not be called ??


You mean like this?



With this you are able to call both methods since Cat reference knows about both the methods.
 
Vijitha Kumara
Bartender
Posts: 3913
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I noticed that the method eat() that is called is the one inside Cat class.


Yes, this is an advantage of using polymorphism. You can refer a subtype object from super type and when you called a method which is overridden in subclass that method will be called at runtime.
 
Natalie Ap
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vijitha Kumara wrote:
N Pats wrote:1) Only eat() can be called on object a since it exists in Animal and Cat. meow() cannot be called.


Yes, you are right. Reference is of type Animal so it knows only the methods defined in Animal class.

2) Can someone please explain, if the object a has been initialized using the Cat type, why is it that the extra method in Cat (meow()) not be called ??


You mean like this?



With this you are able to call both methods since Cat reference knows about both the methods.


No , what i mean is, why does the object 'a' not know the meow method? After all the object made is of Cat class - when we did this - Animal a = new Cat();

Why would someone do this - Animal a = new Cat() and not this Cat c = new Cat(). What is the difference between the two and the practical use/advantage of one over the other.
 
Gurjit Sandhu
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, try this example


 
Vijitha Kumara
Bartender
Posts: 3913
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
N Pats wrote:No , what i mean is, why does the object 'a' not know the meow method? After all the object made is of Cat class - when we did this - Animal a = new Cat();

Because the method "meow" is not defined in class Animal but Cat.

If it does not know it, why would someone do this - Animal a = new Cat() and not this Cat c = new Cat(). What is the difference between the two and the practical use/advantage of one over the other.


That's a usage of a object oriented concept called polymorphism as I said in previous post. The usage would be very much desirable in small to large applications. Consider following class..



Here you can pass any array which consists of objects that are sub type of Animal. So you can reuse the method with any future subclasses of Animal which you may create.
 
Kieren Dixon
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
N Pats wrote:Animal a = new Cat();
1) Only eat() can be called on object a since it exists in Animal and Cat. meow() cannot be called.


Here is the Head First analogy:

Think of the variable, Animal a, as the 'remote control' to your object and think of each method of the class as a button on the remote control.
The reason you can't call the meow() method from the Animal control is because the button doesn't exist.

But the VERSION of the methods called are the one within Cat.
I noticed that the method eat() that is called is the one inside Cat class.

This is because although the reference variable is of type Animal, the JVM actually knows which type of object the variable is pointing to. Because of this knowledge the JVM automatically calls the overridden method of the actual object.

However, if you declare the method of the Animal class static, it will call the eat() from the Animal class, not cat.


 
Campbell Ritchie
Sheriff
Pie
Posts: 48968
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

That is a nice analogy from Head First. Thank you. What you actually want is not a meow() method in the Cat class, but a [not static] makeNoise() method in the Animal class which is overridden to return meow in Cat and woof in Dog. That's called polymorphism.

Don't make that sort of method static: your eat() method should eat grass in the Cow class and wildebeest in the Lion class.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic