Way2Java

Extend Multiple Classes Java

Extend Multiple Classes Java

Summary: At the end of this tutorial you will understand the usage of Extend Multiple Classes Java in the code.

We know earlier that Java does not support multiple inheritance but supports partially through interfaces.

After extends there must be only one class of either concrete (non-abstract) or abstract. After implements there must be interfaces only and can be of any number. But if a class would like to extend a number of concrete classes then, how it can? The following program illustrates.

class A {   }
class B extends A {   }
interface C {   }
interface D {   }

public class Demo extends B implements C, D
{
  public static void main(String args[])
  {
    System.out.println("Hello World");
  }
}

The above program is a combination of multilevel (between A and B) and multiple (with C and D) inheritance. Demo extends B and in turn B extends A. Now Demo can make use of the methods of A and B. Demo implements the interfaces C and D.

Even though Java does not support multiple inheritance directly, it can achieve all the benefits using extends and implements as in the above program.