Multiple inheritance

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Debabrata Jana
    New Member
    • Mar 2007
    • 8

    Multiple inheritance

    Dear Friends
    I have a confution.....
    In java all the class are extends java.lang.Objec t class.
    Now suppose you write a class ----
    public class B extends A
    {
    // Some code here
    }

    Logically, Here class B exteds class A and also extends class java.lang.Objec t. Now java does not support multiple inheritance. Then how does java handle this situation? Please explain it.

    Thank you very much.

    Debabrata
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by Debabrata Jana
    Dear Friends
    I have a confution.....
    In java all the class are extends java.lang.Objec t class.
    Now suppose you write a class ----
    public class B extends A
    {
    // Some code here
    }

    Logically, Here class B exteds class A and also extends class java.lang.Objec t. Now java does not support multiple inheritance. Then how does java handle this situation? Please explain it.

    Thank you very much.

    Debabrata
    Yep, Java does not support multiple inheritance directly.

    Multiple inheritance would be something like

    Code:
    class C extends B, A {
    }
    In your example, B extends class A only. Every class is a descendant of Object but that does not mean that every class extends Object.

    Comment

    • dmjpro
      Top Contributor
      • Jan 2007
      • 2476

      #3
      Originally posted by Debabrata Jana
      Dear Friends
      I have a confution.....
      In java all the class are extends java.lang.Objec t class.
      Now suppose you write a class ----
      public class B extends A
      {
      // Some code here
      }

      Logically, Here class B exteds class A and also extends class java.lang.Objec t. Now java does not support multiple inheritance. Then how does java handle this situation? Please explain it.

      Thank you very much.

      Debabrata

      A good Question you made.
      I appreciate your thought.
      Now look Inheritance,Dat a Encapsulation or may be Polymorphism all these things are Programming Paradigms.
      Means decomposing a big problem.
      And the syntax is for easy learning for programmers and for easy compilation.
      But the ultimate native Code that is totally different from these things.
      The compiler is much more intelligent to understand this situation.
      Look, here B extends A and both of these classes extend Object.
      Now if you are from C++ background then it will be easier to understand.
      Actually to avoid this problem Java removes this Multiple inheritance.
      Here, whatever it may be, you can access the method of java.lng.Object class.
      Now have a look at another situation.
      Like...!
      [code=java]
      Class A
      {
      void test(){}
      }
      interface B
      {
      void test();
      }
      class C extends A implements B
      {
      void test(){}
      //Now here is a question method test is implementation of B or overriding of A.
      //Here the compiler would report, must be as public, because of implementing B
      //But whether you implement or override that does not a matter.
      }
      [/code]

      Java Compiler is much efficient to handle this situation very effectively.
      Actually Java does not support Multiple Inheritance because of programmers have to take care of the Odd keyword Virtual as in C++.
      I think now the picture is clear to you.

      Kind regards,
      Dmjpro.

      Comment

      • kreagan
        New Member
        • Aug 2007
        • 153

        #4
        Originally posted by Debabrata Jana
        Dear Friends
        I have a confution.....
        In java all the class are extends java.lang.Objec t class.
        Now suppose you write a class ----
        public class B extends A
        {
        // Some code here
        }

        Logically, Here class B exteds class A and also extends class java.lang.Objec t. Now java does not support multiple inheritance. Then how does java handle this situation? Please explain it.

        Thank you very much.

        Debabrata
        I agree, this is an interesting question. My friend always told me, Java is ASEXUAL. This means that classes can have only 1 parent class (if it choose to).

        In this LinkedList inherits functionality from its parent, grandparent (abstract list), and so forth. The grand daddy of them all is Object. Hopefully this linear representation will give you a clue. If you want your LinkList to have more properties, such as Queue properties, you must INTERFACE/IMPLEMENT Queue and fill in the Queues stub.

        In C++ multiple inheritance means that a class can have more than 1 parent class.

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          An interface is a pure type definition. A class is a type plus implementation
          definition. Java does support multiple inheritance of types and it supports
          single inheritance of implementation.

          Java makes a distinction between types and implementations , C++ doesnt.
          If C++ hadn't allowed for multiple inheritance it would've been a worthless little
          language, OO-wise speaking, and just because it doesn't distinguish between
          type and implementation it has to allow for multiple inheritance of implementation.

          That's all there is to it.

          Back to the OP's question: a class either explicitly extends from another class
          or it implicitly extends from the Object class; so all classes form a tree in which
          every node (indirectly) extends from the Object class.

          kind regards,

          Jos

          Comment

          • praveen2gupta
            New Member
            • May 2007
            • 200

            #6
            Originally posted by Debabrata Jana
            Dear Friends
            I have a confution.....
            In java all the class are extends java.lang.Objec t class.
            Now suppose you write a class ----
            public class B extends A
            {
            // Some code here
            }

            Logically, Here class B exteds class A and also extends class java.lang.Objec t. Now java does not support multiple inheritance. Then how does java handle this situation? Please explain it.

            Thank you very much.

            Debabrata
            Hi
            Object is the super class of all classes in java.
            Case1.
            public class B extends A
            {
            // Some code here
            }

            class B is inherting only class A It is not inheriting class Object, and class A is inheriting class Object Class B is inheriting only one class at a time that is class A
            so it is not multiple inheritance.

            Case 2
            public class B
            {
            // Some code here
            }
            In this case class B is inheriting class Object by default.

            Comment

            Working...