• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

doubt about extend keyword

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
What do you have in that there bucket? It wouldn't be a tiny ad by any chance ...
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic