Interfaces and Inheritance in Java

Course Curriculum

Interfaces and Inheritance in Java

Interfaces and Inheritance in Java

A class can extends another class and/ can implement one and more than one interface.

// Java program to demonstrate that a class can
// implement multiple interfaces
import java.io.*;
interface intfA
{
void m1();
}

interface intfB
{
void m2();
}

// class implements both interfaces
// and provides implementation to the method.
class sample implements intfA, intfB
{
@Override
public void m1()
{
System.out.println("Welcome: inside the method m1");
}

@Override
public void m2()
{
System.out.println("Welcome: inside the method m2");
}
}

class Prutor
{
public static void main (String[] args)
{
sample ob1 = new sample();

// calling the method implemented
// within the class.
ob1.m1();
ob1.m2();
}
}
Output;

Welcome: inside the method m1
Welcome: inside the method m2

Interface inheritance : An Interface can extend other interface.

// Java program to demonstrate inheritance in
// interfaces.
import java.io.*;
interface intfA
{
void prutorName();
}

interface intfB extends intfA
{
void prutorInstitute();
}

// class implements both interfaces and provides
// implementation to the method.
class sample implements intfB
{
@Override
public void prutorName()
{
System.out.println("prutor");
}

@Override
public void prutorInstitute()
{
System.out.println("IITK");
}

public static void main (String[] args)
{
sample ob1 = new sample();

// calling the method implemented
// within the class.
ob1.prutorName();
ob1.prutorInstitute();
}
}
Output:

prutor
IITK

An interface can also extend multiple interfaces.

// Java program to demonstrate multiple inheritance
// in interfaces
import java.io.*;
interface intfA
{
void prutorName();
}

interface intfB
{
void prutorInstitute();
}

interface intfC extends intfA, intfB
{
void prutorBranch();
}

// class implements both interfaces and provides
// implementation to the method.
class sample implements intfC
{
public void prutorName()
{
System.out.println("prutor");
}

public void prutorInstitute()
{
System.out.println("IITK");
}

public void prutorBranch()
{
System.out.println("CSE");
}

public static void main (String[] args)
{
sample ob1 = new sample();

// calling the method implemented
// within the class.
ob1.prutorName();
ob1.prutorInstitute();
ob1.prutorBranch();
}
}
Output:

prutor
IITK
CSE

Why Multiple Inheritance is not supported through a class in Java, but it can be possible through the interface?
Multiple Inheritance is not supported by class because of ambiguity. In case of interface, there is no ambiguity because implementation to the method(s) is provided by the implementing class up to Java 7. From Java 8, interfaces also have implementations of methods. So if a class implementing two or more interfaces having the same method signature with implementation, it is mandated to implement the method in class also. Refer Java and Multiple Inheritance for details.

Inheritance and constructors in Java (Prev Lesson)
(Next Lesson) Using final with Inheritance in Java
', { 'anonymize_ip': true });