• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Some Questions From Head First Java

 
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 1844
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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...
 
Ranch Hand
Posts: 490
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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).
 
Joel McNary
Bartender
Posts: 1844
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Always look on the bright side of life. At least this ad is really tiny:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic