1

I'm learning about inheritance and I don't quite understand the part of multiple inheritance.

This is my question :

If I create three class name :

Class A

Class B

Class C

Is it possible for class A to inheritance from class C, and also class B inheritance from class C doesn't it makes multiple inheritance because two classes (A,B) inheritance from one class C is that allowed in java?

Basicly this is my question : Is it possible to inheritance from a class only once meaning there cannot be two different classes that inheritance from the same class?

Thank you.

JaVaPG
  • 169
  • 1
  • 4
  • 9

9 Answers9

2

This is not multiple inheritance you describe. One could describe it as reversed multiple inhertiance. Multiple inheritance means a class, say A inherits features from both B and C.

I don't see a reason to allow only one class who inherits from the parent class. One can use the final keyword to state that no child classes can be derived.

Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
2

Multiple inheritance e.g. class A extends B extends C is not supported by Java

Multiple inheritance is where we inherit the properties and behavior of multiple classes    
to a single class. C++, Common Lisp, are some popular languages that support multiple   
inheritance.

In Java, an interface can extend multiple interfaces, some conclude that this means Java can support multiple inheritance. This is not true!. Extending multiple interfaces is not multiple inheritance since a property and behavior inherited will come for a single implementation class. There is never ambiguity.

Multiple inheritance leads to the Diamond Problem. The Diamond Problem does not exist in Java because multiple inheritance is not possible.

cmd
  • 11,622
  • 7
  • 51
  • 61
2

What you described in your second last paragraph

Is it possible for class A to inheritance from class C, and also class B inheritance from class C doesn't it makes multiple inheritance because two classes (A,B) inheritance from one class C is that allowed in java?

Is not called "multiple inheritance", it is simply that Class C has two subclasses. As the other answers have already stated, the concept of multiple inheritance is when one class extends 2 or more classes. So in practical terms, for example, that might mean something like class A extends B, C, etc. In most Object Oriented Languages, such as Java, this is not possible. Some languages, such as C++, do allow this.

Paul Richter
  • 10,908
  • 10
  • 52
  • 85
1

Multiple inheritance means class a extends b extends c which is not allowed in java

Additional Information why java does not provide multiple inheritance

Due to the concept of overridding Java doesn't support multiple inheritance. We have the concept of " Deadly Diamond of Death". i.e., As we have an overridding concept in Java. We know what overridding is, where the method name including parameters return types should be same. If mutliple inheritance is provided in java. eg: A<- B, A-

This is the reason why we dont have multiple inheritance in java. source

SpringLearner
  • 13,738
  • 20
  • 78
  • 116
1

Multiple inheritance means that one class inherits directly from two or more other classes. You can't have class A extend both B and C, for example. It's fine for multiple classes to all extend the same class as you describe. This is just multiple instances of single inheritance.

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
1

Multiple inheritance is when from class A you extends class B, and C.

In java there is no multiple inheritance, but you can implement any Interface you want, but only extend one class.

public interface InterfaceA{

}

public interface InterfaceB{

}

public class classA implements InterfaceA, InterfaceB{

}

public class classB extends classA{

}

How to perform multiple inheritance in Java

the process to perform multiple inheritance in java is to use Composite design pattern.

Imagine you have

public interface IPizza{
    void build();
}

public class CheesePizza implements IPizza{
    public void build(){
    }
}

public class BarbPizza implements IPizza{
    public void build(){
    }
}

and you want to implement a BarbCheesePizza, I would recommend to use Composite and has an instance of each one.

public class BarbCheesePizza implements IPizza{

    private BarbPizza barbPizza;
    private CheesePizza cheesePizza;

    public void build(){
         barbPizza.build();
         cheesePizza.build();
    }
}
RamonBoza
  • 8,898
  • 6
  • 36
  • 48
1

Your base class can have as many childs as you want. However, you can't have a class that extends(is a children of) 2 or more parents. See the Diamond Inheritance problem and how to solve it in Java : http://www.programmerinterview.com/index.php/java-questions/java-diamond-problem/

Silviu Burcea
  • 5,103
  • 1
  • 29
  • 43
0

Basicly this is my question : Is it possible to inheritance from a class only once meaning there cannot be two different classes that inheritance from the same class?

No, in general you can't disable to do so and this is not usable.

You can prevent inheritance by using final keyword in this case no one can inherit your class:

public final class MyClass {
}

But if you want - you can. It is not a good idea. It is a very ugly construction :) but...

public class MyClass {
    public MyClass() {
        checkSubclass();
    }

    public void checkSubclass() {
        if( !getClass().getName().equals("ALLOWED CLASS NAME THERE") ) {
            throw new RuntimeException("Subclassing not allowed");
        }
    }
}

The better way is "hide" class inside package, using private or default access modifier, and inherit it in the same package.

Nicolai
  • 5,489
  • 1
  • 24
  • 31
0

Java doesn't support Multiple Inheritance. In this child class at time one parent class extends. Like this

public class A{

}

public class B extends class A{ }

Iren Patel
  • 729
  • 1
  • 10
  • 22