Abstraction and interface in java

Table of contents

Abstraction

'abstract' is a non-access modifier in Java for classes and methods but not variables.

Characteristic of abstract keyword :

  • Can't create an object directly. You need to extend it by some other class.

  • The abstract method does not have a body; a subclass needs to override it.

  • It can have both abstract and normal(concrete) abstract methods.

  • it can have constructors.

  • it can have instance variables.

  • it can implement interfaces.

An example to demonstrate the use of the 'abstract' keyword.

//define an abstrcat class
abstract class A{
abstract void m1(); //abstract method has no body
//normal methods are also allowed in abstract class
public void m2(){
System.out.println("this is an normal method");
};
}
//normal class B 
class B extends A{
    //class B must override method of Aotherwise compile time error would be shown
    void m1(){
        System.out.println("B's implementation of m1");
}
}
//test class
public class Main {
    public static void main (String[] args) {
        B b=new B();
        b.m1();
        b.m2();
            }
}

Note: it's compulsory to override your method otherwise you would get a compile-time error. Think of this as a rule.

Interfaces

Interfaces are similar to classes in many ways. Interfaces can be used to achieve 100% abstraction.

  • there can only be abstract methods (which means "no content").

  • Can help in achieving multiple inheritance.

  • class extends class; class implements Interface; interface extends interface.

  • Updates -> Since Java 8 we can have default and static methods.

    -> Science Java 9 we can have private methods in an interface.

    An example to demonstrate the use of the 'Interface' keyword.

      interface Bank
      {
    
          float rateOfInterest ();
    
      }
      class SBI implements Bank
      {
    
          public float rateOfInterest ()
          {
              return 9.15f;
          }
    
      }
    
      class PNB implements Bank
      {
    
          public float rateOfInterest ()
          {
              return 9.7f;
          }
    
      }
    
      class TestInterface2
      {
    
          public static void main (String[]args)
          {
    
              Bank b = new SBI ();
    
              System.out.println ("ROI: " + b.rateOfInterest ());
    
         }
      }
    

    PS:- Modifiers are specific keywords present in Java using which we can make changes to the characteristics of a variable, method, or class and limit its scope.

    ->Modifiers in Java are divided into two types – Access Modifiers and Non-Access modifiers.

    ->Non-access modifiers provide information about the characteristics of a class, method, or variable to the JVM. ‘static’ , ‘abstract’, ‘final’ , ‘synchronized’ are some examples of non-access modifiers.

  • ->Access Modifiers - 1. public (member can be accessed by any other code) 2. private (member can only be accessed by other members of its class) 3. default (no modifier) 4. protected (allows a member (method, variable, or constructor) to be accessed within its own class, by its subclasses, and by other classes within the same package)

To the readers : If there is any further scope of improvement, then kindly bring to my attention. :)

0
Subscribe to my newsletter

Read articles from Pratik Kumar Jha directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Pratik Kumar Jha
Pratik Kumar Jha