Java Abstraction
In object-oriented programming, abstraction is all about hiding the implementation details from the user. In the end the user will only know what this function does instead of how it does.
Abstraction is the quality of dealing with ideas rather than events. For example, if we have abstract bank class with one abstract interestRate() method . The definition and logic of calculating interestRate() will be different depending on the bank.
Abstract class
A class contains abstract keyword in declaration known as Abstract class.
// abstract class
abstract class TestClass {
}
Abstract Class Rules:
- Abstract class may or may not have abstract functions.
- If any method defined in the class as abstract than the class must have abstract keyword.
- Abstract class cannot be instantiated.
- Abstract class need to inherit in other class for usage.
- The child class which is implementing Abstract class need to declare itself as abstract or implement all abstract methods to instantiate that class.
- An abstract class can have data member, abstract method, define method(non abstract method) and constructor.
Abstract method
A Method contains abstract keyword in declaration known as Abstract method. We may have any number of abstract methods in a class.
// abstract class
abstract class TestClass { // abstract method
abstract void addition();
}
Abstract Method Rules:
- A class can have any number of abstract methods.
- Class need to be declared as abstract if we have declared single abstract method in the class.
- Abstract method doesn’t contain any method definition.
Example:
In the below example, there is a constructor defined with the abstract method print() in the abstract class Shape.
Sub class Circle extends the parent Shape class and override its print() method.
In the Test public class created a Shape object having a Circle class reference to execute the print() method.
OUTPUT:
Imran Khan, Adobe Community Advisor, AEM certified developer and Java Geek, is an experienced AEM developer with over 11 years of expertise in designing and implementing robust web applications. He leverages Adobe Experience Manager, Analytics, and Target to create dynamic digital experiences. Imran possesses extensive expertise in J2EE, Sightly, Struts 2.0, Spring, Hibernate, JPA, React, HTML, jQuery, and JavaScript.