• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

doubt about extend keyword

 
Manish Jaju
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why doesn't Java allow multiple inheritance using extend keyword, i mean, why java allow only one class to extend.. Please clarify.
 
sarada konda
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Explaning ur doubt with an example
suppose that a class can extends more than one class

class A
{
public void method1()
{
System.out.println("class A method");
}
}

class B
{
public void method1()
{
System.out.println("class B method");
}
}

class C extends A, B
{
public static void main(String s[])
{
C c = new C();
c.method1(); //here is the ambiguity
}
}

the ambiguity raises because it doesn't know which method to be called either of ClassA or of ClassB
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Although such ambiguity is one argument against multiple inheritance, it does not make it impossible. Other languages have multiple inheritance, and get around the ambiguity issue by various means.

I believe that the true reason Java does not have multiple inheritance is that it makes things simpler. JVMs and compilers are simpler to write, may be smaller and may run faster. Single inheritance source code is also usually easier to understand and to maintain, though that may not always be the case: if a problem lends itself to an obvious solution by multiple inheritance, which has to be "kludged" in a single-inheritance language, for example.
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java doesn't support multiple inheritance because the language designers don't think multiple inheritance is a good idea (and many people agree with them).

As a result multiple inheritance is not in the language specification which means it's not part of the language.

No other reason is needed.
 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To understand why Java doesn't support multiple inheritance, you should look at how Java got its start.

James Gosling was leading a programming project at Sun Microsystems using C++ and found that his team was spending as much time dealing with the complexities of the language as they were writing programs.

He decided to design a new C family language that, among other things, eliminated the language features that were difficult to understand and apply to real programs. Selecting the correct C++ function to dispatch with multiple inheritance has rules bordering on the metaphysical. Since we can't all be as smart as C++ inventor Bjarne Stroustrup, Gosling substituted multiple implemented interfaces, where the issue of choosing between method implementations never arises.

It's important to remember that programming languages were designed by real people with specific goals. No one language is right for every purpose. By understanding the design objectives of different languages, you are better equipped to choose the best one for your project.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic