Abstraction
Description
Abstraction is one of the key concepts in object-oriented programming (OOP) that allows to represent complex real-world entities in a simplified manner. It involves hiding the complex implementation details of a system or object and exposing only the essential features or functionalities to the outside world. Abstraction focuses on what an object does rather than how it accomplishes its tasks.
Abstraction is a broader principle, while abstract classes are a specific tool used to implement abstraction.
Key Points about Abstraction
Hiding Complexity: Abstraction hides the complex implementation details of a system or object, allowing users to interact with it at a higher level without needing to understand the intricacies of its internal workings.
Essential Features: Abstraction exposes only the essential features or behaviors of an object, providing a simplified interface for users to work with. This reduces complexity and improves usability.
Generalization: Abstraction involves identifying common patterns and characteristics among different objects or entities and representing them using a generalized form or concept. This helps in organizing and structuring code in a more modular and reusable manner.
Encapsulation: Abstraction is often achieved through encapsulation, which involves bundling data (attributes) and methods (behaviors) that operate on the data into a single unit, known as a class. Encapsulation hides the internal state of an object and exposes a controlled interface for interacting with it.
Levels of Abstraction: Abstraction can occur at multiple levels, ranging from high-level conceptual abstractions to low-level implementation details. For example, a high-level abstraction of a car might focus on its functionality (e.g., driving, braking), while a low-level abstraction might involve the specific mechanics of its engine.
Types of Abstraction in OOP
Data Abstraction: This is primarily achieved through encapsulation. You hide data (fields) within a class and provide controlled access methods (getters and setters). Users interact with the object through these methods without worrying about the internal data structures.
In this example, the balance
field is private, preventing direct access to the raw data. The deposit
method allows controlled addition of funds, potentially performing validation. The getBalance
method provides a way to retrieve the balance without exposing the ability to modify it directly.
Here, data abstraction is achieved by hiding the internal representation of the balance
and providing controlled access methods.
Procedural Abstraction: This involves creating functions or methods that encapsulate a specific task or behavior. The method signature (name and parameters) defines the functionality, while the internal implementation details are hidden from the caller.
This example defines two static methods. add
encapsulates the simple addition operation. calculateArea
hides the formula and Math library usage for calculating the area of a circle. Both methods provide specific functionalities without revealing their internal implementation details.
Class Abstraction: A class itself is an abstraction. It defines a blueprint for creating objects with specific attributes (fields) and behaviors (methods). Users interact with objects of that class without needing to know the intricate details of how the class is implemented.
The Car
class defines a blueprint for creating car objects. It hides the complexities of a real car's engine, wheels, etc., focusing on the functionalities users interact with (start, accelerate). Users can create Car
objects and call these methods without needing to know the underlying mechanics.
Example of Abstraction:
Consider the concept of a Vehicle
in a transportation system. From a user's perspective, a Vehicle
can be abstracted as follows:
High-level Abstraction: Users interact with a
Vehicle
by performing actions such asdrive
,stop
, andrefuel
. They don't need to know the internal mechanisms of how these actions are implemented.Low-level Abstraction: Internally, a
Vehicle
might consist of various components such as an engine, wheels, and fuel tank. Each component has its own functionality and interacts with other components to achieve the desired behavior of theVehicle
. However, users interact with theVehicle
at a higher level, without needing to understand the details of each componen
What is Abstract class?
Description
An abstract class in Java is a class that cannot be instantiated directly, meaning you cannot create objects of an abstract class using the new
keyword. Instead, it serves as a blueprint for other classes and may contain both abstract and concrete methods.
Rules for Abstract Classes:
If a class contains at least one abstract method, it must be declared as abstract.
Abstract methods cannot have a body (implementation) and end with a semicolon (;).
Abstract classes cannot be instantiated directly with the
new
keyword.Subclasses of an abstract class must either provide implementations for all abstract methods or be declared abstract themselves.
Thread is not a abstract class.
Opposite of Abstract class is concrete class.
Abstract class supports multilevel inheritance.
Interface is also used to achieve abstraction.
Key Characteristics
Abstract Keyword: An abstract class is declared using the
abstract
keyword before the class name.
Abstract Methods: Abstract methods are method declarations that don't have a body (implementation). They are declared using the
abstract
keyword before the method return type. Subclasses that inherit from the abstract class must provide implementations for all inherited abstract methods. Abstract methods define the functionality that subclasses must adhere to, promoting a common interface within the class hierarchy.
Non-Abstract Methods: Abstract classes can also have concrete methods (methods with a body) that provide default implementations for common functionalities. Subclasses can inherit and potentially override these methods to customize behavior.
Instantiation: You cannot create objects directly from an abstract class using the
new
keyword. Subclasses, which are not abstract, can be instantiated.
Can Contain Fields: Abstract classes can contain fields (variables), constructors, static methods, and instance methods, similar to regular classes.
Other Details
Abstract class in Java can contain inner abstract classes, inner concrete classes, and static methods within it.
Last updated