Abstraction in java
Article By The Techie King
This tutorial explains what is Abstraction in Java along with programming examples. You will also learn what is an abstract class.The first pillar of OOP is “Abstraction”. “Abstraction is the process of selecting data to show only the relevant information to the user
An Abstraction is the quality of dealing with ideas rather than events. For example, when you consider the case of e-mail, complex details such as what happens as soon as you send an e-mail, the protocol your e-mail server uses are hidden from the user. Therefore, to send an e-mail you just need to type the content, mention the address of the receiver, and click send.
in Object-oriented programming, abstraction is a process of hiding the implementation details from the user, only the functionality will be provided to the user
Abstraction in OOP can be of two types
- Data Abstraction : Data abstraction is the reduction of a particular body of data to a simplified representation of the whole. Abstraction, in general, is the process of taking away or removing characteristics from something in order to reduce it to a set of essential characteristics
- Control Abstraction : Control Abstraction can be used to build new functionalities and combines control statements into a single unit. It is a fundamental feature of all higher-level languages and not just java
What Is Abstraction In Java
An Abstraction can be seen as one of the important features and building blocks of the Java language. In Java abstraction is implemented using an interface and abstract class.Java provides a non-access modifier abstract for implementing abstraction,This abstract modifier can be used with classes and methods but not variables.
The interface provides complete abstraction.it only provides method prototypes and not their implementation. An abstract class provides partial abstraction wherein at least one method should not be implemented.
Abstraction class In Java
A class which is declared as abstract is known as an abstract class. It can have abstract and non-abstract methods. It needs to be extended and its method implemented. It cannot be instantiated.
- An abstract class must be declared with an abstract keyword.
- It cannot be instantiated.
- It can have constructors and static methods also.
- It can have abstract and non-abstract methods.
- It can have final methods which will force the subclass not to change the body of the method.
Example :
abstract class Mobile {
public abstract void mobileStart();
public void off() {
System.out.println("mobile is off");
}
}
From the example above, it is not possible to create an object of the Mobile class:
Abstraction method In Java
A method which is declared as abstract and does not have implementation is known as an abstract method.Abstract method: can only be used in an abstract class, and it does not have a body. The body is provided by the sub class
Example : abstract void run();