Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

polymorphism  RSS feed

 
Sean Mack
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I'm studying Java using "Headfirst Java".
A quick question for someone who may be able to help. In polymorphism we are told that a subclass inherits all the variables and methods of the superclass (unless they are private).

so if class Dog extends Animal then we can say:

Animal myDog = new Dog();

or
Animal[] animals = new Animal[5];
animals[0] = new Dog();

Then later we are told that everything is a subclass of type Object, so it is equally valid to say:

ArrayList<Object> myDogArrayList = new ArrayList<Object>();
Dog aDog = new Dog();
myDogArrayList.add(aDog);

i.e. put the Dog object into an Arraylist of type Object. But then we are warned that the Object reference variable won't reference the Dog object but can only reference the Object object that its wrapped around. So if you had new methods for the object of the Dog class only, you can't access them until you use a cast.

So far so good but my question is, does this also apply to classes that you have defined yourself, i.e. If you put a Dog object into an ArrayList of type Animal does the Animal reference variable recognise the methods specific to the Dog object? (Hope this makes sense) Or do you have to use a cast and if not why not?

Also does polymorphism still work if you add brand new methods for your subclass (i.e. not Overridden or Overloaded)?

Thanks for reading, hope someone can help.

Sean.
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ranchers,

Sean Mack was asking:
So far so good but my question is, does this also apply to classes that you have defined yourself, i.e. If you put a Dog object into an ArrayList of type Animal does the Animal reference variable recognise the methods specific to the Dog object?


If you have an ArrayList of type <Animal>, you can store a Dog into it, because a dog IS-An Animal.
But what you get out of the list will always be an Animal, and you cannot invoke Dog-specific behaviour unless you
1. have the reference variable of type Dog and
2. cast to a dog.

It is just the same as if you said:
Animal dogAnimal = new Dog();

You cannot invoke Dog-methods on this dogAnimal, because the compiler knows only about the methods, that are in class Animal for this dogAnimal.
However, if Animal methods are overriden in the Dog subclass, the method invoked will be that of the object-type, of Dog in this example:


prints out:
woof!
woof!
miaw!



and:
Also does polymorphism still work if you add brand new methods for your subclass (i.e. not Overridden or Overloaded)?

Yes, see above.


Generic classes are in a way lacking some of the polymorphism, that non-generic classes have.
For example:

won't compile

while
works.

But also in the latter example: What you get out of this array will always be of an Animal reference type and of object type Dog.

Putting an Animal into this array
mixedArray [0] = new Animal();
would cause an ArrayStoreException, but no compile error.



And yes: this IS complicated...

[ October 14, 2006: Message edited by: Burkhard Hassel ]
 
Sean Mack
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for that
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!