Interface - Object Oriented Programming : Java

Table of Content

  1. Introduction
  2. What is Interfaces in Java
  3. Why Interfaces are important
  4. Some Important Point To Be Noted
  5. Syntax
  6. Java Interface Examples


Introduction

In the dynamic world of Software Development , Object Oriented Programming (OOP's) stands as a cornerstone for building robust , scalable applications . One of the key features that empowers OOP's is the concept of interfaces. In this article , we will discuss the interfaces and exploring how they enhance code modularity and flexibility.

What is Interfaces in Java? 


Java interfaces provide an abstraction archiving mechanism. The Java interface can only include abstract methods; method bodies are not allowed. In Java, it is used to archive both multiple inheritance and 100% abstraction. 
Stated differently, it is possible for interfaces to contain abstract variables and methods. There cannot be a method body for it.
 It stands for the IS-A connection.


Why Interfaces are Important ? 

there are three main reason :
1. to archive 100% abstraction.
2. using interfaces we can archive the multiple inheritance functionality in java
3.Archive lose coupling and modularity ,and reusability of Code.

Some important Point to be Noted :


1. Interface is implemented by abstract class and normal class using implement keyword.
2. One Interface can extends it parent interface using extends keyword class can not extends interface.
3.Every method in Interface is public and Abstract by default .
4. Whenever we are implementing any interface method in class it is compulsory to declare that method as public.
5.whenever we are implementing any interface every method of that interface should be implement compulsory.
6. interface is declared with interface keyword.

Learn about what is class click here 

Syntax:


1. Implementation of Interfaces :


interface interface_name1{
// static variables
// abstract methods
}
class class_name implement interface_name1{
//normal variables
//normal methods
// implementation of interface method 
}

2. Extend Interface :


interface interface_name1{
// static variables
// abstract methods
}
interface interface_name2 extends interface_name1{
// static variables
// abstract methods
}

*Note : abstract class also used to implement the Interface.

How implement Interface and Extends interface with Example :

before staring write a code of interface and it's implementation and extends it remember some point that are mention above in section of "Some important Point to be Noted :"
Let's Start,

Java Interface Examples:- 

Demo with Code 1(Extending interface and Implementing Interface):
Code:


interface FirstInter{

public void method1(); // abstract method of first interface

}

interface SecondInter extends FirstInter{

public void method2(); // abstract method of Second interface

}

class Message implements SecondInter {

public void method1(){

System.out.println("Implemented  Method from FirstInter ");

}
public void method2(){
System.out.println("Implemented Method from SecondInter");
}
}

public class TestInter{

public static void main(String args[]){
Message msg = new Message();
msg.method1();
msg.method2();
}
}

Output  1 : -


Output Screen of Code

.class file generated after compilation



Demo with code 2 ( when we implement only one method of interface ) :
Code: 


interface FirstInter{

public void method1(); // abstract method of first interface

}

interface SecondInter extends FirstInter{

public void method2(); // abstract method of Second interface

}

class Message implements SecondInter {

public void method1(){ // implementation of method1

System.out.println("Implemented  Method from FirstInter ");

}

}

public class TestInter{

public static void main(String args[]){
Message msg = new Message();
msg.method1();

}
}

Output : 

 

Hence , the Point Number 5 from section :"Some important Point to be Noted " has been proved .

Demo with code 3 (we cant declare method of interface as public in class while implementing, it generate error ) :
Code: 

interface FirstInter{

public void method1(); // abstract method of first interface

}

interface SecondInter extends FirstInter{

public void method2(); // abstract method of Second interface

}

class Message implements SecondInter {

 void method1(){ // removed public access modifier

System.out.println("Implemented  Method from FirstInter ");

}
 void method2(){ //removed public access modifier
System.out.println("Implemented Method from SecondInter");
}
}

public class TestInter{

public static void main(String args[]){
Message msg = new Message();
msg.method1();
msg.method2();
}
}

Output :

Output screen of methods without public

 Hence , the Point Number 4 from section :"Some important Point to be Noted " has been proved .

Demo with Code 4( if you don't know the implementation of Second method of interface and you have use only one method of interface as per Demo with code 2 Example  it is impossible . But there is one technique for solve this . use a Abstract class because abstract class is partially implemented class which contain abstract methods as well as concrete method also .and we have to extends that abstract class and them we can implement the second method )


See actual example below:

Code:


interface FirstInter{

public void method1(); // abstract method of first interface

}

interface SecondInter extends FirstInter{

public void method2(); // abstract method of Second interface

}

abstract class Message implements SecondInter { // declare class as abstract using abstract keyword 

public void method1(){ // concrete method 

System.out.println("Implemented  Method from FirstInter ");

}
}

class Message2 extends Message { // class extends it parent abstract class for
 // implementing the second method of secondinter

public void method2(){ 

System.out.println("Implemented  Method from secondinter ");

}
}


public class TestInter{

public static void main(String args[]){
Message2 msg = new Message2();
msg.method1();
msg.method2();
}
}

Output :


Output screen of abstract class



Comments