This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Polymorphism and Generics - ideas taken from SCJP 6 by K and B Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Polymorphism and Generics - ideas taken from SCJP 6 by K and B" Watch "Polymorphism and Generics - ideas taken from SCJP 6 by K and B" New topic
Author

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

Ted North
Ranch Hand

Joined: Jan 02, 2012
Posts: 193
    
    1

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

Joined: Mar 12, 2011
Posts: 808
Yes, that's basically it. The IS-A relationship goes all the way up the inheritance tree.
Helen Ma
Ranch Hand

Joined: Nov 01, 2011
Posts: 451
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

Joined: Jan 02, 2012
Posts: 193
    
    1


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

Joined: Oct 01, 2001
Posts: 2854
    
  11

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

Joined: Jan 28, 2008
Posts: 5575

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

Joined: Jan 02, 2012
Posts: 193
    
    1

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

Joined: Oct 14, 2002
Posts: 8875
    
    5
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


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
Ted North
Ranch Hand

Joined: Jan 02, 2012
Posts: 193
    
    1

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.

 
jQuery in Action, 2nd edition
 
subject: Polymorphism and Generics - ideas taken from SCJP 6 by K and B