Sitemap
Stackademic

Stackademic is a learning hub for programmers, devs, coders, and engineers. Our goal is to democratize free coding education for the world.

Interface In Java

4 min readFeb 26, 2024

--

In India, it is often said that the kid is just alike either mother or father (ये तो बिलकुल अपने पापा/माँ पर गया है|). This is nothing but inheritance. A kid has two parents and he/she has characteristics of both mother and father. C++ provides just the same kind of inheritance. One class can inherit many classes.

#include <iostream>
using namespace std;

class Vehicle {
public:
void drive() {
cout << "The vehicle is driving." << endl;
}
};

class Engine {
public:
void start() {
cout << "The engine is starting." << endl;
}
};

class Car : public Vehicle, public Engine {
public:
void drive() {
cout << "The car is driving." << endl;
}

void start() {
cout << "The car's engine is starting." << endl;
}
};

int main() {
Car car;

car.drive();
car.start();

return 0;
}

But, java doesn’t support extending (inheriting) multiple classes. Inheriting multiple classes can lead to code conflicts and multiply code complexity.

Java do not support Multiple Inheritance

public class Student1{
public void study(){
System.out.println("Study in morning");
}
}

public class Student2{
public void study(){
System.out.println("Study in night");
}
}

public class IdealStudent extends Student1,Student2{
//now this class inherits study methods from both the classes.
//if we call study method from this class, the won't resolve because
//there are two methods to execute
}

public class demo{
public static void main(String [] args){
IdeadStudent object1 = new IdealStudent();
object1.study(); //error
}
}

To solve this query, James Gosling and Team introduced Interfaces in java. Java classes can extend only one class but can implement many interfaces.

What is Interface?

Interface is a mechanism to achieve abstraction and multiple inheritance. An Interface is defined as an abstract type used to specify the behavior of a class. An interface in Java is a blueprint of a behavior. A Java interface contains static constants and abstract methods.

Properties of Interfaces:

  • Each method in interface is public and abstract by default
  • Methods in interface do not have body
  • We can not create object of inheritance
  • Interface does not have constructor
  • default/static keyword is used to write body for any method in interface
  • If one wants to override a method in interface, one can use default while writing method in interface (default used here is a modifier and not an access specifier) [introduced from 1.8]
public interface demo1{
static void fun(){};
}

public interface demo2{
default void gun(){ // default is modifier
System.out.println("In gun demo2");
}
}

public class DemoChild implements demo1, demo2{
@Override
public void fun(){
System.out.println("In fun DemoChild");
}
}

public class client{
public static void main(String [] args){
DemoChild object1 = new DemoChild();
object1.gun();
object1.fun();

//Output:
// In gun demo2
// In fun DemoChild
}
}
  • Static method written in interface can not be inherited to child
public interface demo{
static void fun(){
System.out.println("In fun");
}
}

public class DemoChild implements demo{

}

public class Client{
public static void main(String [] args){
DemoChild object1 = new DemoChild();
obj.fun(); //error
}
}

Real Time Example:

Following is example of multilevel multiple inheritance implementation in java using interfaces.

interface Player {
void play();
void train();
}

interface Forward extends Player {
void scoreGoals();
}

interface Defender extends Player {
void defendGoals();
}

class FootballPlayer implements Forward, Defender {
@Override
public void play() {
System.out.println("FootballPlayer is playing");
}

@Override
public void train() {
System.out.println("FootballPlayer is training");
}

@Override
public void scoreGoals() {
System.out.println("FootballPlayer is scoring goals");
}

@Override
public void defendGoals() {
System.out.println("FootballPlayer is defending goals");
}
}

public class Main {
public static void main(String[] args) {
FootballPlayer player = new FootballPlayer();
player.play();
player.train();
player.scoreGoals();
player.defendGoals();

//Output:
//FootballPlayer is playing
//FootballPlayer is training
//FootballPlayer is scoring goals
//FootballPlayer is defending goals
}
}
Press enter or click to view image in full size

A message from our Founder

Hey, Sunil here. I wanted to take a moment to thank you for reading until the end and for being a part of this community.

Get Atharv Tembhurnikar’s stories in your inbox

Join Medium for free to get updates from this writer.

Did you know that our team run these publications as a volunteer effort to over 3.5m monthly readers? We don’t receive any funding, we do this to support the community. ❤️

If you want to show some love, please take a moment to follow me on LinkedIn, TikTok, Instagram. You can also subscribe to our weekly newsletter.

And before you go, don’t forget to clap and follow the writer️!

--

--