In the previous tutorial, we have learned about how abstraction is achieved using abstract classes. Now let us learn about interfaces in Java.
Along with that, this tutorial will cover inheritance using interface.
Table of Contents:
An interface is similar to a class but not exactly class.
So the question is –what is the difference between class and interface in java?
The interface consists of data members and member functions but these member functions are abstract (i.e. they don’t have any implementation) by default.
In other words, the interface is just another way to accomplish abstraction in Java.
interface { //data members //abstract member functions }
As we know, a class can inherit another class, in the same way, an interface can extend another interface. But when a class wants to use interface, it implements it.
interface Readable { void read(); } class Student implements Readable { public void read() { System.out.println(“Student Reads.”); } } class Test { public static void main(String args[]) { Readable r = new Student(); r.read(); } }
Output:
Student Reads.
We all know about different types of inheritance in Java, as we have seen in our earlier tutorial. Now let’s dive into interface inheritance.
When one interface inherits or extends the properties of another interface is known as interface inheritance.
A simple illustration of the interface inheritance concept is as follows:
interface Story { void tell(); } interface Readable implements Story { void read(); } class Student implements Readable { public void read() { System.out.print(“Student reads ”); } public void tell() { System.out.print(“ amazing Stories..”); } } class Test { public static void main(String args[]) { Readable r = new Student(); r.read(); r.tell(); } }
Output:
Student reads amazing Stories..
Multiple inheritances in Java is depicted in two ways-
A basic example of multiple inheritances in Java using interfaces is as follows:
interface Writeable { void writes(); } interface Readable { void reads(); } class Student implements Readable,Writable { public void reads() { stem.out.print(“Student reads.. ”); } public void writes() { System.out.print(“ Student writes..”); } public static void main(String args[]) { Student s = new Student(); s.reads(); s.writes(); } }
Output:
Student reads.. Student writes..
One interface can hold another interface inside it. This is known as a nested interface.
interface Task { void display(); interface Play { void show(); } }
Note that, In Java 8, we are provided with the provision to create static and default methods in an interface.
Summing up Java program for multiple inheritance using interface, interfaces also become the medium to achieve abstraction. They are similar to a class but by default contain abstract member functions.
Hi to every single one. I’m new to programming. I find really consistent and precious Information on this website. Thank you!
Really worthy content… And good explanation.
interface Readable implements Story // wrong
interface Readable extends Story