Interface: Extension of abstract class is called interface. An interface can have methods and variables just like the class but the methods declared in interface are by default abstract and public. Also, the variables declared in an interface are public, static, and final.
Advantage: Each sub class can define the functionality on their own way
- It provides security
- helps you to use multiple inheritance in java
- 100 % abstract
- use as a contract
- interface in java is a blueprint of a class
- has method name but no definition
- implement multiple interface
- does not contain any constructor
Differences between interface and abstract class
interface
|
abstract class
|
|
|
Real life example showing interface concept:
package com.cerotid.interfaceconcept;
public interface BankInterface {
void deposit();
void withdraw();
void calcualteInterest();
}
package com.cerotid.interfaceconcept;
public class InterfaceDemo {
public static void main(String[] args) {
BankInterface bi1 = new ChaseBank();
bi1.deposit();
bi1.withdraw();
bi1.calcualteInterest();
BankInterface bi2 = new SuntrustBank();
bi2.deposit();
bi2.withdraw();
bi2.calcualteInterest();
BankInterface bi3 = new SBIBank();
bi3.deposit();
bi3.withdraw();
bi3.calcualteInterest();
}
}
class ChaseBank implements BankInterface{
@Override
public void deposit() {
System.out.println(" ChaseBank deposit implementation");
// TODO Auto-generated method stub
}
@Override
public void withdraw() {
System.out.println(" ChaseBank withdraw implementation");
// TODO Auto-generated method stub
}
@Override
public void calcualteInterest() {
System.out.println("ChaseBank calcualte interest implementation");
// TODO Auto-generated method stub
}
}
class SuntrustBank implements BankInterface{
@Override
public void deposit() {
System.out.println("Suntrust bank deposit implementation");
// TODO Auto-generated method stub
}
@Override
public void withdraw() {
System.out.println("Suntrust bank withdraw implementation");
// TODO Auto-generated method stub
}
@Override
public void calcualteInterest() {
System.out.println("Suntrust bank calcualte interest implementation");
// TODO Auto-generated method stub
}
}
class SBIBank implements BankInterface{
@Override
public void deposit() {
System.out.println("SBI bank deposit implementation");
// TODO Auto-generated method stub
}
@Override
public void withdraw() {
System.out.println("SBI bank withdraw implementation");
// TODO Auto-generated method stub
}
@Override
public void calcualteInterest() {
System.out.println("SBI bank calculate interest implementation");
// TODO Auto-generated method stub
}
}
Output:
ChaseBank deposit implementation
ChaseBank withdraw implementation
ChaseBank calcualte interest implementation
Suntrust bank deposit implementation
Suntrust bank withdraw implementation
Suntrust bank calcualte interest implementation
SBI bank deposit implementation
SBI bank withdraw implementation
SBI bank calculate interest implementation
Notes:
class extends class
interface extends interface
class implements interface
class A extends B.............valid
class A extends B, C...........invalid
class A implements It1..........valid
class A implements It1, It2........valid
class A extends A....................invalid
interface It1 extends It2..............valid
interface It1 extends It2, It3..........valid
interface It1 extends A...................invalid
interface It1 extends It1 .................invalid
class A extends B implements It1, It2.............valid
class implements It1, It2 extends B.................invalid
(extends keyword must be the first keyword)
Example showing interface:
public interface It1 {
void m1();
void m2();
void m3();
}
public class Test2 implements It1 {
public void m1() {
System.out.println("m1 method");
}
public void m2() {
System.out.println("m2 method");
}
public void m3() {
System.out.println("m3 method");
}
public static void main(String[] args) {
Test2 t2 = new Test2();
t2.m1();
t2.m2();
t2.m3();
It1 i = new Test2();
i.m1();
i.m2();
i.m3();
}
}
Output:
m1 method
m2 method
m3 method
m1 method
m2 method
m3 method
No comments:
Post a Comment