Core Java - SPLessons

Java Interface

Chapter 19

SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Java Interface

Java Interface

shape Description

Java Interface, As discussed in the previous chapter interface is used to get full abstraction , to solve the problem of multiple Inheritance, and by default interface is an abstraction. Java interface also follows IS-A relationship. The main advantage of the interface is to get the loose coupling. Following some points needs to be remember.

shape Conceptual
figure

A class can implement the interface.

shape Example

interface printable{  
void print();  
}  
  
class splesson implements printable{  
public void print(){System.out.println("Hello Splesson.");}  
  
public static void main(String args[]){  
	splesson obj = new splesson();  
obj.print();  
 }  
}  

Where printable is an interface and it has only one method that is print().

interface printable{  
void print();}

Output:
When compile the code the result will be as follows.

Hello Splesson.

shape Example

package com.spl.absI;

public class AbstractInterface {
	public static void main(String[] args) {
		System.out.println("Program starts");
		DerivedClass dref=new DerivedClass();
		dref.test1();
		dref.test2();
		System.out.println("Program ends");

	}
}
interface Sample1
{
	void test1();
	void test2();
}
abstract class A implements Sample1
{
public void test1()
{
	System.out.println("test1() method");
}
}
class DerivedClass extends A
{

	@Override
	public void test2() {
		// TODO Auto-generated method stub
		System.out.println("test2() method");
	}

}


Output:
When compile the program following is the output will be displayed.

Program starts
test1() method
test2() method
Program ends

Interface-extends

shape Description

An Java Interface can inherit from another interface using “extends” keyword. Following is the syntax.

interface Sample1{
void test1(int a);
}
interface Sample2 extends Sample1{
void test2();
}

Interface and inheritance

shape Description

A class can implement more than one interface at a time. In this case, the class has to provide definition for all the methods of each interface. A class can inherit from another class and implements more than one interface.

For more detailed overview on inheritance click here .

shape Syntax


Class DerivedClass extends Baseclass implements Interface1,Interface2
{
}

shape Example

package com.spl.concrete;

public class Interface3 {
	public static void main(String[] args) {
		System.out.println("Program starts");
		B dref=new B();
		dref.test1();
		System.out.println("Program ends");

	}
}
class BaseClass
{
	public void test1()
	{
		System.out.println("Running test1() mehod");
	}
}
interface Sample1
{
	void test1();
}
class B extends BaseClass implements Sample1
{
	//inheriting from both Sample1 and BaseClass
}

In the above example, the class B is becoming concrete because the test 1() method inherited from base class is full filling the contract of Sample 6 interface.
Output:

Program starts
Running test1() method
Program ends

shape Example

package com.spl.int4;

public class Interface4 {
	public static void main(String[] args) {
		System.out.println("Program starts");
		E dref=new E();
		dref.test1();
		System.out.println("Program ends");

	}
}
interface Sample7
{
	void test1();
}
interface Sample8
{
	void test1();
}
class E implements Sample7,Sample8
{

	@Override
	public void test1() {
		// TODO Auto-generated method stub
		System.out.println("Running test1() method");
	}

}

In the above example, the class is becoming concrete by overriding only test1() method.The overrided method fulfills the contract of both Sample 7 & Sample 8 interface,because the method signature are same.
Output:
When compile the code output will be as follows.

Program starts
Running test1() method
Program ends

Multiple Inheritance By Using Interface

The following is an example to achieve multiple inheritance by using interface.

package com.splessons;

interface Printable{  
void print();  
}  
interface Showable{  
void show();  
}  
class Splesson implements Printable,Showable{  
public void print(){System.out.println("Hello");}  
public void show(){System.out.println("Welcome To Splessons");}  
  
public static void main(String args[]){  
Splesson obj = new Splesson();  
obj.print();  
obj.show();  
 }  
}  

In the above example, Printable and Showable are the two interfaces that have their own methods and where Splesson is the class name that implements two interfaces(there is no ambiguity).
Output:

Hello
Welcome To Splessons

Advantages of interfaces

shape Points

The following are the some advantages of interfaces.

Summary

shape Key Points

  • Multiple inheritance problem will be done by interface.
  • 100% of abstraction will be achieved through an interface.