Let’s learn abstraction in java.
Abstraction in java
Abstraction is defined as hiding internal implementation and showing only necessary information. There are two ways to achieve abstraction in java,
- Through abstract class and
- Through interfaces
To create an abstract class and abstract method in java “abstract” keyword is used. “abstract” is a non-access modifier. Here’s abstraction in java with example.
// abstract class abstract class Shape { abstract void sides(); } class Square extends Shape { void sides() { System.out.println("square has 4 sides."); } } class Triangle extends Shape { void sides() { System.out.println("triangle has 3 sides."); } public static void main(String[] args) { Square s = new Square(); s.sides(); Triangle t = new Triangle(); t.sides(); } }
Output:
square has 4 sides.
triangle has 3 sides.
Abstract method in java
An abstract method is a method without body or implementation. Implementation of abstract method is determined by child class.
A class having an abstract method or if there is an abstract method in a class then, that class should be declared as abstract.
Syntax:
// abstract class abstract class Calculator { // abstract method abstract int add(int number1, int number2); }
Abstract class in java
Abstract class is a class which cannot be instantiated. To access an abstract class it should be inherited from another class.
An abstract class can have abstract methods and non-abstract methods. Let’s see java abstract class example.
// abstract class abstract class Shape { // abstract method abstract void sides(); } class Triangle extends Shape { void sides() { System.out.println("Triangle shape has three sides."); } } class Pentagon extends Shape { void sides() { System.out.println("Pentagon shape has five sides."); } public static void main(String[] args) { Triangle obj1 = new Triangle(); obj1.sides(); Pentagon obj2 = new Pentagon(); obj2.sides(); } }
Output:
Triangle shape has three sides.
Pentagon shape has five sides.
Abstraction in java – realtime example
A real time example of abstraction is Car. To drive a car steering wheel, gear, accelerator, brake, clutch etc., are shown.
These are necessary to drive a car and are abstract. But to drive a car internal functionality or working of gear box, disc brake, power steering etc., are not shown.
Because there is no need to know the internal functionality of how gear box or disc brake works for a driver to drive a car. Thus hiding implementation and showing only relevant data to the user.
Abstract class and method in java
- We cannot create object of abstract class.
- If a class has at least one abstract method then it should be declared as an abstract class.
- “abstract” keyword is used to declare an abstract class.
- Abstract methods in an abstract class are meant to be overridden in subclass. If not overridden compile time error is thrown.
- If a non-abstract class extends an abstract class then non-abstract class must implement all abstract methods of parent class else non-abstract class has to declare as abstract.
- An abstract class also consists of default and parameterized constructor.
- Abstract class may or may not have abstract methods.
- In an abstract class there can be concrete methods.
Abstraction in java advantages:
- Increases security of java application since only necessary information is shown to user.
- Enhancement ability will be very effortless.
- Maintainability will be improved.