Monday, June 1, 2020

Interface and Abstract Class

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
  1. It provides security
  2. 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
  • Use interface, when you have multiple classes having the same method name but implementation is different from one subclass to another sub class
  • if we do not know anything about implementation , just we have  requirement specification then we should go for interface
  •  interface methods are abstract and public by default (100 % abstract class)
  • we can not declare interface method with the following modifiers:
  1. public>>>>> private, protected
  2. abstract>>>> final, static, synchronized, native, strictfp
  •  variables present inside interface are  public, static, and final whether we are declaring or not
  • we can not declare interface variables with the following modifiers: private, protected, transient, volatile
  • interface variables must be initialized at the time of declaration
  • it is not possible to declare instance and static blocks inside the interface
  • it is not possible to write constructor inside the abstract class
  • we can achieve the multiple inheritance

  • Use abstract class, when you have multiple classes having the same method name but only partial implementation is common. If you feel implementation is different in sub class, declare them as abstract method so that sub classes can only provide implementation for the abstract method and other methods can be reused from super class.
  • if we are talking about implementation but not completely (partial implementation) then we should go for abstract class
  • abstract methods need not be public and abstract. Abstract class may or may not contain abstract method (partially abstract)
  • there are no restrictions on abstract class method modifiers
  • variables present in abstract class need not be public, static, and final.
  • there is no restriction of modifiers on abstract class variable
  • it is not mandatory to perform initialization of abstract variable at the time of declaration
  • it is possible to declare instance and static block inside the abstract class
  • it is possible to write constructor inside the abstract class
  • we can not achieve the multiple inheritance
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