Meaningless Drivel is fun!*
The moose likes Beginning Java and the fly likes Inheritance, overriding methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Inheritance, overriding methods" Watch "Inheritance, overriding methods" New topic
Author

Inheritance, overriding methods

Natalie Ap
Ranch Hand

Joined: Jan 09, 2009
Posts: 49
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 ??


Fiona
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3817

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.


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3817

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

Joined: Jan 09, 2009
Posts: 49
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

Joined: Jan 29, 2009
Posts: 11
Hi, try this example


Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3817

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

Joined: Jan 30, 2009
Posts: 27
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

Joined: Oct 13, 2005
Posts: 38045
    
  22
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Inheritance, overriding methods
 
Similar Threads
why superclass reference cant invoke subclass nonoverriden method
Question about Parent call childe method
a strange question about polymorphism
Inheritance and Polymorphism
Concept