Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Liutauras Vilda
  • Knute Snortum
  • Bear Bibeault
Sheriffs:
  • Devaka Cooray
  • Jeanne Boyarsky
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • salvin francis
Bartenders:
  • Tim Holloway
  • Piet Souris
  • Frits Walraven

has-a and is-a (inheritance)

 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can someone explain in detail what has-a and is-a in inheritance is?
I am trying add to a class some functionality from another class and a co-worker said to me,

There is a difference between a class that HAS and one that IS.

 
Sheriff
Posts: 3035
12
Mac IntelliJ IDE Python VI Editor Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"is a" and "has a" distinguish inheritance from delegation. For example, let's say you want a Vector that only allows objects of type Animal to be added to it. You have a choice of extendig java.util.Vector and overriding the methods to make that restriction. That's "is a". On the other hand, you could make a class that contained a Vector as a member variable. Your methods could mimic the interface of Vector, and delegate the work to the Vector member. That's "has a". Each approach has pros and cons. The GOF Patterns book is a good reference for this issue.
 
Ryan Tracy
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anyone have another example or explanation?
 
Sheriff
Posts: 9099
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A Car "is-a" Vehicle.
A Vehicle "has-a" Motor.
A Car "is-a" Vehicle.
A Car "has-a" Motor.
A Bicycle "is-a" Vehicle.
A Bicycle does not have a Motor.
 
Ranch Hand
Posts: 2676
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is the code that someone showed me to explain it.

All vehicles have a motor so the vehicle has a member variable of type motor. A Car is a specialized type of vehicle because it inherits from vehicle. Since the Motor in Vehicle is protected Car can access this. A Car also has a Trunk and a Tire.
 
Greg Charles
Sheriff
Posts: 3035
12
Mac IntelliJ IDE Python VI Editor Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ouch!
 
Matthew Phillips
Ranch Hand
Posts: 2676
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Greg Charles:
Ouch!


???
 
You guys haven't done this much, have ya? I suggest you study this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!