CrazyEngineers Forum

Turn The Screws!

Open your eyes, look in front, look at the back, look at any angle and you will see something that is an engineer's creation.

Engineers have been making things possible that others could only imagine. It perfectly makes sense to have a common place for engineers from around the world where they can share ideas, innovate, & help each other. Engineers are eternal, with the younger at 62 & the youngest at 17, the CEan gang consists of working professionals, students, entrepreneurs, CEOs, professors, geeks & nerds.

Need we say more? Click Here To Join The Gang!
Navigation
Go Back   CrazyEngineers Forum > CE : Technical Discussions > Computer Science & IT Engineering
Reply

  #1 (permalink)
Old 2nd July 2008, 06:08 PM
CE - Apprentice
 
I'm a Crazy Computer Science & IT Engineer
Join Date: 30th June 2008
Posts: 12
Default Java-Multiple inheritance

Hello CEans. Can anybody please help me know what is the problem with multiple inheritance. Why Java does't support it?
prakash.athani is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored links
  #2 (permalink)
Old 2nd July 2008, 06:12 PM
Good Administrator
 
The_Big_K's Avatar
 
I'm a Crazy Electrical Engineer
Join Date: 26th November 2005
Location: Terra-Firma
Posts: 3,969
Send a message via Yahoo to The_Big_K
Default Re: Java-Multiple inheritance

Well, most probably because the designers of Java wanted it to keep it a simple, object oriented language.

Java does support multiple inheritance through 'interfaces':

Check: Java Multiple Inheritance

and

Designing with interfaces - Java World
The_Big_K is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)
Old 4th July 2008, 01:14 PM
CE - Apprentice
 
I'm a Crazy Computer Science Engineer
Join Date: 12th March 2008
Location: Bangalore
Posts: 12
Send a message via Yahoo to shalini_goel14
Default Re: Java-Multiple inheritance

Check the following scenario:
class A{
void method1(){
//some code here
}
}

class B{
void method1(){
// some code here
}
class C extends A,B { //do not try..not possible in java
//assuming it inherits method1() method

}
class JavaMultipleInheritenceTest{
public static void main(String args[]){
C cObj=new C();
cObj.method1();//creates an ambiguous situation for JVM to detect which
class's overridden method method1() it is asking for(classA or class B)

}
}
This is the reason in Java we cannot extend multiple classes but can extend multiple interfaces.
__________________
Thanks & regards
Shalini
shalini_goel14 is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)
Old 5th July 2008, 12:25 AM
CE - Newbie
 
I'm a Crazy software Engineer
Join Date: 4th July 2008
Posts: 2
Default Re: Java-Multiple inheritance

Quote:
Originally Posted by shalini_goel14 View Post
Check the following scenario:
class A{
void method1(){
//some code here
}
}

class B{
void method1(){
// some code here
}
class C extends A,B { //do not try..not possible in java
//assuming it inherits method1() method

}
class JavaMultipleInheritenceTest{
public static void main(String args[]){
C cObj=new C();
cObj.method1();//creates an ambiguous situation for JVM to detect which
class's overridden method method1() it is asking for(classA or class B)

}
}
This is the reason in Java we cannot extend multiple classes but can extend multiple interfaces.

The problem you defined is simple to solve. It just uses simple method renaming so they have different names. Dynamic binding would know what method to call in the case of polymorphism despite the renaming.

The more complicated problem to solve is the case of repeated inheritance where one class directly or indirectly inherits several times from the same class. But even for this there is elegant solution as implemented in Eiffel.
viktor_ns is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)
Old 7th July 2008, 03:05 PM
CE - Apprentice
 
xero's Avatar
 
I'm a Crazy Computer Science Engineer
Join Date: 13th January 2007
Posts: 24
Default Re: Java-Multiple inheritance

Starting with what's the problem with multiple inheritance.

Let's start with a funny scenario. Consider a family, comprising of a child and his/her mom and dad

A common behavior of mom and dad is that both are short tempered, with a difference that child's dad starts breaking things when angry, and mom only yells.

