Abstract class and keywords in JAVA

Java Object oriented programmi Published on 15 February 2020

As the name suggests Abstract means something not complete, Having the only outline(like any painting outline which is not complete but have some information).


An abstract class is a kind of class that you can’t create a direct object of them. It can’t be instantiated by itself but it can be subclassed.


In java, By applying abstract keyword abstract class is created. Abstract keyword is used for methods & classes and it is a non-access modifier.


Abstract class


When we use abstract keyword with the class it restricted that class can’t be used to create an object (to use it, it must be inherited from another class).


syntax: abstract class Example_abstract { }  


Abstract method


When we use abstract keyword with any method it is restricted that method can only be used within an abstract class, and the method does not have a body(No implementation). The body is given by the inherited class.


syntax:  abstract String logPrinter ();


Why we use abstract class..?


It is used to achieve oops one of the important features Abstraction. Abstraction is hiding the Business Logic (important implementation) from the end-user and giving only functionality for use to the end-user. 


There are many ways to achieve abstraction in java.


 1.By using Abstract class

 2.By using interface 


A real-time example of abstract class


 // Abstract class

abstract class LogPrinter

{

   // No body

  abstract void logPrinter ();

   // With body

  public void bodyMetheod ()

  {

    System.out.println ("Call body Method.");

  }

}



 // Class A 

class A extends LogPrinter

{

  void logPrinter ()

  {

    System.out.print ("Print Log class A");

  }

}


 //class B

class B extends LogPrinter

{

  void logPrinter ()

  {

    System.out.print ("Print Log Class B");

  }

}


// Main Class

public class Main

{

public void static main(String s...) {

// Making the object of class A

A a=new A();

a.logPrinter();// Metheod Call

// Making the object of class B

B b=new B();

b.logPrinter();// Metheod Call

// Making object by reference 

LogPrinter ref_a=new A();

ref_a.bodyMetheod();// Metheod Call

}

}


Output:

Print Log class A                                                                                                                              

Print Log Class B                                                                                                                              

Call body Method


Some Important points on Abstract Class.


  • The abstract class has a constructor. While creating the base class object It is called.
  • We can create an abstract class without abstract methods.
  • An abstract class can also have final methods.


Abstract with final


It is a very famous question asked what if an abstract class with the final..? If a class is declared final and abstract It becomes useless because the abstract totally depends on oops feature inheritance and final is use to preventing inheritance.





Know More:


What is Garbage Collection ?

Follow our page JAVA Object oriented programming Language, where you can find every topic related to java