Table of Contents
Abstraction in Java
Data Abstraction
Data abstraction, as the name suggests, is the process of abstracting some amount of the data while only displaying information that will be relevant to the user. It is a property by which users can only view information that is either important or serves a purpose that directly affects them. The data manipulation that goes within the program is neither important to the user nor does it have to be disclosed. A very simple example of this is the working of a car. A car is always viewed as a complete product rather than individual components.
Data abstraction can also be defined as ‘the process of identifying only the required characteristics of an object ignoring the irrelevant details.’ All objects have a particular behaviour that singles them out from other objects of the same category. Therefore, data abstraction inadvertently helps in the classification of objects.
Let us take forward the experiment mentioned before to explain the mechanism of data abstraction better. As mentioned before, a car is only of importance to the user when it has all the components attached to it. For example, the user will want the car to accelerate when the accelerator pedal is pressed. However, he/she will not be interested in the mechanism of the car as to what happens such that the car starts moving faster when the pedal is pressed. Therefore, the mechanism of the acceleration is abstracted from the user while the pedal and procedure to press the pedal is only provided. This analogy can also be extended to hitting the brake pedal to slow down the car and eventually come to a halt. In Java, abstraction is normally associated with interfaces and abstract classes.
Abstract Classes and Abstract Methods
- The keyword ‘abstract’ declares all abstract classes.
- Normally, when defining an abstract method, an implementation of the method is not required, and neither is it provided.
- There is no mandate regarding the type of methods an abstract class can harbour. It can have either abstract classes or a combination of both concrete and abstract classes.
- A method that has been declared to be abstract must be redefined in the subclass. This will either make overriding compulsory or make the subclass abstract in itself.
- If a class has one or more abstract methods, the class needs to be declared to be abstract.
- What makes abstract classes special is that they cannot be instantiated using the new keyword. This means that abstract classes cannot have objects.
- An abstract class may have parameterized constructors.
- The default constructor is always present in an abstract class.
When to Use Abstract Classes and Abstract Methods
In several examples, there will be situations where a superclass is to define a general class of the same category. In such cases, a superclass can be defined that only declares the structure of a given abstraction which any implementation of sorts. This superclass will only be a generalization that will be extended by the subclasses to form specific entities.
For example, let us consider a ‘Shape’ superclass. Shape is a general example. Each specific shape will have several attributes such a colour, size, and so on. Shape subclasses inherit the general shape class and have attributes of their own.
Sample Code
abstract class Shape { String colour; abstract double area (); public abstract String toString (); public Shape (String colour) { System.out.println(“Shape constructor called”); this.colour = colour; } public String getColour () { return colour; } } class Circle extends Shape { double radius; public Circle (String colour, double radius) { super (colour); System.out.println (“Colour constructor called”) ; this.radius = radius; } @override double area() { return Math.PI*Math.pow (radius, 2); } @override public String toString () { return “Circle colour is” + super.getColour() + “and area is :” + area(); } } class Rectangle extends Shape { double length; double width; public Rectangle (String colour, double length, double width) { super (colour); System.out.println (“Rectangle constructor called”); this.length = length; this.width = width; } @override double area() { return length*width; } @override public String to String () { return “Rectangle colour is” + super.getColour () + “and area is :” + area(); } } public class Test { 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()); } }
OUTPUT
Shape constructor called
Circle constructor called
Shape constructor called
Rectangle constructor called
Circle colour is Red an area is: 15.20530844
Rectangle colour is Yellow and area is: 8.0
Encapsulation vs Data Abstraction
Although both these concepts sound quite similar, encapsulation mainly deals with information hiding while abstraction deals with implementation hiding. While encapsulation tries to bring together data under one entity, abstraction only shows information that is relevant to the user.
Advantages of Abstraction
- Since only information that is important and easy to comprehend is shown to the user, it is always quite easy to work with that kind of data.
- Avoids code duplication and increases reusability.
- Abstraction helps to increase the security of any program by providing only important details to the user.
0 Comments