What is Abstraction?
Abstraction is a process of hiding the implementation details and showing only functionality to the user.
The main purpose of abstraction is hiding the unnecessary details from the users.
Abstraction is a process of hiding the implementation details and showing only functionality to the user.
The main purpose of abstraction is hiding the unnecessary details from the users.
Abstraction is selecting data from a larger pool to show only relevant details of the object to the user.
It helps in reducing programming complexity and efforts.
Real Life Example of Abstraction:
1) We only know about how to drive a car but can not know about how it work and also we do not know internal functionality of car.
There are two ways to achieve abstraction in java
An abstract method is a method that is declared without an implementation.
Syntax:
abstract class A{}
Example:
2) Interface:
Real Life Example of Abstraction:
1) We only know about how to drive a car but can not know about how it work and also we do not know internal functionality of car.
- Abstract class (achieves 0 to 100% abstraction)
- Interface (achieves 100% abstraction)
- An abstract class is a class that is declared with the keyword abstract.
- It may contain a mix of methods declared with or without an implementation.
- It needs to be extended and all abstract methods must be implemented in child class.
- It cannot be instantiated means you can not create an object of abstract class.
An abstract method is a method that is declared without an implementation.
Syntax:
abstract class A{}
Example:
1
2
3
4
5
6
| abstract class Vehical {
abstract void run(); //abstract method
void display() {
System.out.println("Display Method");
}
}
|
1
2
3
4
5
6
7
8
9
10
| class BMW extends Vehical {
public void run() { //implementing abstract method
System.out.println("Run method");
}
public static void main(String args[]) {
BMW obj = new BMW();
obj.run();
}
}
|
2) Interface:
- An interface in java is a blueprint of a class.
- Its collection of abstract methods means all the methods in interface are abstract methods.
- Along with abstract methods, an interface may also contain constants, default methods, static methods, and nested types.
- The interface in java is a mechanism to achieve fully abstraction.
- It cannot be instantiated just like abstract class.
Example:
1
2
3
4
| interface Demo {
public void display();
public void run();
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
| class TestClass implements Demo {
public void display() {
System.out.println("Dispaly Method");
}
public void run() {
System.out.println("Run Method");
}
public static void main(String args[]) {
TestClass obj = new TestClass();
obj.display();
}
}
|
Thanks for sharing the information about java and keep updating us.This information is really useful to me.
ReplyDeleteVery Very helpful blog. This blog helped a lot to understand about Java concepts. Thank you Vinod.
ReplyDeleteVery helpful Thanks
ReplyDeleteToo good and very helpful thanks for sharing.
ReplyDelete