Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Inheritance in JAVA

Status
Not open for further replies.

munnanext

Programmer
Aug 20, 2004
49
0
0
US
I have a questions related to Inheritance. Let's say I have a ClassA, ClassB and ClassC. ClassC is inherited from Class B. So I have access to the Members of the ClassB so I can reuse them in ClassC. Let's say I have some members and methods that are existing in ClassA and I want them to re-use in ClassC (Basically I want to re-use from ClassA and Class B in ClassC) . I very well know JAVA does not allow multiple Inheritance. Can any body explain me how I can Achieve this in JAVA.

Thanks in Advance.



 
Class B will have to inherit from Class A in order for Class C to see it.

Don't want A's functionality visible in B? Sorry, with single inheritance, there is a limit to what can be done.

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Yes I do not want ClassB inherit from ClassA. If this is the case yes what you stated is right. But with Inheriting ClassB from Class A , I want to Re-Use ClassA in Classc. I read some where that we can achieve this thru Interface but I did not understand how we achieve this .

Thanks
munnanext
 
It's hard to make a suggestion from your abstract description.

In most cases you don't use inheritance at all, but composition.

Assume a class Printer, and a class UsbDevice.
Now you want to model a UsbPrinter.
Code:
class UsbPrinter extends Printer
{
    private UsbDevice usbdevice;

Tell us more about the concrete situation, and maybe we can tell you, whether extending one class is appropriate, and which, or whether interfaces are a good solution.

seeking a job as java-programmer in Berlin:
 
Stefan -
Thanks, I was going to mention the design issue.

munnanext -
If you have an inheritance tree that tall, you've got a design problem. You generally just don't do that without a very good reason.

Interfaces would not help in your situation, because you said: "Let's say I have some members and methods that are existing in ClassA". This is saying to me that you already have code in the methods, which then that rules out using an interface. Interfaces are a contract, meaning that it requires any class implementing that interface to have all the methods that the interface defines. The code would exist in the implementing class(es), and not in the interface itself. The interface itself consists of just method stubs -- the implementor provides the, uhhh, implementation.

In other words, an Interface guarantees that a certain set of methods will be available to callers. So if you're calling a class that implements the interface, you can blissfully assume that the method exists and just make your call.

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
But an interface may help although:

Code:
interface Ia {void fa ();}
class A implements Ia {void fa (){/*something*/};}
class B {void fb (){/*something*/};}
class C extends B implements Ia
{
   A a;
   void fa (){a.fa ();};
   /*something*/
}
Now if all needed methods in A are defined in the interface, you can use C as a kind of Ia - not exactly as A, but perhaps you might replace every usage of A wiht a usage of Ia.

seeking a job as java-programmer in Berlin:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top