File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Some Questions From Head First Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Some Questions From Head First Java" Watch "Some Questions From Head First Java" New topic
Author

Some Questions From Head First Java

Michael Hubele
Ranch Hand

Joined: Dec 17, 2005
Posts: 182
I am doing chapter 7 from the head first Java book and I doing a sharpen your pencil question(179)

Instructions:

Put a check next to the relationships that make sense: I am using the is-a and then I going one step further by trying if it could be a has a.

Oven extends Kitchen - Oven is a Kitchen - does not work if it was Kitchen has a oven then that would mean oven is a instance variable of Kitchen?

Guitar extends instrument - Guitar is a instrument - makes sense so Guitar can inherate from instrument.

Person extends Employee - Person is a Employee - not sure about this one is sounds like it makes sense but not sure though but I guess sicne not every person is a employee it would not work.

Ferrari extends Engine - Ferrari is a Engine - does not make sense but ferrair has a Engine so Ferrari would be a instance variable of Engine?

FriedEgg extends Food - FriedEgg is a Food - makes sense so FriedEggy inherates from Food

Beagle extends Pet - Beagle is a pet - makes sense so Beagle inherates from Pet.

Container extends Jar - Container is a Jar - could make sense too since a Container could be a Jar.

Metal extends Titanium - metal is a Titanium - does not make sense so it does not work. Since not all metal is Titanium

GratefulDead extends Band - GratefulDead is a Band - it works

Blonde extends Smart - Blonde is a Smart - to me that does not make sense but according to the answers I found it works.

Beverage extends Martini - Beverage is a Martini -- this one to me could work too since martini could be classified as Beverage but then again not every beverage is a Martini.
Mark Van Tuyl
Ranch Hand

Joined: Mar 22, 2002
Posts: 60
I have a few comments:

"Person extends Employee - Person is a Employee - not sure about this one is sounds like it makes sense but not sure though but I guess sicne not every person is a employee it would not work."

It works the other way around - every Employee is a Person.


"Ferrari extends Engine - Ferrari is a Engine - does not make sense but ferrair has a Engine so Ferrari would be a instance variable of Engine?"

Ferrari has an Engine, so Ferrari would implement the Engine interface.


"Container extends Jar - Container is a Jar - could make sense too since a Container could be a Jar."

But not all Containers are Jars. The reverse works: Jar is a Container so Jar extends Container.


"Beverage extends Martini - Beverage is a Martini -- this one to me could work too since martini could be classified as Beverage but then again not every beverage is a Martini."

Again, it's reversed - Martini is a Beverage, so Martini extends Beverage.


<a href="http://www.catb.org/~esr/faqs/smart-questions.html" target="_blank" rel="nofollow">How To Ask Smart Questions</a>
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1821

I have a few comments:

"Ferrari extends Engine - Ferrari is a Engine - does not make sense but ferrair has a Engine so Ferrari would be a instance variable of Engine?"

Ferrari has an Engine, so Ferrari would implement the Engine interface.



Implementing an interface is a type of "is-a" relation, not "has-a." So an Engine would be an instance variable of a Ferrari.

Now, back to the OP---

When you extend/implement a superclass/interface, you are narrowing the possibilities. I like to think of it in terms of Linnean Taxonomy:



Note that each extension adds a level of specificality. We could go in and fill in more and more details at each level (attributes, like "spinalCord" at the "Chordata" level, and methods, like "giveBirthToLiveYoung()" at the "Mammalia" level), until finally we've gotten specific enough that we can accurately describe an ocelot.


Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8829
    
    5


Ouch! Cognitive overload

What the heck is a Pardalis?

Wouldn't it be easier on the brain to say:

class LivingThing
class Animal extends LivingThing
class Dog extends Animal
class Beagle extends Dog


Same idea but Pardalis' seem to distract from the actual learning topic...


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

Joined: Jan 03, 2006
Posts: 490
Implementing an interface is a type of "is-a" relation, not "has-a." So an Engine would be an instance variable of a Ferrari.


You could have Ferrari implement an Engine interface. Unless of course, there is an Engine class heirarchy.

When I see a has-a relationship I normally think interface(not always the interface keyword).


"Computer science is no more about computers than astronomy is about telescopes" - Edsger Dijkstra
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1821

But if I have a Truck, I can't install a Ferrari as it's Engine. (Of course, that's assuming that a Truck is composed of an Engine instead of itself being an implementation of an engine).

More importantly, If I have a Ferrari, I can change it's Engine. A Ferrari is composed of an Engine (and some other stuff). Composition is "has-a"



If I need a different Engine, I can do it because a Ferrari has an Engine. If a Ferrari is an engine and I need a new engine, then I need a whole new car because I can't replace the engine.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Some Questions From Head First Java