Now, the child's also short tempered, but its impossible to know whether the short tempered behavior is his/her's mom's or dad's (assuming that child won't get both the behaviors )

So I guess, the problem with multiple inheritance is pretty clear


Second question: Why java doesn't support it?

I would rephrase the question, Why java doesn't allow it?
In Java, the above scenario is resolved as ...
in java, if the child is short tempered then its behavior must be known before its born !

Now, as stated in above replies that multiple inheritance is achieved via interfaces.
Interfaces are not objects, and inheritance only exists between objects, so i guess its no more multiple inheritance

I think this should answer your question.. By the way, i hate explaining OOP concepts with code, so if u were expecting it, m sorry, I won't

n'joy
__________________
crAzy xerO
xero is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored links
  #6 (permalink)
Old 7th July 2008, 04:53 PM
CE - Apprentice
 
I'm a Crazy Computer Science & IT Engineer
Join Date: 30th June 2008
Posts: 12
Default Re: Java-Multiple inheritance

Do you guys think that multiple inheritence is an ambiguous feature that an OO language should not allow?

C++ allows multiple inheritence.

Check out this paper by Bjarne Stroustrup (inventor of C++) where he mentions its misconception.

http://www-plan.cs.colorado.edu/diwan/class-papers/mi.pdf

Also for the scenarios mentioned by shalini_goel14 and xero, we just need a scope resolution operator :: to decide which parent class's method should a child class's object invoke

Also can you guys give me examples of some systems developed using C++,
having problems or crashed, because of its multiple inheritence feature ?

Last edited by prakash.athani : 7th July 2008 at 05:20 PM.
prakash.athani is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)
Old 7th July 2008, 05:26 PM
CE - Apprentice
 
xero's Avatar
 
I'm a Crazy Computer Science Engineer
Join Date: 13th January 2007
Posts: 24
Default Re: Java-Multiple inheritance

I'll start with clearing a mis conception that inheritance is a feature. Inheritance is a characteristic of OO paradigm, so multiple inheritance is not a feature, and it is ambiguous, but not bad :P

and yes C++ has its own way to implement it using virtual functions if i'm not wrong, so wherever multiple inheritance comes into picture, it would have been / will be implemented using the standard ways of C++ (either using virtual functions or any new mechanism )

And i don't think that applications/systems will ever crash if multiple inheritance is implemented, atleast i know about those written in C++.


And even if it does crash, then its CURSED !!

n'joy
__________________
crAzy xerO
xero is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)
Old 7th July 2008, 05:59 PM
CE - Apprentice
 
xero's Avatar
 
I'm a Crazy Computer Science Engineer
Join Date: 13th January 2007
Posts: 24
Default Re: Java-Multiple inheritance

Hey prakash thanks for the linked. Its pretty good, i would suggest everyone to go through it once !!
__________________
crAzy xerO
xero is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)
Old 8th July 2008, 06:38 AM
CE - Newbie
 
I'm a Crazy software Engineer
Join Date: 4th July 2008
Posts: 2
Default Re: Java-Multiple inheritance

I like this little bit written by Bertrand Meyer:

Quote:
In some parts of the object-oriented community, the mere mention of multiple inheritance seems to evoke visions of mysterious, uncharted territories where complexity is the law of the land and treacherous ambiguities lie in cover behind every bush, ready to prey on the poor wandering programmer. The purpose of this month's column is to dispel any such image and show that multiple inheritance is in fact as simple to use as it is powerful.
For a full column follow this link.

I must point out one more thing. We do inherit features from our parents but we are not our parents. That is, child is not its mother, neither is its father. Child has a mother and has a father so that can only be a client relationship not an inheritance.

But CHILD can inherit from classes HUMAN_THAT_YELLS and HUMAN_THAT_BREAKS_THINGS that inherit from class HUMAN. If HUMAN has method engage_short_temper() and it is redefined in both classes HTY and HTB problem can be solved in two ways. First is that in CHILD we make one method from one parent deferred and take the method only from other parent. Second is that we rename methods and select one to use with dynamic binding when engage_short_temper() is called on HUMAN that is really a CHILD.
viktor_ns is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored links
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +5.5. The time now is 04:50 PM.
Powered by vBulletin® Version 3.6.7
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.1.0
Member comments are owned by the poster. Copyright © 2005-2008 CrazyEngineers.com. All rights reserved.Ad Management by RedTyger