2

I know that multiple inheritance is not supported in java. I wrote the code as shown below.

abstract class abc {
    public abstract void print();
}

abstract class xyz {
    public abstract void print();
}

public class Test {

    public static void main(String[] args) {
        abc obj1 = new abc() {
            public void print() {
                System.out.println("abc");
            }
        };

        xyz obj2 = new xyz() {
            public void print() {
                System.out.println("xyz");
            }
        };

        obj1.print();
        obj2.print();       
    }
}

The output produced is:

abc
xyz

My question is, here I am using two abstract classes with a concrete class. Isn't that an implementation of multiple inheritance? And I intend to implement the code using classes, not interfaces.

khelwood
  • 55,782
  • 14
  • 81
  • 108

3 Answers3

3

I am using two abstract classes with a concrete class. Isn't that an implementation of multiple inheritance?

No, you are creating instances of two anonymous classes in your main method. There is no inheritance relation between the Test class and any of the two anonymous class instances created.

There is single inheritance relation between the abc class and the anonymous class that extends it.

There is another single inheritance relation between the xyz class and the anonymous class that extends it.

Eran
  • 387,369
  • 54
  • 702
  • 768
  • 3
    I disagree with "there is no inheritance" - `new abc() { ... }` is a subclass of `abc`. You probably meant "there is no multiple inheritance here" – Andrew Tobilko Aug 09 '18 at 11:59
  • 2
    @AndrewTobilko yes, I was inaccurate. I should have said there is no inheritance relation between the Test class and the two anonymous class instances. – Eran Aug 09 '18 at 12:02
  • Okay, I get your point. But using this method, I am still able to use the functionalities of both other classes in the Test class. Isn't that the purpose of inheritance? – Himanshu Goyal Aug 09 '18 at 12:08
2

An example of multiple inheritance is

interface IA {
    default void printOne() { System.out.println("one"); }
}
interface IB {
    default void printTwo() { System.out.println("two"); }
}
class C implements IA, IB {
}

C c = new C():
c.printOne();
c.printTwo();

The sample class have multiple interfaces it inherits directly from.

What Java doesn't allow is multiple inheritance of a super class.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • As a side note: Technically, an interface is **implemented**, not *extended*. It's not clear if one would call this *inheritance* too. See the following discussion: [If I implement an Interface, is it called an Inheritance?](https://softwareengineering.stackexchange.com/questions/316893/if-i-implement-an-interface-is-it-called-an-inheritance). – Zabuzard Aug 09 '18 at 12:09
  • @Zabuza to add to the confusion, the implementation is entirely in the interfaces, not the class which `implements` them. – Peter Lawrey Aug 09 '18 at 12:44
1

No, this is not multiple inheritance.

Multiple inheritance means that the same derived class inherits from multiple base classes: enter image description here

Your classes each have their own base class.

Note that you can have multiple inheritance of interfaces in Java.