0% found this document useful (0 votes)
24 views

Abstraction in Java16

Uploaded by

mohit chouhan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

Abstraction in Java16

Uploaded by

mohit chouhan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Java Programming

Topperworld.in

Abstraction in Java

Abstraction in Java refers to hiding the implementation details of a code


and exposing only the necessary information to the user.

It provides the ability to simplify complex systems by ignoring irrelevant


details and reducing complexity.

Java provides many in-built abstractions and few tools to create our own.

Abstraction in Java can be achieved by Use of :

 Abstract classes
 Interfaces

Java Abstract Class

The abstract class in Java cannot be instantiated (we cannot create


objects of abstract classes). We use the abstract keyword to declare an
abstract class. For example,

// create an abstract class


abstract class Language {
// fields and methods
}
...

// try to create an object Language


// throws an error
Language obj = new Language();
Java Programming

An abstract class can have both the regular methods and abstract
methods. For example,

abstract class Language {

// abstract method
abstract void method1();

// regular method
void method2() {
System.out.println("This is regular method");
}
}

Java Abstract Method

A method that doesn't have its body is known as an abstract method. We


use the same abstract keyword to create abstract methods. For example,

abstract void display();

Here, display() is an abstract method. The body of display() is replaced

by ;.
If a class contains an abstract method, then the class should be declared
abstract. Otherwise, it will generate an error.

For example,

// error
// class should be abstract
class Language {

// abstract method
abstract void method1();
Java Programming

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.

The major advantage of hiding the working of the brake is that now
the manufacturer can implement brake differently for different
motorbikes, however, what brake does will be the same.

Let's take an example that helps us to better understand Java abstraction..

Example : Java Abstraction

abstract class MotorBike {


abstract void brake();
}

class SportsBike extends MotorBike {

// implementation of abstract method


public void brake() {
System.out.println("SportsBike Brake");
}
}

class MountainBike extends MotorBike {

// implementation of abstract method


public void brake() {
System.out.println("MountainBike Brake");
}
}

class Main {
public static void main(String[] args) {
MountainBike m1 = new MountainBike();
m1.brake();
SportsBike s1 = new SportsBike();
Java Programming

s1.brake();
}
}

Output:

MountainBike Brake
SportsBike Brake

In the above example, we have created an abstract super


class MotorBike. The superclass MotorBike has an abstract

method brake().

The brake() method cannot be implemented inside MotorBike. It is


because every bike has different implementation of brakes. So, all the
subclasses of MotorBike would have different implementation of brake().

So, the implementation of brake() in MotorBike is kept hidden.

Here, MountainBike makes its own implementation

of brake() and SportsBike makes its own implementation of brake().

Advantage of Abstraction :

 Modularity: Abstraction allows you to break down a complex


system into smaller, more manageable parts. By hiding
implementation details and focusing only on the essential features,
you can create modular components that can be easily maintained,
modified, and reused.
 Encapsulation: Abstraction helps to enforce the principle of
encapsulation, which is one of the key principles of OOP.
Encapsulation means that the implementation details of an object
are hidden from the outside world, and can only be accessed
through a well-defined interface. Abstraction allows you to define a
Java Programming

well-defined interface for an object, and hide its implementation


details.
 Polymorphism: Abstraction allows you to achieve polymorphism in
Java, which means that you can treat objects of different classes as
if they were of the same type. By defining a common interface for a
group of objects, you can write code that can work with any object
that implements that interface, without knowing its specific type.
 Code Reusability: Abstraction promotes code reusability in Java,
as you can define common behaviors and interfaces that can be
implemented by multiple classes. This reduces the amount of code
that needs to be written and maintained, and allows you to create
more efficient and scalable applications.

Disadvantage of Abstraction :

 Complexity: Abstraction can add complexity to your code if not


used properly. If the abstraction is too complex or abstract, it can
be difficult to understand and maintain the code.
 Performance Overhead: Abstraction can introduce a performance
overhead, as the code needs to be processed through additional
layers of abstraction. This can lead to slower execution times,
especially for large or complex applications.
 Design Overhead: Abstraction requires careful design and
planning, which can add additional overhead to the development
process. This can lead to longer development times and higher
development costs.
 Limited Flexibility: Abstraction can limit the flexibility of your
code, as it may restrict the ways in which you can use or extend the
code. This can make it more difficult to adapt the code to changing
requirements or new use cases.

You might also like