Interface explained with example

Credits:
Naveen reddy (Telusko youtube channel)
www.geeksforgeeks.org

Like a class, an interface can have methods and variables, but the methods declared in interface are by default abstract (only method signature, no body).

  • Interfaces specify what a class must do and not how. It is the blueprint of the class.
  • If a class implements an interface and does not provide method bodies for all functions specified in the interface, then class must be declared abstract and the method without body needs to be abstract.
abstract class random implements testInterface{

	public abstract void something(); //No body with abstract
	public void hello() {
		System.out.println("Hello");
	}
}
//Because one method is abstract, the class must also be abstract
  • To declare an interface, use interface keyword.
  • It is used to provide total abstraction. That means all the methods in interface are declared with empty body
  • All fields are public, static and final by default.
  • A class that implement interface must implement all the methods declared in the interface. To implement interface use implements keyword.
  • Since java does not support multiple inheritance in case of class, but by using interface it can achieve multiple inheritance .
  • So the question arises why use interfaces when we have abstract classes?
    The reason is, abstract classes may contain non-final variables, whereas variables in interface are final, public and static.

Important points about interface

  • We can’t create instance(interface can’t be instantiated) of interface but we can make reference of it that refers to the Object of its implementing class.
  • A class can implement more than one interface.
  • An interface can extends another interface or interfaces (more than one interface) .
  • A class that implements interface must implements all the methods in interface.
  • All the methods are public and abstract. And all the fields are public, static, and final.
  • It is used to achieve multiple inheritance.

An example below:

//public class InterfaceDemo extends Pencil { // Extends can happen for only single class, JAVA does not support multiple inheritance
public class InterfaceDemo implements sharpner { // Implements interface now from extending class, implementing sharpner interface which again extends rubber and pencil adv

	public static void main(String[] args) {
		Pen p = new Pen(); // If show method is not static we need to create an object and then we can
							// access pen class
		p.showPen();

		Pencil.showPencil(); // If show method is static , we can access it after extending Pen class

		Pencil pc = new Pencil();
		pc.showPencil(); // Also we can access show by creating object of pencil class
		pc.erase();
		
		Pencil_advance pa=new InterfaceDemo(); // Reference of interface but object of class, to access the methods of interface
		pa.showPencilAdv();
		pa.eraseAdv();
		
		// Since there is already InterfaceDemo() object created, we need to cast for another one
		// Since sharpner interface already extends rubber and pencil advance, we can call all methods from pencil adv, rubber, sharpener
		sharpner sh=new InterfaceDemo();
		sh.sharpen();
		sh.EraseRubber();
		sh.showPencilAdv();
		sh.eraseAdv();
		
	}
	

	public void showPencilAdv() { 				//Method definition is mandatory if implementing interface
	System.out.println("Show Adv pencil");
	}

	public void eraseAdv() { 						///Method definition is mandatory if implementing interface
		System.out.println("Erase adv pencil");
	}


	public void EraseRubber() {
		// TODO Auto-generated method stub
		
	}


	public void sharpen() {
		// TODO Auto-generated method stub
		
	}

}
class Pen {
	void showPen() {
		System.out.println("Pen class_show method");
	}
}
class Pencil {
	static void showPencil() {
		System.out.println("Pencil class_show method");
	}
	void erase() {
		System.out.println("Pencil can erase");
	}
}
//Interfaces with declaration and no definition
interface Pencil_advance { 
	void showPencilAdv();
	void eraseAdv();
}
interface rubber{
	void EraseRubber();
}
interface sharpner extends rubber,Pencil_advance{ //Interface extends interface not implements, interface can extend multiple interface
	void sharpen();
}

Leave a Reply