0% found this document useful (0 votes)
116 views

Interfaces and Inheritance in Java

A class can extend one class and implement one or more interfaces. An interface can extend other interfaces, allowing for multiple inheritance through interfaces. When a class implements interfaces, it must provide implementations for all methods declared in the interfaces. This allows interfaces to inherit behavior while avoiding ambiguities caused by multiple inheritance through classes.

Uploaded by

egdejuana
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
116 views

Interfaces and Inheritance in Java

A class can extend one class and implement one or more interfaces. An interface can extend other interfaces, allowing for multiple inheritance through interfaces. When a class implements interfaces, it must provide implementations for all methods declared in the interfaces. This allows interfaces to inherit behavior while avoiding ambiguities caused by multiple inheritance through classes.

Uploaded by

egdejuana
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Interfaces and Inheritance in Java

Prerequisites: Interfaces in Java, Java and Multiple Inheritance

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

Interface inheritance : An Interface can extend other interface.

// Java program to demonstrate inheritance in


// interfaces.

// Java program to demonstrate that a class can


 import java.io.*;
interface intfA
// implement multiple interfaces
 {
 import java.io.*; void geekName();
interface intfA
 }
 {
void m1(); interface intfB extends intfA
 } {
void geekInstitute();
interface intfB }
{
void m2(); // class implements both interfaces and provides
} // implementation to the method.
class sample implements intfB
{
Output;
@Override
public void geekName()
{
System.out.println("Rohit");
}

@Override
public void geekInstitute()
{
System.out.println("JIIT");
}

public static void main (String[] args)


{
sample ob1 = new sample();

// calling the method implemented


// within the class.
ob1.geekName();
ob1.geekInstitute();
}
An interface can also extend multiple interfaces. }

// Java program to demonstrate multiple inheritance Output:


// in interfaces
 import java.io.*;
interface intfA
 {

Output:

Why multiple inheritance is not supported through class in Java, but it can be possible through interface ?.
Multiple inheritance is not supported by class because of ambiguity. In case of interface there is no ambiguity because implementation to
method(s) is provided by the implementing class. Refer Java and Multiple Inheritance for details.

This article is contributed by Nitsdheerendra. If you like GeeksforGeeks and would like to contribute, you can also write an article using
contribute.geeksforgeeks.org or mail your article to [email protected]. See your article appearing on the GeeksforGeeks main
page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

You might also like