aspose file tools*
The moose likes OO, Patterns, UML and Refactoring and the fly likes How Multiple Inheritance can be achieved in Java ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "How Multiple Inheritance can be achieved in Java ?" Watch "How Multiple Inheritance can be achieved in Java ?" New topic
Author

How Multiple Inheritance can be achieved in Java ?

shailesh sonavadekar
Ranch Hand

Joined: Oct 12, 2000
Posts: 1874
How multiple inheritance can be acieved in Java ? What are the ways to achieve this ?
Velmurugan Periasamy
Ranch Hand

Joined: Nov 09, 2000
Posts: 95
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/


------------------------------------------------------------------------------------<BR>Velmurugan Periasamy<BR>Sun Certified Java Programmer for Java 2 Platform<BR>Sun Certified Web Component Developer for J2EE platform<BR>Sybase Certified EAServer Developer (similar to jCert level 3)<BR>------------------------------------------------------------------------------------<BR>Study notes for Sun Java Certification<BR><A HREF="http://www.geocities.com/velmurugan_p/" TARGET=_blank rel="nofollow">http://www.geocities.com/velmurugan_p/</A><BR>------------------------------------------------------------------------------------
shailesh sonavadekar
Ranch Hand

Joined: Oct 12, 2000
Posts: 1874
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

Joined: Jan 21, 2001
Posts: 148
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

Joined: Oct 12, 2000
Posts: 1874
I just want to ask how I can Achieve Multiple Inheritance in java? Simple.
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
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?


Read about me at frankcarver.me ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
John Wetherbie
Rancher

Joined: Apr 05, 2000
Posts: 1449
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).]


The only reason for time is so that everything doesn't happen all at once.
- Buckaroo Banzai
Michael Ernest
High Plains Drifter
Sheriff

Joined: Oct 25, 2000
Posts: 7292

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


Make visible what, without you, might perhaps never have been seen.
- Robert Bresson
John Wetherbie
Rancher

Joined: Apr 05, 2000
Posts: 1449
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

Joined: Dec 20, 2000
Posts: 111
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

Joined: Oct 25, 2000
Posts: 7292

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

Joined: Feb 13, 2001
Posts: 23
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

Joined: Aug 30, 2001
Posts: 898
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

Joined: Aug 30, 2001
Posts: 898
Update
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How Multiple Inheritance can be achieved in Java ?