Abstraction in Java
Abstraction in Java is the process in which we only show
essential details/functionality to the user. The non-essential
implementation details are not displayed to the user.
In this article, we will learn about abstraction and what abstract
means.
Simple Example to understand Abstraction:
Television remote control is an excellent example of
abstraction. It simplifies the interaction with a TV by hiding the
complexity behind simple buttons and symbols, making it easy
without needing to understand the technical details of how the
TV functions.
What is Abstraction in Java?
In Java, abstraction is achieved by interfaces and abstract
classes. We can achieve 100% abstraction using interfaces.
Data Abstraction may also be defined as the process of
identifying only the required characteristics of an object ignoring
the irrelevant details. The properties and behaviours of an object
differentiate it from other objects of similar type and also help in
classifying/grouping the objects.
Abstraction Real-Life Example:
Consider a real-life example of a man driving a car. The man
only knows that pressing the accelerators will increase the speed
of a car or applying brakes will stop the car, but he does not
know how on pressing the accelerator the speed is actually
increasing, he does not know about the inner mechanism of the
car or the implementation of the accelerator, brakes, etc in the
car. This is what abstraction is.
Java Abstract classes and Java Abstract
methods
1. An abstract class is a class that is declared with an abstract
keyword.
1|Page
2. An abstract method is a method that is declared without
implementation.
3. An abstract class may or may not have all abstract methods.
Some of them can be concrete methods
4. A method-defined abstract must always be redefined in the
subclass, thus making overriding compulsory or making the
subclass itself abstract.
5. Any class that contains one or more abstract methods must
also be declared with an abstract keyword.
6. There can be no object of an abstract class. That is, an
abstract class can not be directly instantiated with the new
operator.
7. An abstract class can have parameterized constructors and
the default constructor is always present in an abstract class.
Algorithm / Steps to implement abstraction in Java
1. Determine the classes or interfaces that will be part of the
abstraction.
2. Create an abstract class or interface that defines the common
behaviors and properties of these classes.
3. Define abstract methods within the abstract class or interface
that do not have any implementation details.
4. Implement concrete classes that extend the abstract class or
implement the interface.
5. Override the abstract methods in the concrete classes to
provide their specific implementations.
6. Use the concrete classes to implement the program logic.
When to use abstract classes and abstract methods?
There are situations in which we will want to define a superclass
that declares the structure of a given abstraction without
providing a complete implementation of every method.
Sometimes we will want to create a superclass that only defines
a generalization form that will be shared by all of its subclasses,
leaving it to each subclass to fill in the details.
2|Page
Consider a classic “shape” example, perhaps used in a
computer-aided design system or game simulation. The base
type is “shape” and each shape has a color, size, and so on.
From this, specific types of shapes are derived(inherited)-circle,
square, triangle, and so on — each of which may have additional
characteristics and behaviors. For example, certain shapes can
be flipped. Some behaviors may be different, such as when you
want to calculate the area of a shape. The type hierarchy
embodies both the similarities and differences between the
shapes.
Java Abstraction Example
Example 1:
// Java program to illustrate the
// concept of Abstraction
abstract class Shape {
String color;
// these are abstract methods
abstract double area();
public abstract String toString();
// abstract class can have the constructor
3|Page
public Shape(String color)
{
System.out.println("Shape constructor called");
this.color = color;
}
// this is a concrete method
public String getColor() { return color; }
}
class Circle extends Shape {
double radius;
public Circle(String color, double radius)
{
// calling Shape constructor
super(color);
System.out.println("Circle constructor called");
this.radius = radius;
}
@Override
double area()
{
return Math.PI * Math.pow(radius, 2);
}
@Override
public String toString()
{
return "Circle color is " + super.getColor()
+ " and area is : " + area();
}
}
class Rectangle extends Shape {
4|Page
double length;
double width;
public Rectangle(String color, double length,
double width)
{
// calling Shape constructor
super(color);
System.out.println("Rectangle constructor
called");
this.length = length;
this.width = width;
}
double area()
{
return length * width;
}
public String toString()
{
return "Rectangle color is " + super.getColor()
+ " and area is : " + area();
}
}
public class Test5 {
public static void main(String[] args)
{
Shape s1 = new Circle("Red", 2.2);
Shape s2 = new Rectangle("Yellow", 2, 4);
System.out.println(s1.toString());
System.out.println(s2.toString());
}
5|Page
}
Output
Shape constructor called
Circle constructor called
Shape constructor called
Rectangle constructor called
Circle color is Red and area is : 15.205308443374602
Rectangle color is Yellow and area is : 8.0
Example 2:
// Java Program to implement
// Java Abstraction
// Abstract Class declared
abstract class Animal {
private String name;
public Animal(String name) { this.name = name; }
public abstract void makeSound();
public String getName() { return name; }
}
// Abstracted class
class Dog extends Animal {
public Dog(String name) { super(name); }
public void makeSound()
{
System.out.println(getName() + " barks");
}
}
// Abstracted class
6|Page
class Cat extends Animal {
public Cat(String name) { super(name); }
public void makeSound()
{
System.out.println(getName() + " meows");
}
}
// Driver Class
public class AbstractionExample2 {
// Main Function
public static void main(String[] args)
{
Animal myDog = new Dog("Buddy");
Animal myCat = new Cat("Fluffy");
myDog.makeSound();
myCat.makeSound();
}
}
Output
Buddy barks
Fluffy meows
Explanation of the above Java program:
This code defines an Animal abstract class with an
abstract method makeSound(). The Dog and Cat classes
extend Animal and implement the makeSound() method.
The main() method creates instances of Dog and Cat and
calls the makeSound() method on them.
This demonstrates the abstraction concept in Java, where
we define a template for a class (in this case Animal), but
leave the implementation of certain methods to be
defined by subclasses (in this case makeSound()).
7|Page