Geeks Talk

Prepare for your Next Interview




multiple inheritance in java...

This is a discussion on multiple inheritance in java... within the Java forums, part of the Software Development category; In java why we are not using multiple inheritance ? what is the problem ? ------------------ suresh...


Go Back   Geeks Talk > Software Development > Java

Register FAQ Members List Calendar Mark Forums Read

Reply

 

Thread Tools Display Modes
  #1 (permalink)  
Old 12-22-2006
Contributing Member
 
Join Date: Sep 2006
Location: bangalore, india
Posts: 1,007
Thanks: 0
Thanked 76 Times in 64 Posts
psuresh1982 will become famous soon enough
multiple inheritance in java...

In java why we are not using multiple inheritance ? what is the problem ?

------------------
suresh
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 01-01-2007
Junior Member
 
Join Date: Jan 2007
Location: delhi
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
manu_bans2710 is on a distinguished road
Re: multiple inheritance in java...

Quote:
Originally Posted by psuresh1982 View Post
In java why we are not using multiple inheritance ? what is the problem ?

------------------
suresh
hi,
u can understand by the following example y we don't have multiple inheritance..

let suppose we have two classes A & B both contain a variable i, now another class C that extends both the classes at a time want to call the variable "i" by one of its method now JVM is confused about which vaalue of "i" it should take from two which are at the same level thats why we don't have the multiple inheritance in java.
Reply With Quote
  #3 (permalink)  
Old 01-02-2007
Contributing Member
 
Join Date: Sep 2006
Location: bangalore, india
Posts: 1,007
Thanks: 0
Thanked 76 Times in 64 Posts
psuresh1982 will become famous soon enough
Re: multiple inheritance in java...

Ok manu...In C++ they handled multiple inheritance using virtual keyword. In java why they are not used the "virtual" keyword ? Why they completely remove the multiple inheritance concept?

------------------
suresh
Reply With Quote
  #4 (permalink)  
Old 01-02-2007
Expert Member
 
Join Date: Nov 2006
Location: Hyd-IND
Posts: 520
Thanks: 1
Thanked 53 Times in 45 Posts
sutnarcha is on a distinguished road
Re: multiple inheritance in java...

That is probably because of its low usage. I think it is hardly used though it is available in C++.

Best practice to name a variable is to have some part of it as the name of the class. It is being followed on a large scale. With that, there won't be a need to use the keyword 'virtual'. This solves the problem.

I am not sure, GUESSING.
__________________
Lack of WILL POWER has caused more failure than
lack of INTELLIGENCE or ABILITY.

-sutnarcha-
Reply With Quote
  #5 (permalink)  
Old 01-05-2007
Expert Member
 
Join Date: Oct 2005
Location: india,cochi,bangalore
Posts: 395
Thanks: 2
Thanked 8 Times in 7 Posts
vmshenoy is on a distinguished road
Re: multiple inheritance in java...

hi suresh

When new languages come, ofcourse they come with improvement. the programmers found that MULTIPLE INHERITANCE concept in c++ is of not much use.So they removed it from java language.By not allowing multiple inheritance java simpifies the INHERITANCE MODEL. Infact multiple inheritacne adds overhead to both the complier and runtime systems.


I would like to give an eg

cosider two hierarchies

Case 1) there is a subclass C and it inherits both class A and Class B at the SAME TIME.

Case 2) B inherits class A and C inherits Class B.
Both have same effects.

I hop that with this eg ur query will b solved.
Bye

So y to go with the first one.
__________________
:)
NEVER SAY DIE.
Reply With Quote
  #6 (permalink)  
Old 01-05-2007
Contributing Member
 
Join Date: Sep 2006
Location: bangalore, india
Posts: 1,007
Thanks: 0
Thanked 76 Times in 64 Posts
psuresh1982 will become famous soon enough
Re: multiple inheritance in java...

Shenoy i am not accepting the sentence.."MULTIPLE INHERITANCE concept in c++ is of not much use".

I would like kalayama give a reply for this one.....

--------------------
suresh
Reply With Quote
  #7 (permalink)  
Old 01-05-2007
Junior Member
 
Join Date: Jan 2007
Location: india
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
bikgeek is on a distinguished road
Cool Re: multiple inheritance in java...

In order to understand multiple inheritance, first you have to focus on the complexity , which is inherent in software system,and then you can think of why inheritance is there in java. First, java is a object oriented language in short a OOPL. The goal is reusability of software(code sharing)and of robust design. In C++ multiple inheritance is there which with bad design can increase the complexity of the code written in C++. In java that problem is removed but the advantages of the feature, that of multiple inheritance is kept through interface. Now , an abstract example can be given (which is well known)to illustrate the problem. Suppose there is a base class A and two classes x and y are derived from A which has a method aMethod() then x and y both will inherit the method. Furthermore they can override their inherited method from A. Now, if again a derived class call B is derived from x and y it will inherit the aMethod() from x and from y. If class B is instanciated and the aMethod() is called the obvious ambiguity is faced and therby leading to complexity. In order to keep the advantages of multiple inheritance java has incorporated the concept of "interface" which is nothing but type.
I hope this'll give you and idea and get you started out with the subtle and finer features of java. REad good books to probe more deeply. you'll understand.

bye
Bikgeek
Reply With Quote
  #8 (permalink)  
Old 01-05-2007
Junior Member
 
Join Date: Jan 2007
Location: india
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
bikgeek is on a distinguished road
Re: multiple inheritance in java...

In C++ multiple inheritance in not of much is not true. It's all about design. In oopl the focus is and must be on design first. Without design one can't go further with creating good quality software. Ask yourself first what is complexity and why is it inherent in software(industrial-strength-software) systems? How to manage the complexity? These are the issues one has to face. And experts say that in limited sense even in C you can achive an OO approach.
Reply With Quote
  #9 (permalink)  
