Abstraction in Java (Part 7)
Pre-requisites : Inheritance , Upcasting , Method Overridding
Java Abstraction
Abstraction is the process of hiding implementation details and only showing essential information to the user.
In other words only outer information is provided to the user while internal details is hidden.
Abstraction mainly focuses on what the object does rather than how it is done.
There are two ways to achieve abstraction:
1. Abstract Classes (Provides 0 to 100% abstraction)
2. Interface (Provides 100% abstraction)
A class which is declared with an abstract keyword is called as an Abstract Class. It can have both abstract methods and non-abstract methods. To access its methods it must be inherited. Abstract Classes cannot be instantiated i.e its objects cannot be created.
Rules For Creating Abstract Classes :
- An abstract class must be declared with an abstract keyword.
- To access an Abstract Class , it must be inherited and forcefully override its abstract methods.
- It can have abstract and non-abstract methods.
- It cannot be instantiated.
- It can have constructors and static methods and final methods too.
Syntax:-
abstractclassClass_Name{//abstract / non-abstract methods}
Abstract Methods
Abstract Methods does not contain any body. As they are to be overridden their body is implemented according to user's need.
If there is an Abstract Method in a class then that class must be declared as Abstract Class.
Syntax:-
abstractreturnTypeMethod_Name();
Example of Abstract Class:-
//creating an abstract classabstractclassCar{//creating an abstract methodabstractvoidrun();//a regular methodvoidchangeGear() { System.out.println("Gear Changing !"); } }/* To access methods of abstract class * we must inherit abstract class */classHonda extends Car{//@Overriding parent methodvoidrun() { System.out.println("Car Running vrooommm...."); } } public classMyClass{ public static void main(String[] args) {//upcastingCarcar=new Honda();car.run();car.changeGear();//non-abstract methods are not forcibly overridden} }
Output:-
Car Running vrooommm....
Gear Changing ! Another Example:-
//creating an abstract classabstractclassBank{privateStringbankName;// creating an abstract methodabstractintgetRateOfInterest();// a regular methodpublic StringgetBankName() {returnbankName; } }/* * To access methods of abstract class we must inherit abstract class */classAXIS extends Bank{// @Override parent methodintgetRateOfInterest() {return8; } public StringgetBankName() {return"AXIS BANK"; } } classPNB extends Bank{// @Override parent methodintgetRateOfInterest() {return9; } public StringgetBankName() {return"PNB BANK"; } } public classMyClass{ public static void main(String[] args) {// upcastingBankb1= new AXIS(); System.out.println("Bank Name:" +b1.getBankName()); System.out.println("Rate Of Interest of AXIS bank:"+ b1.getRateOfInterest() + "%");// upcastingBankb2= new PNB(); System.out.println("Bank Name:" +b2.getBankName()); System.out.println("Rate Of Interest of PNB bank:"+b2.getRateOfInterest() + "%"); } }
Output:-
Bank Name: AXIS BANK
Rate Of Interest of AXIS bank: 8%
Bank Name: PNB BANK
Rate Of Interest of PNB bank: 9%
We will learn about Interface in upcoming tutorials.

