Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Polymorphism

 
Marcelo Ortega
Ranch Hand
Posts: 528
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Steven Bell
Ranch Hand
Posts: 1071
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i just deleted my post but here i am again
you can allways do this:
 
Alan Jump
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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
 
miguel lisboa
Ranch Hand
Posts: 1281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I admit, polymorphism still confuses me

why dont you experiment your own sugestions?

did you run my last post?
 
Alan Jump
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1071
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 528
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1071
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 528
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1071
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yep, that's it.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic