aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes a strange question about polymorphism Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "a strange question about polymorphism" Watch "a strange question about polymorphism" New topic
Author

a strange question about polymorphism

Teng Sun
Greenhorn

Joined: Oct 19, 2004
Posts: 3
please look at the following code first...

public class Animal {

public Animal() {}

private final void run() {
System.out.println("This is a private final method in Animal!");
}

public static void eat() {
System.out.println("Animal must eat!");
}

public static void main(String[] args) {
Animal animal = new Cat();
animal.run();
animal.eat();
}
}

class Cat extends Animal {

public Cat() {}

public static void eat() {
System.out.println("Cats like eating fish!");
}
}

In the main method of Animal, Animal animal = new Cat(); the animal variable refers to a Cat object in fact, so it should execute the method of Cat. But the Cat is the subclass of Animal, and it could neither extends the private and final methods nor see them. And to my surprise, the output is:
This is a private final method in Animal!
Animal must eat!
Besides, the static method eat in Cat must be a redefining method of Animal, not override i think.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Hi,

Welcome to JavaRanch!
You haven't really told us what your question about this is. Code in the Animal class calls a private Animal method, and calls a static Animal method using a reference of type Animal. Nothing here is surprising to me. Tell me what part is surprising to you?

Maybe you expected the eat() method to be polymorphic, but in fact, static methods never are. The compiler chooses what static method will be invoked, not the runtime system. The compiler looks only at the compile-time type of the variable "animal", and doesn't know or care that animal points to a Cat.


[Jess in Action][AskingGoodQuestions]
Teng Sun
Greenhorn

Joined: Oct 19, 2004
Posts: 3
Thanks, i'm a new comer here.
Animal animal = new Cat();
The animal refers to a cat object in fact, and it should execute the cat's method at runtime, but the cat doesn't have a run() method. So i feel a little confusion...
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Because Cat is a subclass of Animal, the method Animal.run() is also available as Cat.run().
Teng Sun
Greenhorn

Joined: Oct 19, 2004
Posts: 3
Although the cat is a subclass of animal, but it doesn't extends the animal's private final void run() method, how can it execute the run() method?
Besides, I send a e-mail to you, is it ejfried@javaranch.com? Waiting for your reply, thanks... And my e-mail: ebusiness022@yahoo.com.cn
Peixiao Lin
Greenhorn

Joined: Oct 04, 2004
Posts: 28
In this case, once you instantiate cat class, cat constructor will also create animal instance because cat extends animal. So you can use an animal reference to access run() method though it refer to cat object actually. About eat(), cat override it. So the output is cat version. I don't know if this can answer your question.
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
About eat(), cat override it. So the output is cat version.


Actually, you get the Animal version of eat()

When you use a reference variable to access a static method, the compiler just converts the reference type into its class name. So animal.eat() becomes Animal.eat() and the actual object referenced by animal is irrelevant. In fact, animal.eat() would work just the same if animal==null


Mike Gershman
SCJP 1.4, SCWCD in process
Atul Chandran
Greenhorn

Joined: Oct 24, 2004
Posts: 22
A reference of Animal type can call only the methods that are declared in the class Animal. The version of the method called is deffered till the runtime as the compiler is not sure what object the reference will refer. But in this case on seeing the keywords final or private the compiler can assume that there is only one version for this method and bind it at compile time.I think that's why the run() of Animal class gets called.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: a strange question about polymorphism