Abstraction (Cont.)
An Abstraction Example (Cont.)
Few details about the program are given next:
- public Shape( String color ) { this.color = color; }
-
The
this
keyword refers to the current object.
- super( color );
-
It is used to invoke the constructor of its parent class, and the
super
keyword refers to a class’s base class in a method or constructor.
- @Override
-
The
@Override
annotation is used when we override a method in subclass, though it is not mandatory to use this annotation.
- int area( ) { return (int) ( Math.PI * Math.pow( radius, 2 ) ); }
-
The command “
(int)
” is type casting, which converts a double value to an integer value.
The field Math.PI
is the value close to π (3.14), and the method Math.pow(double a, double b)
returns the value of the first argument raised to the power of the second argument.
Abstraction vs Encapsulation
- Abstraction is detail hiding (implementation hiding) while encapsulation is data hiding (information hiding).
- While encapsulation groups together data and methods that act upon the data, data abstraction deals with exposing the interface to the user and hiding the details of implementation.
Advantages of Abstraction
- It reduces the complexity of viewing the things.
- Avoids code duplication and increases reusability.
- Helps to increase security of an application or program as only important details are provided to the user.