This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Polymorphism and Generics - ideas taken from SCJP 6 by K and B

 
Ted North
Ranch Hand
Posts: 201
1
Java Python
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
With the following source code:


Why does the public void addAnimal(List<? super Dog> animals) method, line 29, take a Cat object and also add a Cat object when it only takes super types of Dog or Dog objects. Is this because Cat IS-A Dog which in turn IS-A animal - therefore Cat IS-A animal which is a super type of Dog?

Respectfully,

TN - powerhouse of industry and IT
 
dennis deems
Ranch Hand
Posts: 808
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, that's basically it. The IS-A relationship goes all the way up the inheritance tree.
 
Helen Ma
Ranch Hand
Posts: 451
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. In this example, you can add Dog or subtype to the animals list. The animals list may refer to a list of Dog/Animal/Object. But you cannot add Dog's super type: Animal or Object.
For example, you have Animal pet = new Dog();
Can animals add pet? No.
The compiler prevents programmers from adding Animals to animals list.
What happens if pet is changed into pet = new Horse()? Horse is not in this chain Dog-> Animal-> Object. Horse has no relationship with Dog. If a horse is added to the animals list, it will not be type safe.
 
Ted North
Ranch Hand
Posts: 201
1
Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Dennis Deems wrote: Yes, that's basically it. The IS-A relationship goes all the way up the inheritance tree.


Awesome. Got it! Thank-you Dennis.


Respectfully,

TN
 
Greg Charles
Sheriff
Posts: 2989
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can also check out the Java Generics Tutorial if you haven't already. In particular, the page More Fun with Wildcards. ("Fun" here is clearly meant sarcastically.) Fortunately bounded wildcards are an advanced topic in Java generics, and one you don't need to use very often. If you do, then a little trial and error, or a quick post to JavaRanch is usually enough to get something working.

By the way, I'm not sure if you created the code you posted, but I've found it useful when abstracting real-life concepts into code, to never violate real-life logic. Make Poodle extend Dog, for example, not Cat. That way, when you're mired deep in the abstraction, trying to figure out what's what, you don't have the extra distraction of trying to remember if a Cat is a Dog in your world.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Helen Ma wrote:Yes. In this example, you can add Dog or subtype to the animals list

you are talking about this animals.add(new Cat()); ? if yes actually you can add only Dog or it's subtype only.
 
Ted North
Ranch Hand
Posts: 201
1
Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Helen Ma wrote:Yes. In this example, you can add Dog or subtype to the animals list. The animals list may refer to a list of Dog/Animal/Object. But you cannot add Dog's super type: Animal or Object.
For example, you have Animal pet = new Dog();
Can animals add pet? No.
The compiler prevents programmers from adding Animals to animals list.
What happens if pet is changed into pet = new Horse()? Horse is not in this chain Dog-> Animal-> Object. Horse has no relationship with Dog. If a horse is added to the animals list, it will not be type safe.


Thank-you Helen, at first this did not make any sense to me, but now it does. I added a new class that subclasses Cat and tested it out and your logic holds true - that adding super-types of Dog is not allowed but adding subclasses of Dog is okay.

Greg Charles wrote:
By the way, I'm not sure if you created the code you posted, but I've found it useful when abstracting real-life concepts into code, to never violate real-life logic. Make Poodle extend Dog, for example, not Cat. That way, when you're mired deep in the abstraction, trying to figure out what's what, you don't have the extra distraction of trying to remember if a Cat is a Dog in your world.

Yes, I agree that the world in my program does not make much sense. I was just trying to come up with a quick and dirty solution to test polymorphism with generics.

Respectfully,

TN - SETI institute champion - CPU juggernaut

 
Bert Bates
author
Sheriff
Posts: 8900
5
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Slightly off topic, but in a nutshell...

What Greg said!!!

This is a great example of the underlying principle beneath almost all good teaching, e.g.

reduce cognitive load

Wanna be a great teacher?

reduce cognitive load

Wanna write a great technical book?

reduce cognitive load

It's reducing cognitive load, all the way down
 
Ted North
Ranch Hand
Posts: 201
1
Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for the feedback Bert. It is always an honor.

I am going to have to remember to 'reduce cognitive load' in order to master these concepts for the OCPJP.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic