Java Abstraction
Introduction to Java Abstraction
Java Abstraction is a fundamental concept of Object-Oriented Programming (OOP) that allows us to hide the unnecessary details and only show the essential features of an object. It mainly focuses on what an object does instead of how it does it.
What is Abstraction?
In Java, Abstraction is achieved using Abstract classes and interfaces. It is a process of hiding the implementation details and showing only the functionality to the user. Another way, it shows only important 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.
Abstract Class
An abstract class in Java is a class that cannot be instantiated, which means you cannot create new instances of an abstract class. It works as a base for subclasses. The purpose of an abstract class is to function as a framework for classes that extend it. A class that contains at least one abstract method becomes itself abstract.
abstract class Animal {
public abstract void animalSound();
public void sleep() {
System.out.println("Zzz");
}
}
In the example above, Animal
is an abstract class, animalSound()
is an abstract method and sleep()
is a regular method.
Abstract Method
An abstract method is a method declared without an implementation. It only has a method signature and does not contain a method body. Abstract methods are declared with the keyword abstract
and are always part of an abstract class. A class containing an abstract method becomes an abstract class itself.
abstract void move();
In the example above, move()
is an abstract method that must be implemented in any class that extends the abstract class containing this method.
Interface
An interface in Java is a completely abstract class that contains only abstract methods. It is a way to achieve full abstraction and multiple inheritance in Java. A class implements an interface, thereby inheriting the abstract methods of the interface.
interface Animal {
public void animalSound();
public void sleep();
}
In the example above, Animal
is an interface that contains two abstract methods.
Implementing Abstraction
To achieve abstraction in Java, we must either use interfaces or abstract classes. By using these, we can have fields and methods in the superclass that are common to all objects. The objects themselves then only need to implement the methods that are unique to them.
For example, consider a superclass Shape
, and its subclasses Circle
and Rectangle
. The Shape
class can contain an abstract method area()
that is then implemented in each of the subclasses.
abstract class Shape {
abstract void area();
}
class Circle extends Shape {
void area() {
System.out.println("Area of Circle");
}
}
class Rectangle extends Shape {
void area() {
System.out.println("Area of Rectangle");
}
}
In conclusion, abstraction in Java is a key concept of OOP that allows us to build complex applications with components that hide their internal workings and expose only the necessary interfaces. It makes application development more intuitive and manageable.