Results 1 to 9 of 9
  1. #1
    Neha Deshmukh is offline Member
    Join Date
    Mar 2011
    Location
    Nagpur
    Posts
    2
    Rep Power
    0

    Default Why there is no multiple inheitance in java?

    I am fresher. I want to improve java skills. So help me.

  2. #2
    dswastik is offline Senior Member
    Join Date
    Dec 2008
    Location
    Kolkata
    Posts
    282
    Rep Power
    6

  3. #3
    Neha Deshmukh is offline Member
    Join Date
    Mar 2011
    Location
    Nagpur
    Posts
    2
    Rep Power
    0

    Default

    sir i want only explanation not a link

  4. #4
    subith86 is offline Senior Member
    Join Date
    Jan 2011
    Location
    Bangalore, India
    Posts
    102
    Rep Power
    0

    Default

    the reason is very well described in that link.

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,515
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Neha Deshmukh View Post
    I am fresher. I want to improve java skills. So help me.
    Java doesn't have multiple inheritance (MI) of implementation; it does have MI of type (interface). MI of implementation is a burden because it needs carefull fiddling with the 'this' pointer and Java tries to keep things simple so it doesn't implement it. If a class D already extends a class B and you also want to 'extend' it a class C, class C needs to implement an interface CI, so you have:

    Java Code:
    interface CI { void aMethod(); }
    class C implements CI { ... }
    class D extends B { ... }
    Now create an instance of C and pass it to D as follows:

    Java Code:
    class D extends B implements CI {
       private C c;
       public D(C c) { this.c= c; }
       public void aMethod() { c.aMethod(); }
    }
    As you can see a B passed the call to aMethod() to its member c and it implements CI, just as a C does. Through composition you have the same effect as if MI of implementation were allowed.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    sanjeevtarar's Avatar
    sanjeevtarar is offline Senior Member
    Join Date
    Apr 2008
    Location
    Jaipur(India)
    Posts
    318
    Rep Power
    7

    Default

    There is a way of Multiple inheritances in Java using Interfaces

    One reason why multiple class inheritance in not allowed because class describe HOW to do a thing and in interface describes only WHAT to do that's why it is allowed at interface level only

    because in case of class, a subclass can not decide which implementation to pick
    sanjeev,संजीव

  7. #7
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Say you subclassed two different classes, and both classes have a method of the same name, how would the compiler be able to tell the difference?

    This is one of the things that makes multiple inheritance more difficult. In the end it comes down to the fact that using single inheritance is easier.

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,515
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by sunde887 View Post
    Say you subclassed two different classes, and both classes have a method of the same name, how would the compiler be able to tell the difference?
    C++ can't tell the difference either, it picks the implementation that is first in the list of extended classes, i.e.

    Java Code:
    class C : B1, B2 { ... }
    if a method M would've been present in both B1 and B2, the implementation of M in B1 is chosen because B1 comes first in the list.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Ah, thanks for the information, one of the books I had read said that was one fault of multiple inheritance.

Similar Threads

  1. How to put multiple image in java?
    By Miwrath in forum New To Java
    Replies: 5
    Last Post: 10-12-2010, 11:38 AM
  2. Running multiple threads on multiple CPU cores?
    By Dosta in forum Threads and Synchronization
    Replies: 2
    Last Post: 09-19-2010, 03:48 PM
  3. Multiple Java in a single BOX
    By luke_devon in forum New To Java
    Replies: 2
    Last Post: 07-05-2010, 01:59 PM
  4. multiple inheritance in java
    By pawanspace in forum New To Java
    Replies: 2
    Last Post: 12-31-2007, 04:08 AM
  5. Multiple Inheritance in java
    By paty in forum New To Java
    Replies: 4
    Last Post: 08-02-2007, 02:25 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •