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:-
abstract
class
Class_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:-
abstract
returnType
Method_Name
();
Example of Abstract Class:-
//creating an abstract class
abstract
class
Car
{
//creating an abstract method
abstract
void
run
();
//a regular method
void
changeGear
() { System.out.println("
Gear Changing !
"); } }
/* To access methods of abstract class * we must inherit abstract class */
class
Honda extends Car
{
//@Overriding parent method
void
run
() { System.out.println("
Car Running vrooommm....
"); } } public class
MyClass
{ public static void main(String[] args) {
//upcasting
Car
car
=new Honda();
car
.
run
();
car
.
changeGear
();
//non-abstract methods are not forcibly overridden
} }
Output:-
Car Running vrooommm....
Gear Changing !
Another Example:-
//creating an abstract class
abstract
class
Bank
{
private
String
bankName
;
// creating an abstract method
abstract
int
getRateOfInterest
();
// a regular method
public String
getBankName
() {
return
bankName
; } }
/* * To access methods of abstract class we must inherit abstract class */
class
AXIS extends Bank
{
// @Override parent method
int
getRateOfInterest
() {
return
8; } public String
getBankName
() {
return
"
AXIS BANK
"; } } class
PNB extends Bank
{
// @Override parent method
int
getRateOfInterest
() {
return
9; } public String
getBankName
() {
return
"
PNB BANK
"; } } public class
MyClass
{ public static void main(String[] args) {
// upcasting
Bank
b1
= new AXIS(); System.out.println("
Bank Name:
" +
b1
.
getBankName
()); System.out.println("
Rate Of Interest of AXIS bank:
"+ b1.getRateOfInterest() + "
%
");
// upcasting
Bank
b2
= 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.