Sign in to follow this  
Syed Atif Akhtar

Multiple inheritance in java.....??

Recommended Posts

I missed a few of my java classes and had to give a test yesterday....even though im pretty comfortable with programming the following question stumped me... We were asked to create a class called customer and then from it derive to classes.....savings_account and a recurring_account with their own deposit and retrieve functions....and from these two classes derive a single class called Account.Now i know java does not support multiple inheritance in any forms but this was to be done using interfaces.....googling did not help...can anyone give a basic idea as to how to go about the problem using simple interfaces?

Share this post


Link to post
Share on other sites
DevFred    840
The term "inheritance" is overloaded ;-)

The extends keyword means inheritance of interface and implementation.

The implements keyword means inheritance of interface only.

Google for java interface or something, this is one of the first results.

Share this post


Link to post
Share on other sites
Stani R    1070
Quote:
Original post by Syed Atif Akhtar
Yes but how........i still dont get it......???


What you described is not possible using classes in Java, as you are well aware, but with interfaces you can of course mix in as many as you want (use the implements keyword). Also, an interface can extend another interface.

Although to be honest, the example you describe sounds more like a case for composition, not inheritance. It depends on what they wanted from you, and I will go out on a leg here and say that you might have gotten the problem text mixed up a bit (SavingsAccount deriving from Customer makes very little sense to me). A basic structure that I can see in this scenario could be a class called Customer which includes a list of Account members, where Account is an interface and SavingsAccount and RecurringAccount are concrete implementations of that interface. There is no multiple inheritance in that structure, though if you needed to you could always add it (class A extends B implements C, D, E, F, G {})

Share this post


Link to post
Share on other sites
smc    292
You can implement pseudo multiple inheritance using composition. Usually you will want to take a look at the design and see if you can avoid this. However, it works for a hack, and in some instances my be your only choice.

From you description of the project it sounds like you will need to use interfaces.

Share this post


Link to post
Share on other sites
Quote:
Original post by lightbringer

A basic structure that I can see in this scenario could be a class called Customer which includes a list of Account members, where Account is an interface and SavingsAccount and RecurringAccount are concrete implementations of that interface. There is no multiple inheritance in that structure, though if you needed to you could always add it (class A extends B implements C, D, E, F, G {})


Thats what i did.........but my teacher says its not the way he wanted it....it turns out i misinterpreted the problem a little bit..........the solution to the problem was that we were supposed to make reccuringaccount and savingsaccount as interfaces with different retrieval and deposit functions,and Account should have implemented both interfaces and extended the customer class..............another way was composition although that didnt require any interfaces......

Share this post


Link to post
Share on other sites
Stani R    1070
That doesn't make much sense to me. Either I am missing something or your instructor has some strange notions about programming ^_^;;; There must be a less contrived example to demonstrate usage of interfaces in Java... something like Actor extends Entity implements CanWalk, CanSwim.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this