What is Abstraction in Java | Implementations of Abstraction in Java | A Definitive Guide with Best Practices
Last updated on 04th Nov 2022, Artciles, Blog
- In this article you will get
- Abstraction in Java
- Abstract classes and Abstract methods
- Encapsulation vs Data Abstraction
- Advantages of Abstraction
- Why do we need Abstract Classes in Java?
Abstraction in Java
Data Abstraction is a property by virtue of which only an essential details are displayed to user. Ex: A car is viewed as car rather than its individual components.Data Abstraction may also be explained as the process of identifying only a required characteristics of an object ignoring an irrelevant details.Consider real-life example of a man driving a car. A pressing the accelerator the speed is actually increasing, does not know about an inner mechanism of the car or implementation of accelerator, brakes, etc in the car. This is what an abstraction is. In java, abstraction is achieved by a interfaces and abstract classes.
Abstract classes and Abstract methods
An abstract class is the class that is declared with a abstract keyword.An abstract class may or may not have all the abstract methods. Some of them can be a concrete methods.A method-defined abstract must always be redefined in a subclass, thus making an overriding compulsory or making a subclass itself abstract.Any class that contains a one or more abstract methods must also be declared with the abstract keyword.There can be no object of abstract class. That is, abstract class can not be directly instantiated with a new operator.An abstract class can have a parameterized constructors and default constructor is always be present in an abstract class.
When to use a abstract classes and abstract methods with example:
There are situations in which will want to explain a superclass that declares a structure of a given abstraction without providing the complete implementation of an every method. Sometimes will want to create the superclass that only explains a generalization form that will be shared by all of a subclasses, leaving it to every subclass to fill in details.
Consider classic “shape” example, perhaps used in the computer-aided design system or game simulation. The base type is a “shape” and each shape has a color, size, and so on. From this, specific types of a shapes are derived(inherited)-circle, square, triangle, and so on — each of which may have the additional characteristics and behaviors. For example, a certain shapes can be flipped. Some behaviors may be different, such as when need to calculate the area of shape. The type hierarchy embodies both similarities and differences between shapes.
Encapsulation vs Data Abstraction
- Encapsulation is a data hiding(information hiding) while an Abstraction is detailed hiding(implementation hiding).
- While encapsulation groups together data and methods that act upon a data, data abstraction deal with an exposing the interface to user and hiding the details of an implementation.
- Encapsulated classes are the java classes that follow data hiding and abstraction while can implement abstraction by using an abstract classes and interfaces.
- Encapsulation is the procedure that takes place at implementation level, while abstraction is the design-level process.
Advantages of Abstraction
- It reduces a complexity of viewing things.
- Avoids code duplication and also increases reusability.
- Helps to increase a security of an application or program as only an essential details are provided to user.
- It improves maintainability of an application.
- It improves a modularity of an application.
- The enhancement will become more easy because without an affecting end-users can able to perform any type of changes in an internal system.
Why do need an Abstract Classes in Java?
- If cannot create or use any object from an abstract classes, then what is a need of abstract classes?
- To answer this question, let’s take situation where need to create a class that declares a general form or structure or guidelines without any special considerations, without giving the complete implementation of every method.
- And, need this generalised form or structure of a class to be used by all its child classes, and child classes will enforce these guidelines by fulfilling all an implementation details as per requirement.
- There is no need to implement a method in a parent class, so we can declare these methods as abstract in a parent class. That is, will not provide the any method bodies or implementations of abstract methods.
- Making these methods are abstract will force all the derived classes to implement these abstract methods otherwise, there will be a compilation error in a code.
In Java, cannot create an object from the abstract class using a new operator. If try to instantiate an abstract class, it will give compilation error.An abstract class can have the constructor.This can include the both abstract and concrete methods. An abstract method has only a declaration but no method body.In Java, only classes or methods can be declared as an abstract, and cannot declare a variable as a abstract.Use an abstract keywords to declare the both class and method as abstract.If declare any method as a abstract, class automatically needs to become abstract class.Through a data abstraction can reduce a real world entity to its essential explained features and show only what is required by users. The concept of a data abstraction is completely based on a abstract classes in Java.
Are you looking training with Right Jobs?Contact Us
- Abstraction in C# Tutorial – Learn the Abstract class and Interface
- Control Statements in Java – Expert’s Top Picks
- How to Use Java Classes In Talend ? : Step-By-Step Process with REAL-TIME Examples
- What is Abstraction in Java | Implementations of Abstraction in Java | A Definitive Guide with Best Practices
- Kotlin vs Java | Know Their Differences and Which Should You Learn?
- What is Dimension Reduction? | Know the techniques
- Difference between Data Lake vs Data Warehouse: A Complete Guide For Beginners with Best Practices
- What is Dimension Reduction? | Know the techniques
- What does the Yield keyword do and How to use Yield in python ? [ OverView ]
- Agile Sprint Planning | Everything You Need to Know