• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Query on Inheritance

 
Deepak Lal
Ranch Hand
Posts: 561
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,



1> what are the bottlenecks of using Inheritance Concept in Java?

2> When do i need to use Interfaces against Abstract classes.Are interfaces really helpful against Abstract Classes in Java ?



Help provided will be highly appreciated.


--
Deepak Lal
 
kranthi chamarthi
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When do i need to use Interfaces against Abstract classes.Are interfaces really helpful against Abstract Classes in Java ?

Interface consists of only methods signatures and declarations of static final variables where as Abstract classes can contain both methods with implementation and methods without implementation or only methods with implementation. Interfaces will come in handy when you want to have multiple inheritance in java.... i.e a class can implement multiple interfaces but can extend only one class.
Suppose You have a more generalized entity like Vehicle and more specific entities like Car, Byke, Truck and so on.... If you declare Vehicle as Abstract class and make the Car, Byke, Truck extend Vehicle then you can't make Car, Byke, Truck extend any other classes.
But if you make Vehicle as an interface you can make the sub entities implement Vehicle as well as any other interfaces. required,they can also extend any other class.
[ November 26, 2008: Message edited by: kranthi kumar.chamarthi ]
 
Ravikanth kolli
Ranch Hand
Posts: 179
Chrome Eclipse IDE Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
posts in multiple forums here
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ravikanth kolli:
posts in multiple forums here


Thank you for noticing.

Deepak Lal: please tell people on both fora if you post twice: look at this FAQ.
 
Deepak Lal
Ranch Hand
Posts: 561
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry Campbell,
what about the below query



1> what are the bottlenecks of using Inheritance Concept in Java?



I'm afraid i could not find any answers for the same.Please help me !!!

Sorry for cross posting on other websites.

--
Deepak Lal
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Apology accepted . Please also tell them on the other forum you are discussing the same question here.

Not sure I quite understand what you mean by bottleneck, but . . .

As mentioned on your other thread, in Java (and C#, but not in other languages, eg Eiffel), you can only extend one class at a time. So Java doesn't implement diamond inheritance. You have already been told that you can implement several interfaces, so you could have a diagram like thiswhere I1 I2 I3 are interfaces, X is an abstract class and C1 C2 C3 are concrete classes, which are all in an IS-A relationship to X, I1 I2 and I3.

It is often easier to use composition rather than inheritance; have an abstract Vehicle class, Car, Van, Bus classes. But don't have DieselEnginedCar, DieselEnginedVan classes. Let each class have an Engine field so it can have a DieselEngine, PetrolEngine, TwoStrokeEngine, etc.

Google for the Liskov Substitution Principle (LSP), and you find all sorts of interesting things. This link to a pdf explains the LSP and shows how it might break down when faced with Square extends Rectangle.
 
Deepak Lal
Ranch Hand
Posts: 561
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell wrote:
It is often easier to use composition rather than inheritance; have an abstract Vehicle class, Car, Van, Bus classes. But don't have DieselEnginedCar, DieselEnginedVan classes. Let each class have an Engine field so it can have a DieselEngine, PetrolEngine, TwoStrokeEngine, etc.



My Comments :

Well Explained.



Thanks Campbell,



My next question is how do i go about implementing the DieselEngineCar and DieselEngineVan principle in java. ??



how do my classes look like,how does my Superclass look like. ??
How is composition supported in this case.??

If you can explain me with AN EXAMPLE for Composition ,It would be highly appreciated.

I will be highly obliged if you could throw some light on this Composition principle in java through an Example.?



--
Deepak Lal
 
Deepak Lal
Ranch Hand
Posts: 561
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you explain me the "above concept of Composition" with an example.?

--
Deepak Lal
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes
 
Deepak Lal
Ranch Hand
Posts: 561
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Campbell Ritchie,
The example which you have provided is really a good one,Understood Composition in a better way.



--
Deepak Lal
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic