Abstract Class in Java - Javastudypoint

Sunday, December 30, 2018

Abstract Class in Java

Before discussing Abstract Class in Java. We can recall you in C++ if a class has at least one pure virtual function then the class becomes an abstract class. We can make an abstract class in C++ with the help of the virtual keyword. But in java, there is no virtual Keyword.


In Java, we can create an abstract class with the help of the abstract keyword. Like C++, in Java, an abstract class cannot be instantiated (instance cannot be created). But we can create a reference variable of an abstract class. 

Syntax:   abstract class class_name {    }
Abstract Class in Java

Important points about Java Abstract class:

  • We can create an abstract class with the help of the abstract keyword.
  • We cannot instantiate an abstract class. But we can create a reference for an abstract class.
  • Abstract class contains abstract and non-abstract methods.
  • If a class has an abstract method then it must be declared that class as abstract.
  • An abstract class contains constructor.
  • An abstract class also have final methods. (methods can't be overridden).
  • An abstract class is a way to achieve the abstraction (not 100 percent). Abstraction is an OOPs concept that is used to hide the implementation details from the user.

Abstract Methods in Java:

  • An abstract class can include methods that contain no implementations (methods without body) is known as abstract methods in java.
  • Like an abstract class, abstract methods must be declared with abstract keyword.
  • The abstract methods declaration must end with a semicolon rather than a block.
  • Abstract methods must be created and declared in an abstract class. 
  • Abstract methods don't have method implementation (method body) it has only method signature.
Example of abstract method:  abstract void showMsg();  // No body

Why we use Abstract Class?

Let's say we have a class Bank that has a method getRateOfInterest() and subclasses of it like OBC, SBI, and PNB. Since the Bank interest different from one bank to another bank, there is no point to implement this method in Parent class (Bank class). This is because every child class ( like OBC class, SBI class etc.) must override this method to give its own implementation details, like SBI class will say "SBI has 7 percent interest" and OBC class will say "OBC has 8 percent interest". 



So when we know that all the Bank child classes (SBI, OBC, and PNB etc.) will and should override this method, then there is no point to implement this method in the parent class (Bank class). Thus, making this method abstract would be a good practice. 

Now each Bank must have a rateOfInterest, by making this method abstract we made it compulsory to the child class (SBI, OBC etc.) to give implementation details to this method. That's why we use the Abstract Class
Let's understand this by a real world scenario Bank Example:


Example of Abstract class in Java:


abstract class Bank{
 abstract int getRateOfInterest();
}
 class OBC extends Bank{
  int getRateOfInterest(){
   return 7;
  }
 }
 class PNB extends Bank{
  int getRateOfInterest(){
   return 8;
  }
 }
 class SBI extends Bank{
  int getRateOfInterest(){
   return 9;
  }
 }
 class Example{
  public static void main(String args[]){
   Bank obj;
   obj = new OBC();
   System.out.println("OBC Rate of interest is: " +obj.getRateOfInterest()+ "%");
   obj = new PNB();
   System.out.println("PNB Rate of interest is: " +obj.getRateOfInterest()+ "%");
   obj = new SBI();
   System.out.println("SBI Rate of interest is: " +obj.getRateOfInterest()+ "%");
  }
 }

OutputAbstract class in java

Example of Abstract class with Constructor:

Like any other class, an abstract class has a constructor, data members, and methods.

abstract class Car{
 Car(){
  System.out.println("Car is created");
 }
 abstract void running();
 void changeColour(){
  System.out.println("Car colour is changed");
 }
}
class Maruti extends Car{
 void running(){
  System.out.println("Car is running safely");
 }
}
class Practical{
 public static void main(String args []){
  Maruti obj = new Maruti();
  obj.running();
  obj.changeColour();
 }
}

Output:
Abstract class in java


Example of Abstract Class without abstract method:

Abstract class contains a non-abstract method (normal method) with method definition.

abstract class Car{
 public void running(){
  System.out.println("Car is running");
 }
}
class Practical extends Car{
 public void marutiRunning(){
  System.out.println("Maruti car is running");
 }
 public static void main(String args[]){
  Practical obj = new Practical();
  obj.running();
  obj.marutiRunning();
 }
}

Output: 
Abstract class in java



You may also like:
Inheritance in Java with Example
What are Java Beans?
Java Authentication and Authorization Service - JAAS




No comments:

Post a Comment