Interfaces in Java

Previous
Next

Class:              

  • Complete definition of Object
  • (Only Concrete methods are allowed)

Abstract class:            

  • Partial definition of Object
  • (Both Concrete & Abstract methods are allowed)

Interface:        

  • Complete specification (declaration) of Object
  • (Only abstract methods are allowed)

Note: ‘interface’ is a pre-defined modifier is used to define set of specifications in java application.

interface Test
{
	void method()
	{
		// Error : interface methods can't have body
	}
}

Note: By default interface methods are “public & abstract”

interface Test
{
	void f1() ; // by default public & abstract
	void f2() ;
}

To check compiler added code to the “class file”, we use javap command.

Can we define a constructor inside interface?

Answer: No, constructor is a special method in java. We cannot define constructor inside the interface.

Can we instantiate an interface?

Answer: No, we need constructor definition to instantiate class or interface but it doesn’t have constructor. It is impossible to instantiate without constructor definition.

interface Test{
	/* logic */
}
class Main{
	public static void main(String args[]){
		new Test();// Error : interface cannot be instantiated
	}
}

Following table explains in how many ways we can instantiate class, abstract class and interface.

Can we define main method inside interface?

  • From JDK1.8 version, it is allowed to define static methods inside interface.
  • Main() method is static, hence allowed.
  • JVM access main() method automatically when we run interface.
interface Test 
{
	public static void main(String[] args) 
	{
		System.out.println("main() inside interface");
	}
}

Why they allowed static members inside interface?

  • Static members are not specific.
  • Abstract means Specific(Non static)
  • Static members can be accessed using class name.
  • Static method cannot be overridden.

We can define user static methods inside interface and access them using identity of interface.

interface Test 
{
	public static void main(String[] args) 
	{
		System.out.println("main() inside interface");
		Test.check();
	}
	static void check()
	{
		System.out.println("user static method in interface");
	}
}

Interface cannot be extended:

interface Test 
{
}
public class Access extends Test
{
}
  • Interface contains specifications(abstract methods).
  • We must override(implement) the methods instead of extends(update)
  • Hence we use ‘implements’ keyword to connect classes and interfaces.
interface Test 
{
}
public class Access implements Test
{
}

Why a class ‘implements’ interface instead of ‘extends’ ?

  • Classes contains concrete methods(with logic) can be updated(extended)
  • Abstract classes contains concrete methods(with logic) can be extended.
  • Interface contains only abstract methods(no logic) cannot be updated. We must provide body(implement logic).

Note: Interface methods are by default public and abstract. Hence these methods must override as public only. While overriding the method, we cannot decrease access privileges.

  • Private can override as protected
  • Protected can override as public
  • Public can override as public
  • Public cannot override as protected or package or private
interface Test 
{
	void fun();
}
public class Access implements Test
{
	void fun(){
		// logic
	}
}
Compile: Error - cannot override as package level method(it is public by default)

Need to override as public:

interface Test 
{
	void m1();
	void m2();
}
public class Access implements Test
{
	public void m1(){
		// logic
	}
	public void m2(){
		// logic
	}
}

Note: If any class is unable to implement all the specifications (abstract methods) of interface become ‘abstract class’

interface Test 
{
	void m1();
	void m2();
}
abstract class First implements Test
{
	public void m1(){
		// logic
	}
}
class Second extends First
{
	public void m2(){
		// logic
	}
}
Previous
Next

Add Comment

Courses Enquiry Form