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 ]
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
Joined: Oct 13, 2005
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.
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 :
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.?