aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes 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 "Polymorphism" Watch "Polymorphism" New topic
Author

Polymorphism

Marcelo Ortega
Ranch Hand

Joined: May 31, 2005
Posts: 519

Hi Java friends,

Could somebody please explain what point i am missing out on when it comes to Polymorphism.
i.e:


class Animal{

void play(){
System.out.println("I am playing like an Animal!");
}

void eat(){
System.out.println("I am eating like an Animal!");
}

}


class Dog extends Animal{

void play(){
System.out.print("I am playing like a Dog!");
}

void bark(){
System.out.println("Wolf Wolf...");
}

}

public class Poly1 {

public static void main(String[] args) {

Animal a = new Dog();

//a.bark(); Error, beacause the bark method isn't in the Animal class

Dog b = new Dog();

a=b;
a.play();
a.bark(); //Error again, but why?, the "a" reference variable
//refers to a Dog object of TYPE DOG

}

}

But i don't understand why when i do this:

Animal a = new Dog();

i can't refer to any methods in the Dog object using the reference variable "a" which i haven't overriden, not even instance vars.

???

Please help me people.

Thank you all.


SCJP 1.4, SCWCD 1.4, SCBCD 1.3, SCJD, SCEA/OCMJEA

Live life to an interface, not an implementation!
Steven Bell
Ranch Hand

Joined: Dec 29, 2004
Posts: 1071
When you write

Animal a

you are declaring that the variable 'a' will now and always refer to an Animal object. You are able to do

Animal a = new Dog();

because a Dog is an Animal. However the variable 'a' is still an Animal and therefore only Animal methods can be called on it.
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
i just deleted my post but here i am again
you can allways do this:


java amateur
Alan Jump
Greenhorn

Joined: May 25, 2005
Posts: 26
Originally posted by Steven Bell:
However the variable 'a' is still an Animal and therefore only Animal methods can be called on it.


Just to make sure I've got it straight in my head, the only way to get 'a' to refer to (and therefore make use of the methods in) a Dog() class would be to break the reference first, then rereference it as a Dog(), like this:


...or would casting it at declaration time work?


I admit, polymorphism still confuses me, even with the good books and tutorials available...
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

"Marzo"-
Welcome to the JavaRanch! Please adjust your displayed name to meet the

JavaRanch Naming Policy.

You can change it

here.

Thanks! and welcome to the JavaRanch!

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
I admit, polymorphism still confuses me

why dont you experiment your own sugestions?

did you run my last post?
Alan Jump
Greenhorn

Joined: May 25, 2005
Posts: 26
Originally posted by miguel lisboa:

why dont you experiment your own sugestions?

did you run my last post?


If I were at my own machine I could do just that. Sadly, I'm not at a machine I can drop a JDK onto, so I have to try and process all these good ideas and suggestions in my grayware (which may be what's confusing me).
Steven Bell
Ranch Hand

Joined: Dec 29, 2004
Posts: 1071


This is a compile time error. As I said the variable 'a' is declared as type Animal and will from then on always be of type Animal. If the Object referenced by 'a' is of type Dog the only way to call Dog specific methods on it would be
Marcelo Ortega
Ranch Hand

Joined: May 31, 2005
Posts: 519

So when you do this:

Animal a = new Dog();

'a' can access ALL of the members in Animal (if access modifiers all it)because of the IS-A relationship, and it can ONLY access the METHODS (not instance variables) of the Dog class which override the methods inherited from the Animal class? So nothing else can be accessed in the Dog class?

Is this correct, or is there actually more to it?
Alan Jump
Greenhorn

Joined: May 25, 2005
Posts: 26
Originally posted by Steven Bell:
[QBAs I said the variable 'a' is declared as type Animal and will from then on always be of type Animal. [/QB]


*cue sound effect: light switch*

Got it now...many thanks.
Steven Bell
Ranch Hand

Joined: Dec 29, 2004
Posts: 1071
Originally posted by Marzo O.C:
So when you do this:

Animal a = new Dog();

'a' can access ALL of the members in Animal (if access modifiers all it)because of the IS-A relationship, and it can ONLY access the METHODS (not instance variables) of the Dog class which override the methods inherited from the Animal class? So nothing else can be accessed in the Dog class?

Is this correct, or is there actually more to it?


I think you pretty much have it, let me rephrase it and see if we are on the same page.

Compile Time:
The compiler knows that the variable 'a' refers to an Animal Object. It doesn't know or care if the actual Object at runtime is an Animal or some subclass of Animal. Therefore during compile time you can only call things visible on the Animal class with the 'a' reference.

RunTime: (may be a bit of simplification, but the basic point)
During runtime the variable 'a' is a reference (pointing to) and actual Object on the JVM heap. When 'a.someMethod()' is called the JVM looks up the actual Object that 'a' references. It looks at that Object to see if it has a method signature that matches the method being called (someMethod() in this case). If a matching method is not found it gets the superclass of that Object (when ever you create an Object all it's superclasses are also created, more or less) and looks for the method there. It will keep going up the chain until it finds the method.
Marcelo Ortega
Ranch Hand

Joined: May 31, 2005
Posts: 519

Ahhh... i see.

So thats why they call it Dynamic Binding, cos at runtime the JVM looks the method up.? Correct?

So at compile time you can only invoke methods wich the Animal class defines.
Steven Bell
Ranch Hand

Joined: Dec 29, 2004
Posts: 1071
Yep, that's it.
 
jQuery in Action, 2nd edition
 
subject: Polymorphism
 
Similar Threads
Creating Objects
polymorphism, overriding - confusion ?
Upcasting of Classes Problem
Confused with instanceof, please help
Why can only "real" dog bark?