Java: Abstract Class


Abstraction in Java

Abstraction is a concept to show only essential detail i.e hide internal detail and show functionality in simple terms. To achieve abstraction in Java, use abstract class or interface.

Example: In ATM we don’t know how internally work. In a Car don’t know about the internal mechanism.

Ways to achieve Abstraction

We can achieve abstraction in two ways:

  1. Abstract class: abstraction (0 to 100%)\
  2. Interface: abstraction (100%)

Abstract class in Java

A class that is declared with the keyword abstract is known as an abstract class in Java. It can have abstract and concrete methods (method with the body). An abstract class can not be instantiated but need to extend to implement abstract methods.

abstract class ClassA{}  

Points to Remember

  • Abstract class must be declared with the keyword abstract.
  • Abstract class can have abstract and non-abstract methods.
  • Abstract class cannot be instantiated.
  • Abstract class can have constructors and static methods also.
  • Abstract class can have final methods that will force the subclass not to change the body of the method.
  • Abstract class can have a data member, abstract method, method body (non-abstract method), constructor, and even main() method.

Abstract Method in Java

A class method that is declared with keyword abstract and not having any implementation is known as an abstract method.

For Example

abstract class ClassA{
//no method body and abstract
abstract void printStatus(); 
}  

Note:

  • Rule 1: If a class having one or more abstract method then make to class as an abstract class by declaring with keyword abstract.
  • Rule 2: If a class is extending with an abstract class that has an abstract method, then the subclass must either need to make an abstract class or provide the implementation of the method.
  • Rule 3: The abstract class can also be used to provide some implementation of the interface. In this case, the programmer may not be forced to override all the methods of the interface.

Example of an abstract method

Let’s have a real scenario, Consider a group of vehicles like Bike, Bus, Car and Truck, and each vehicle has similarities like fuel amount, capacity, brakes, etc.

For this example, define Vehicle class as abstract with this method declaration as engine() because are similarities with all vehicles. Every vehicle has different ways to implement it so write an implementation of these methods in respective classes.

abstract class Vehicle{
  abstract void engine();
}

class Car extends Vehicle{
void engine(){
System.out.println("Car Engine is running safely");
} 

class Bike extends Vehicle{
void engine(){
System.out.println("Bike Engine is running safely");
} 

class Truck extends Vehicle{
void engine(){
System.out.println("Truck Engine is running safely");
} 

class Bus extends Vehicle{
void engine(){
System.out.println("Bus Engine is running safely");
}
} 

public TestAbstractClass
{
  public static void main(String[]args)
  {
    Vehicle v=new Car();
	v.engine();
	v=new Bike();
	v.engine();
	v=new Truck();
	v.engine();
	v=new Bus();
	v.engine();
  }
}

Output

Car Engine is running safely
Bike Engine is running safely
Truck Engine is running safely
Bus Engine is running safely

Mostly, we don’t know about the implementation class (which is hidden to the end-user), and an object of the implementation class is provided by the factory method.

abstract class Bank{
abstract int getRateOfInterest();
}
class SBI extends Bank{
int getRateOfInterest(){return 8;}
}
class ICICI extends Bank{
int getRateOfInterest(){return 9;}
}    

class TestBankDetail{
public static void main(String args[]){
Bank b;
b=new SBI();
System.out.println("SBI Rate of Interest is: "+b.getRateOfInterest()+" %");
b=new PNB();
System.out.println("ICICI Rate of Interest is: "+b.getRateOfInterest()+" %");
}}

Output

SBI Rate of Interest is: 8 %
ICICI Rate of Interest is: 9 %
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s