Abstraction in Java

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 12

Abstraction in Java

Abstraction is a process of hiding the implementation details and showing only


functionality to the user.

Another way, it shows only essential things to the user and hides the internal
details, for example, sending SMS where you type the text and send the message.
You don't know the internal processing about the message delivery.

A practical example of abstraction can be motorbike brakes. We know what brake


does. When we apply the brake, the motorbike will stop. However, the working of
the brake is kept hidden from us.

Advantages of Abstraction

 It reduces the complexity of viewing the things.


 Avoids code duplication and increases reusability.
 Helps to increase security of an application or program as only important
details are provided to the user.

Ways to achieve Abstraction

There are two ways to achieve abstraction in java

1. Abstract class (0 to 100%)


2. Interface (100%)

Abstract Modifier:

Abstract is the modifier applicable only for methods and classes but not for
variables.
Abstract class:

A class which is declared as abstract is known as an abstract class. It can have


abstract and non-abstract methods. It needs to be extended and its method
implemented. It cannot be instantiated.

For any java class if we are not allow to create an object such type of class we have
to declare with abstract modifier that is for abstract class instantiation is not
possible.

 An abstract class must be declared with an abstract keyword.

Example of abstract class

abstract class A{
}
 It can have abstract and non-abstract methods.
 It cannot be instantiated.

Example:

abstract class Test{


public static void main(String args[]){
Test t=new Test();
}}
Output:
Compile time error.
D:\Java>javac Test.java
Test.java:4: Test is abstract; cannot be instantiated Test t=new Test();
 It can have constructors and static methods also.
 It can have final methods which will force the subclass not to change the
body of the method.

Abstract Methods:

 Even though we don't have implementation still we can declare a method


with abstract modifier.
 That is abstract methods have only declaration but not implementation.
 Hence abstract method declaration should compulsory ends with semicolon.

Ex:

Class Test{

public abstract void m1();//valid

public abstract void m1(){

System.out.println(“providing implementation”);//invalid

Child classes are responsible to provide implementation for parent class abstract
methods.

Ex:
Ex-2:-

File: TestBank.java

abstract class Bank{


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

 The main advantage of abstract methods is , by declaring abstract method in


parent class we can provide guide lines to the child class such that which
methods they should compulsory implement.
 Abstract method never talks about implementation whereas if any modifier
talks about implementation then the modifier will be enemy to abstract and
that is always illegal combination for methods.

The following are the various illegal combinations for methods.

Diagram:
All the 6 combinations are illegal

Abstract class:

For any java class if we are not allow to create an object such type of class we have
to declare with abstract modifier that is for abstract class instantiation is not
possible.

Example:

abstract class Test{

public static void main(String args[]){

Test t=new Test();

}}
Output:

Compile time error.

D:\Java>javac Test.java

Test.java:4: Test is abstract; cannot be instantiated Test t=new Test();

Abstract class having constructor, data member and methods:

An abstract class can have a data member, abstract method, method body (non-
abstract method), constructor, and even main() method.

File: TestAbstraction2.java

//Example of an abstract class that has abstract and non-abstract methods


abstract class Bike{
Bike(){
System.out.println("bike is created");//constructor
}
abstract void run(); //abstract method
void changeGear(){
System.out.println("gear changed");//concret method
}
}
//Creating a Child class which inherits Abstract class
class Honda extends Bike{
void run(){
System.out.println("running safely..");
}
}
//Creating a Test class which calls abstract and non-abstract methods
class TestAbstraction2{
public static void main(String args[]){
Bike obj = new Honda();
obj.run();
obj.changeGear();
}
}
Output:-
bike is created
running safely..
gear changed

What is the difference between abstract class and abstract method?

 If a class contain at least on abstract method then compulsory the


corresponding class should be declare with abstract modifier. Because
implementation is not complete and hence we can't create object of that
class.
 Even though class doesn't contain any abstract methods still we can declare
the class as abstract that is an abstract class can contain zero no of abstract
methods also.

Rule: If there is an abstract method in a class, that class must be abstract.

Ex:-
class Bike{
abstract void run();
}
Output:
Compile time error.
Example2:

class Parent{

public void m1();

Output:

Compile time error.

D:\Java>javac Parent.java

Parent.java:3: missing method body, or declare abstract

public void m1();

Example3:

class Parent{

public abstract void m1(){}

Output:

Compile time error.

Parent.java:3: abstract methods cannot have a body


public abstract void m1(){}

Example3:

class Parent{

public abstract void m1();

Output:

Compile time error.

D:\Java>javac Parent.java

Parent.java:1: Parent is not abstract and does not override abstract method
methodOne() in Parent

class Parent

Note: If a class extends any abstract class then compulsory we should provide
implementation for every abstract method of the parent class otherwise we have to
declare child class as abstract.

Example:

abstract class Parent{


public abstract void m1();
public abstract void m2();
}
class child extends Parent{
public void m1(){}
}
Output:
Compile time error.
D:\Java>javac Parent.java
Parent.java:6: child is not abstract and does not override abstract method m2() in
Parent class child extends Parent

If we declare class child as abstract then the code compiles fine but child of child is
responsible to provide implementation for m2().

What is the difference between final and abstract?

 For abstract methods compulsory we should override in the child class to


provide implementation. Whereas for final methods we can't override hence
abstract final combination is illegal for methods.
 For abstract classes we should compulsory create child class to provide
implementation whereas for final class we can't create child class. Hence
final abstract combination is illegal for classes.
 Final class cannot contain abstract methods whereas abstract class can
contain final method.
Note:

Usage of abstract methods, abstract classes and interfaces is always good


Programming practice.

You might also like