• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Rob Spoor
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Carey Brown
Bartenders:

About multiple inheritance in Java

 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

Is there a way in Java where we can achieve multiple inheritance, for example

ClassA has a method aMethodA();
ClassB has a method aMethodB();

I want ClassC to inherit these two methods aMethodA() as well as aMethodB(). Is there a way we can do that except with interfaces, because we cannot provide default behaviour for interface methods.

Thanks & Regards,
Ganesh.
 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I am finding two ways to do that which I think is right.
1. Inherit ClassB from ClassA and ClassC from class ClassB. Take the access modifier that suits you.(is-A at two levels)
2. Have two instances of ClassB and ClassA in ClassC.Take the access modifier that suits you. (has-A).

The 2nd option is usually used at places where you don't want/have multiple inheritance.
 
Ranch Hand
Posts: 584
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ganesh,

Definitely, multiple inheritance is not allowed in Java.

An work around would be using interfaces.

What you mean when you say : "We cannot provide default behavior for interface methods" ?

Once you declared a concrete class that implements an interface, you MUST provide default behavior for all methods declared by interface.
 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Ganesh Prakhya ,

one way to achieve effect of multiple inheritance is

create class A extends CLASSA {
class IN extends classb
{

}
}
now create object of class class IN it is capable to access method of class A which is extended from CLASSA so indirectly method of CLASSA

and as class IN is extended from classb you can use method of classb

i.e. method from two different classes

ok bywee............
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

It has been a long time since I took this test. Getting to the point, but why do you want to have multiple inheritence. The concept of OO will be destroyed if you have multiple inheritance. It is like 2 objects like a dog and an orange. You can't inherit 2 objects into one object. It is either a dog or an orange. So multiple inheritence won't work.

Thanks,
Ron
 
Sheriff
Posts: 3061
12
Mac IntelliJ IDE Python VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
While I agree that Java disallows multiple inheritance, I don't agree that it would destroy the concept of OO. Yes, I could live a happy life without seeing a dog/orange hybrid, but I think camera cell phones are sort of cool. Some can even play MP3s. So there are real-life examples of objects with multiple inheritance.

However, it dirties up a language. If both "Phone" and "Camera" have a method with the same name, which one do you use for CameraPhone? What if they have different return types? It's a mess Java chooses to avoid entirely. You can make a CameraPhone that contains Phone and Camera instances instead.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To expand on what amod said above...

One interesting consideration is that inner classes can inherit independently from their enclosing class. So considering the special "link" an inner class has to its enclosing class, this might be considered a form of multiple inheritance...
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is one more elegant way of implementing MI in Java through AOP
http://www.custommode.com/space/Multiple+Inheritance+in+Java
reply
    Bookmark Topic Watch Topic
  • New Topic