In this article Abstraction in Java we give the information about Abstract Class shows only the necessary information to the user and the background details are hidden.

Abstraction in Java:

Abstraction is a process in which the implementation is hidden and only the functionality is shown to the user.

In other words, “It shows only the necessary information to the user and the background details are hidden.”

For example: – We type the message in WhatsApp and send it. But we do not know about its internal working and how the message is sent.

Important terms for abstraction:

  • Abstraction is always necessary to declare abstract class with abstract keyword.
  • Abstraction can have abstract and non-abstract methods.
  • It cannot be instantiated, that is, it cannot be used to create objects.
  • It can also have constructors and static methods.
  • To use an abstract class, we have to inherit it from another class and provide implementation of abstract methods.
  • It may also have final methods.
  • Abstract methods do not have a body.

How to Achieve Abstraction in Java:

There are two ways to achieve abstraction in Java.

  1. By using Abstract class
  2. By using the interface

Interface provides us 100% abstraction i.e. by using Interface we can abstract all the method implementations. In simple words, all the methods inside the interface are abstract, that is, no method has an implementation body.

By using Abstract class we achieve partial abstraction, that is, depending on our requirement we can achieve abstraction from 0 to 100%. In Abstract class, some methods may have implementation body and some may not.

What is abstract method?

The method which is declared through abstract keyword is called abstract method. This method does not have implementation i.e. it does not have a body.

  • In this, abstract keyword is written in front of the method name.
  • An abstract method contains a method signature but does not have a body.
  • Instead of curly braces, use a semicolon at the end of this method; It is applied.

Example of this:-

abstract void showDetail();//no method body and abstract

Example of abstract class and method

Some important points:

  • A method without body (no implementation ) is knows as abstract method.
  • A method must always be declared in an abstract class or we can say that if a class has an abstract method, it should be declared abstract as well.
  • If a regular class extends an abstract class, then the class must have to implement all the abstract methods of abstract parent class or it has to be declared abstract as well.
  • Abstract methods in an abstract class are meant to be overridden in derived concreate classes otherwise compile time error will be thrown.
  • Abstract classes cannot be instantiated means we cant create an object of abstract class.

// Program 1

abstract class Vehicle
{
abstract void start();
}
class Car extends Vehicle
{
void start()
{
System.out.println(“Car start with key.”);
}
}
class Scooter extends Vehicle
{
void start()
{
System.out.println(“Scooter start with kick.”);
}
public static void main(String [] args)
{
Car c = new Car();
c.start();
Scooter s = new Scooter();
s.start();
}
}

Output:

Compile: javac Scooter.java

Run: java Scooter
Car start with key.
Scooter start with kick.

// Program 2

abstract class Animal

{

     abstract void breath();

     void eat()

     {

        System.out.println(“I am eating as Animal…”);

     }

}

 class Dog extends Animal

 {

    void breath()

 {

        System.out.println(“Breathing as Dog…”);

 }

}

 class AbstractClassDemo

 {

 public static void main(String args[])

 {

 Animal a = new Dog();

 a.breath();

}

}

OutPut:

Breathing as Dog…

Explanation:

Above Program Animal is an abstract class, in which eat is a regular method and breath is an abstract method, which has no method body { } and is separated by a semicolon; has been closed from.

Abstract class Animal is inherited by Dog class.

Subclass Dog has defined its implementation body { } by overriding the abstract method breath () of its parent abstract class Animal.

Advantages of abstraction in Java

  1. It avoids code duplication and increases its reusability.
  2. It increases security and confidentiality because the user is informed only about the necessary functionality.
  3. Through this, it becomes easy to enhance the code because, if needed, we can change the internal coding without informing the end-user.
  4. Abstract class helps in creating loosely-coupled classes.

Some More: 

POP- Introduction to Programming Using ‘C’

DS – Data structure Using C

OOP – Object Oriented Programming 

Java Programming

DBMS – Database Management System

RDBMS – Relational Database Management System

Join Now: Data Warehousing and Data Mining 

Leave a Reply

Your email address will not be published. Required fields are marked *