• 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

"is a" and "has a"

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Who can tell me the differentiation between "is a" and "has a" relation? The detailed, the better.Thanks a lot.
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
assume we have
class B{}
for relation "class A is a class B",
u can write your code as:
class A extends B{
//
//
}
if relation is "class A has a class B"
you can code as,
class A{
B b;
//
}
 
Ranch Hand
Posts: 229
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Bin,
Hi! the diffrence is pretty simple...
Just keep in mind this example.....CAR HAS FOUR WHEELS AND CAR IS A VEHICLE..
Now coming to classes and objects.....let us take an example..
Class Vehicle{}
Class car extends Vehicle
{
String Wheels;
}
just remember where ever u see "extends" that is "is a" relationship...here car extends Vehicle ..so car "is a" Vehicle and car "has" String Wheels as an attribute....so has a relationship........
Hope I am clear,
Harpal
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
you can find better explanation at http://www.anilbachi.8m.com I have gone thru that material but couldn't recollect the url for the tuts
bye.
 
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for these updates.
But what is the relationship of Vehicle with respect to car.
Mahesh
 
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It has to do with classifications and helps to determine the ordering of classes and subclasses. If you are defining these from the start, you list some knowns about them to determine which class should be the super class.
A Vechile is used for transportation.
All Cars are Vehicles.
Not all Vehicles are Cars. (You have Buses, Bikes, Motorcycles etc.)
With these facts, you can see that Vehicle should be the superclass and Car would be the subclass of Vehicle.
Hope this helps
 
Harpal Singh
Ranch Hand
Posts: 229
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
WoW....Carl..No wonder you are a bartender..good explanation...
Thanks,
Harpal
 
Ranch Hand
Posts: 3143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well the relationship of Vehicle to Car ... now you are talking about Inheritance.
If they were java classes Vehicle would be the parent class and car it's child class.
Vehicle may have many attributes that are common to all vehicles such as NumberOfWheels, NumberOfSeats, Speed etc. When Car extends Vehicle it inherits those attributes (and any methods in the Vehicle class) then it might have some of it's own which are more specific to a car such as MilesPerGallon. Other Classes such as Bus, Van and Truck might also extend (be child Classes of) Vehicle and also inherit NumberOfWheels etc.. but they would not have MilesPerGallon unless it was specifically declared.
Here's a outline of what this may look like .... Note this code doesn't actually do anything!

Hope this helps
 
Harpal Singh
Ranch Hand
Posts: 229
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Angela,
Cooool example.....very nicely explained...Even my fundamentals got more clear....keep up the good work sheriff
Harpal
 
Mahesh Bansal
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the excellent explaintions. But my problem is that in the exam if some asks what is the relation of vehicle with respect to car then what should I mention.
Kindly explain.
Thanks
Mahesh
 
Get me the mayor's office! I need to tell her about 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!