aspose file tools*
The moose likes Beginning Java and the fly likes Query on Inheritance Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Query on Inheritance" Watch "Query on Inheritance" New topic
Author

Query on Inheritance

Deepak Lal
Ranch Hand

Joined: Jul 01, 2008
Posts: 507

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


When The Going Gets Tougher,The Tougher gets Going
kranthi chamarthi
Ranch Hand

Joined: May 08, 2007
Posts: 95
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

Joined: Feb 10, 2008
Posts: 179

posts in multiple forums here


-kolli
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39409
    
  28
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

Joined: Jul 01, 2008
Posts: 507

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

Joined: Oct 13, 2005
Posts: 39409
    
  28
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

Joined: Jul 01, 2008
Posts: 507


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

Joined: Jul 01, 2008
Posts: 507

Can you explain me the "above concept of Composition" with an example.?

--
Deepak Lal
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39409
    
  28
Yes
Deepak Lal
Ranch Hand

Joined: Jul 01, 2008
Posts: 507

Thanks Campbell Ritchie,
The example which you have provided is really a good one,Understood Composition in a better way.



--
Deepak Lal
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Query on Inheritance