Old 01-05-2007
Junior Member
 
Join Date: Jan 2007
Location: india
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
bikgeek is on a distinguished road
Re: multiple inheritance in java...

in java multiple inheritance concept(yes it is a designing concept, many language implements different way) is not removed but achived in a better way than in C++. In C++ the problem with multiple inheritance is solved through "virtual function". I am not going into detail in this space. But In java also runtime polymorphism (which is the main goal of Object Oriented Systems) is achived through "virtual function". THINK! that is required when dealing with OO.
Reply With Quote
  #10 (permalink)  
Old 01-05-2007
Expert Member
 
Join Date: Oct 2005
Location: india,cochi,bangalore
Posts: 395
Thanks: 2
Thanked 8 Times in 7 Posts
vmshenoy is on a distinguished road
Re: multiple inheritance in java...

hi suresh
I think so that u haven't got my pt!what iam trying to say is that if you have an better altenative to multiple inheritance ,then y to keep such an feature.Can u plz tell are there any programs in C++ in which using multiple inheritance is necessary. (OFCOUSE there will be many programs using multiple inheritance,but in all those v can convert them to multilevel inheritance)
bye
__________________
:)
NEVER SAY DIE.
Reply With Quote
  #11 (permalink)  
Old 01-06-2007
Contributing Member
 
Join Date: Sep 2006
Location: bangalore, india
Posts: 1,007
Thanks: 0
Thanked 76 Times in 64 Posts
psuresh1982 will become famous soon enough
Re: multiple inheritance in java...

Shenoy,

If i have five classes that is already tested and every class have a different functionality. Now i create a new class and i want to use the above classes functionality in my new class. How you achieve using multilevel inheritance?

-----------------
suresh
Reply With Quote
  #12 (permalink)  
Old 01-06-2007
Junior Member
 
Join Date: Dec 2006
Location: Theni
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Chithambaram.P is on a distinguished road
Thumbs up Re: multiple inheritance in java...

Hi
Suresh.P
I'm Chithambaram doing II MCA in Coimbatore Institute of Management And Technology.
In Java all the concepts are explained in Class and Objects Only,If we are use Multiple inheritence,it makes big confusion.It is a headache for programmers,so before developing the java program the C++ developers avoided the multiple inheritence.
Instead, Java's designers chose to allow multiple interface inheritance through the use of interfaces, an idea borrowed from Objective C's protocols. Multiple interface inheritance allows an object to inherit many different method signatures with the caveat that the inheriting object must implement those inherited methods.

Multiple interface inheritance still allows an object to inherit methods and to behave polymorphically on those methods. The inheriting object just doesn't get an implementation free ride. For an excellent discussion of interface inheritance, read Wm. Paul Rogers's "Reveal the Magic Behind Subtype Polymorphism"
Thank You.
By
Chithambaram.P
Reply With Quote
  #13 (permalink)  
Old 01-06-2007
Contributing Member
 
Join Date: Sep 2006
Location: bangalore, india
Posts: 1,007
Thanks: 0
Thanked 76 Times in 64 Posts
psuresh1982 will become famous soon enough
Re: multiple inheritance in java...

Nice to hear you chithambaram....

According to my previous example, how do you acheive using interface ?

In C++ they simply inherits all the classes and then used it....

But here....I expect your reply....

-----------------------
suresh
Reply With Quote
  #14 (permalink)  
Old 01-06-2007
Expert Member
 
Join Date: Oct 2005
Location: india,cochi,bangalore
Posts: 395
Thanks: 2
Thanked 8 Times in 7 Posts
vmshenoy is on a distinguished road
Re: multiple inheritance in java...

hi suresh

plz read this article CAREFULLY and i think ur doubt on Multiple inheritance will b clear.

Designing with interfaces - Java World


bye
__________________
:)
NEVER SAY DIE.
Reply With Quote
  #15 (permalink)  
Old 01-06-2007
Expert Member
 
Join Date: Oct 2005
Location: india,cochi,bangalore
Posts: 395
Thanks: 2
Thanked 8 Times in 7 Posts
vmshenoy is on a distinguished road
Re: multiple inheritance in java...

believe me !if u read this article carefully,i suppose ur query will b solved.

Bye
__________________
:)
NEVER SAY DIE.
Reply With Quote
  #16 (permalink)  
Old 01-08-2007
Contributing Member
 
Join Date: Sep 2006
Location: bangalore, india
Posts: 1,007
Thanks: 0
Thanked 76 Times in 64 Posts
psuresh1982 will become famous soon enough
Re: multiple inheritance in java...

Nice shenoy....

Once again you explain the diamond problem deeply....Thanks...

I told you i have a valuable reason why they are not using multiple inheritance.

I expect some reply about this....After i post my answer....

------------------
suresh
Reply With Quote
  #17 (permalink)  
Old 01-08-2007
Contributing Member
 
Join Date: Sep 2006
Location: bangalore, india
Posts: 1,007
Thanks: 0
Thanked 76 Times in 64 Posts
psuresh1982 will become famous soon enough
Re: multiple inheritance in java...

In most of the books they explain this diamond problem.....I already know this one...But my assumption is this is not a main reason for removing multiple inheritance....

This is one of the reason.....i accept it..

----------------
suresh
Reply With Quote
  #18 (permalink)  
Old 01-09-2007
Junior Member
 
Join Date: Jan 2007
Location: Surat
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Ankita Modi is on a distinguished road
Re: multiple inheritance in java...

ya though java doesnt support multiple inheritance directly bt the task is accomplalished by using concept of interface in java.