• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How Multiple Inheritance can be achieved in Java ?

 
shailesh sonavadekar
Ranch Hand
Posts: 1874
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How multiple inheritance can be acieved in Java ? What are the ways to achieve this ?
 
Velmurugan Periasamy
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can achieve multiple inheritance using interfaces. A class can extend from only one class, but can implement multiple interfaces.
HTH
------------------
Velmurugan Periasamy
Sun Certified Java Programmer
----------------------
Study notes for Sun Java Certification
http://www.geocities.com/velmurugan_p/
 
shailesh sonavadekar
Ranch Hand
Posts: 1874
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
With inheritance , you inherit layout , behaviour & interface. If I have to inherit interfaces , then what I should do ?
I want only layout & behaviour , what is to be done ? if I want all three , what I should do ?
 
Avijeet Dash
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by shailesh sonavadekar:
With inheritance , you inherit layout , behaviour & interface. If I have to inherit interfaces , then what I should do ?
I want only layout & behaviour , what is to be done ? if I want all three , what I should do ?

looks like u have the question as well as the answer. could u pl. explain what r u trying to say.
 
shailesh sonavadekar
Ranch Hand
Posts: 1874
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just want to ask how I can Achieve Multiple Inheritance in java? Simple.
 
Frank Carver
Sheriff
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The question seems simple, but the answer depends on which of the benefits and disadvantages lumped togather as "multiple inheritance" you need to use.
If you just need to be able to refer to an object as if it is one of its "base classes" the interfaces is the obvious answer. If you need to inherit data items then use of the Decorator pattern to encapsulate an instance of each of the "base" classes, and add accessors which pass through to the local instances.
This may seem complex, but it does mean that you remain in control of the tricy sematics of multiple inheritance, particularly the "shared base class" problem.
Do you need anything else?
 
John Wetherbie
Rancher
Posts: 1449
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey,
I have done this by using "implementation classes" for lack of a better term. I was about to launch into my best attempt to explain this but there is a better way. Robert Martin wrote this article a few years ago. It compares C++ to Java and the first section is on MI. The part of this section that deals with Java describes what I call an implementation class.
BTW, I don't agree with Bob's assessment of MI in Java at the end of the section.
John
[This message has been edited by John Wetherbie (edited February 02, 2001).]
 
Michael Ernest
High Plains Drifter
Sheriff
Posts: 7292
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the obvious answer is the best: Java does not support true multiple inheritance in any way, shape or form. You cannot trick Java into doing so. It's deliberately excluded from the design. Period.
Composition and encapsulation can approximate the appearance of multiple inheritance, but that's about it.
------------------
Michael Ernest, co-author of: The Complete Java 2 Certification Study Guide
 
John Wetherbie
Rancher
Posts: 1449
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think it depends on how you depend you define multiple inheritance. Most people use this to mean multiple inheritance of implementation. I tend to think in terms of inheritance of interface and inheritance of implementation. So I usually answer "yes and no" and explain why if someone asks me if Java supports MI. I will agree that you can't do MI in Java like you can in C++ but I think for situations where you really need MI (or MI behavior) I prefer the way you have to do it in Java.
John
 
Laojar Chuger
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by shailesh sonavadekar:
With inheritance , you inherit layout , behaviour & interface. If I have to inherit interfaces , then what I should do ?
I want only layout & behaviour , what is to be done ? if I want all three , what I should do ?

What do you mean by "layout"?
 
Michael Ernest
High Plains Drifter
Sheriff
Posts: 7292
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java simply doesn't support multiple inheritance of implementations. It supports implementation of interfaces, of course, and also inheritance of interfaces. But the latter case only applies if you're writing another interface.
If there's a benefit to a faceted answer in this case, I'm not seeing it. It sure sounds like the topic author wants to know how to extend functional code from multiple parent classes. We can save him a lot of time with "can't be done.".
------------------
Michael Ernest, co-author of: The Complete Java 2 Certification Study Guide
 
Pete Cassetta
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't agree with the "MI can't be done in Java" of some replies. If you want to get something functionally similar to the MI of C++, you need to work with inner classes in Java. Just make an outer class which has a series of inner classes that extend each of the base classes you want the behavior/attributes of. Your outer class would take care of providing a clean interface between the outer world and the inner classes. You can thereby write an (outer) class that does pretty much anything you would use MI for in C++, but the inner class solution gets around the messy problems MI causes in C++. I think you end up with a cleaner and nicer design in Java.
By the way, there was a post about the decorator pattern which I think was describing the same thing I've described here. But I thought the term "inner class" might help turn on the lightbulb...
 
Guennadiy VANIN
Ranch Hand
Posts: 898
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a few beginner questions to this topic:
Originally posted by shailesh sonavadekar:
With inheritance , you inherit layout , behaviour & interface. If I have to inherit interfaces , then what I should do ?
...
I want only layout & behaviour , what is to be done ? if I want all three , what I should do ?

What is "layout" of class?
Originally posted by Michael Ernest:
Composition and encapsulation can approximate the appearance of multiple inheritance, but that's about it.

About what? Why, what for need we encapsulation for realisation of inheritance, well, MI?
Originally posted by Pete Casseta:
Just make an outer class which has a series of inner classes that extend each of the base classes you want the behavior/attributes of.

I could not understand why inner classes? The same may be achieved by inclusion of MULTIPLE objects/references to external classes... What specifically inner (classes) and encapsulation bring to this problem?
 
Guennadiy VANIN
Ranch Hand
Posts: 898
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Update
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic