Contents of page :
- Features of Interface in java >
- Let’s write Interface to show default additions done by compiler >
- Program 1 - Let’s write Interface and also create a concrete class that will implement interface and override its method
- Program 2 - Multiple inheritance using interface >
- Now comes the very important question that why java allows multiple inheritance using interface but not by using classes?
- Example why interface allow multiple inheritance- Program 2.1
- Example why classes doesn’t allow multiple inheritance- Program 2.2
- Marker interfaces
- Program 3 - If any new method is added in Interface then all concrete classes which implements that interface must provide implementation of newly added method
- When to use abstract class or interface practically - Choosing between interface and abstract class>
- When to use interface practically - Program 4.1
- When to use abstract class practically - Program 4.2
Features of Interface in java >
- Pure abstraction - Interfaces helps in achieving pure abstraction in java. Interface are purely abstract in java. Interfaces can only have abstract methods.
- All Interface are abstract by default. So, it’s not mandatory to write abstract keyword with interface.
- Multiple inheritance - Interface allows us to achieve multiple inheritance as well.
- Interface always extends another interface.
- Interface can extend more than one interface.
- methods in interface are public and abstract by default.
- Class always implements interface.
- Interface doesn’t extend classes.
- If any new method is added in Interface then all concrete classes which implements that interface must provide implementation of newly added method, because all methods in interface are abstract by default.
- Interface methods cannot have declare abstract methods as synchronized concrete class which implements interface can make methods synchronized.
Let’s write Interface to show default additions done by compiler >
interface MyInterface { //compiler will add abstract
int i=2; //compiler will add public, static and final
void m(); //compiler will add public and abstract
}
|
Because of default additions done by compiler, writing above code will be same as writing below code-
abstract interface MyInterface {
public static final int i=2;
public abstract void m();
}
|
Program 1 - Let’s write Interface and also create a concrete class that will implement interface and override its method
Q. What are concrete classes?
A. Non-abstract classes are known as concrete classes.
interface MyInterface {
int i=2;
void m1();
void m2();
}
/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */
public class MyClass implements MyInterface{
public static void main(String[] args) {
MyInterface obj=new MyClass();
obj.m1();
obj.m2();
System.out.println("MyInterface's i = "+i);
}
@Override
public void m1(){
System.out.println("in m1()");
}
@Override
public void m2(){
System.out.println("in m2()");
}
}
/*OUTPUT
in m1()
in m2()
2
*/
|
Program 2 - Multiple inheritance using interface >
MyClass implements two interfaces and provides implementation to the methods of both interface.
interface MyInterface1 {
void m();
}
interface MyInterface2 {
void n();
}
/**
* Copyright (c), AnkitMittal JavaMadeSoEasy.com
* Class implements two interfaces and provides implementation to the
* methods of both interface.
*/
public class MyClass implements MyInterface1, MyInterface2{
public static void main(String[] args) {
MyClass obj=new MyClass();
obj.m();
obj.n();
}
@Override
public void m(){
System.out.println("in implementation of MyInterface1's m()");
}
@Override
public void n(){
System.out.println("in implementation of MyInterface2's n()");
}
}
/*OUTPUT
in implementation of MyInterface1's m()
in implementation of MyInterface2's n()
*/
|
Now comes the very important question that why java allows multiple inheritance using interface but not by using classes?
Because Interfaces are purely abstract in java.
Example why interface allow multiple inheritance-
Program 2.1 - Let’s say there are two interfaces MyInterface1 and MyInterface2 having method with same name [ m() ] and some other class extends both of them, then no ambiguity will be caused, because m() is abstract method in both interfaces and their implementation will be provided by implementing class.
interface MyInterface1 {
void m();
}
interface MyInterface2 {
void m();
}
public class MyClass implements MyInterface1, MyInterface2{
public static void main(String[] args) {
MyClass obj=new MyClass();
obj.m();
}
@Override
public void m(){
System.out.println("in m()");
}
}
|
Example why classes doesn’t allow multiple inheritance-
Program 2.2 -Let’s say there are two classes MyClass1 and MyClass2 having method with same name [m() ] and some other class extends both of them, then ambiguity will be caused, because m() is non-abstract method in both classes and they have different implementations.
class MyClass1 {
void m(){
System.out.println("MyClass1 - m()");
}
}
class MyClass2 {
void m(){
System.out.println("MyClass2 - m()");
}
}
|
Marker interfaces
Marker interfaces are those interfaces which does not have any methods or any member variables.
Marker interfaces are also known as tagged interfaces.
Now, I will like to elaborate 11th point >
Program 3 - If any new method is added in Interface then all concrete classes which implements that interface must provide implementation of newly added method, because all methods in interface are abstract by default.
interface MyInterface {
void m1();
void m2();
}
/*
* abstract class
*/
abstract class MyAbstractClass implements MyInterface {
@Override
public void m1(){
System.out.println("in m1()");
}
}
/*
* concrete class
*/
class MyClass extends MyAbstractClass {
@Override
public void m2(){
System.out.println("in m2()");
}
}
|
MyAbstractClass implements MyInterface and does not provide implementation of m2(). Than,
MyClass extends MyAbstractClass (MyClass indirectly implements MyInterface) and provides implementation of unimplemented method m2().
But, if concrete class MyClass implements interface directly than it must provide implementation of all methods(As done in Program 1)
When to use abstract class or interface practically - Choosing between interface and abstract class>
When to use interface practically -
Let’s say we have to choose between class or interface for Animals, than habitat of animals might be land or water.
And food of all animals might be different.
So, we will create interface with -
abstract method = habitat() [because animals might be living on land or water]
abstract method = food() [because food of all animals might be different]
Program 4.1 - use interface practically
/*
* interface
*/
interface Animals {
abstract void habitat();
abstract void food();
}
/*
* concrete class - Lion
*/
class Lion implements Animals {
@Override
public void habitat(){
System.out.println("Habitat of Lion is land");
}
@Override
public void food(){
System.out.println("Lion eat - flesh");
}
}
/*
* concrete class - Lion
*/
class Whale implements Animals {
@Override
public void habitat(){
System.out.println("Habitat of Whale is water");
}
@Override
public void food(){
System.out.println("Whale eat - aquatic animals");
}
}
public class MyClass{
public static void main(String[] args) {
Animals lion=new Lion();
lion.habitat();
lion.food();
System.out.println();
Animals whale=new Whale();
whale.habitat();
whale.food();
}
}
/*OUTPUT
Habitat of Lion is land
Lion eat - flesh
Habitat of Whale is water
Whale eat - aquatic animals
*/
|
When to use abstract class practically -
Let’s say we have to choose between class or interface for TerrestrialAnimals, than one thing will be for sure that habitat of all Terrestrial animals must be land. That means we can have same implementation of habitat method for all Terrestrial animals. And food of all Terrestrial animals might be different.
So, we will create abstract class with -
instance method = habitat() [because all Terrestrial animals live on land]
abstract method = food() [because food of all Terrestrial animals might be different]
instance method habitat() will be inherited in all subclasses.
Program 4.2 - use abstract class practically
/*
* abstract class
*/
abstract class TerrestrialAnimals {
void habitat(){
System.out.println("Habitat of Terrestrial animal is land");
}
abstract void food();
}
/*
* concrete class - Lion
*/
class Lion extends TerrestrialAnimals {
@Override
void food(){
System.out.println("Lion eat - flesh");
}
}
/*
* concrete class - Lion
*/
class Goat extends TerrestrialAnimals {
@Override
void food(){
System.out.println("Goat eat - grass");
}
}
public class MyClass{
public static void main(String[] args) {
TerrestrialAnimals lion=new Lion();
lion.habitat();
lion.food();
System.out.println();
TerrestrialAnimals goat=new Goat();
goat.habitat();
goat.food();
}
}
/*OUTPUT
Habitat of Terrestrial animal is land
Lion eat - flesh
Habitat of Terrestrial animal is land
Goat eat - grass
*/
|
RELATED LINKS>
Abstract class in java - When to use abstract class or interface practically, 10 features
10 Differences between Interface and abstract class in java - in detail with programs
Labels:
Core Java