In this blog we will discuss what is Abstract class? How Abstract class is linked with Abstraction? We will check the use of Abstract class in Java. Also we will discuss why do we use Abstract class in Java?We will understand the real scnearion of Abstract class with examples. We will discuss Abstraction in java with example. We will unstandard the purpose of abstraction in OOP and also we will check that we can create constructor in abstract class.
Table of Abstract class:
1. What is Abstraction?
1.1 Abstraction in java with real time example.
1.2 How do you achieve abstraction?
2. Abstract Class
2.1 Use of Abstract Class
2.2 Why do we use Abstract class in Java?
2.3 What is the synatx of Abstract Class in Java?
2.4 Abstract method in java
2.5 Where do we use abstract class in real time?
2.6 Can abstract class have constructor?
2.7 What is the difference between data hiding and data abstraction?
Abstraction in Java:
Abstraction is a process that hide the implementation and show only functionality. For example you have a car and you know how to drive car but how exactly internally clutch,brake and gear box work, we don't know. And it is not needed for driving car.Here implemetation of clutch,brake and gear is hidden. We know only functionality. Abstract class is sed to implement Abstraction process.
Ways to achieve Abstraction
There are two ways to achieve abstraction in java
- Abstract class (0 to 100%)
- Interface (100%)
Abstract java:
Key Point Of Abstract Class:
- It can have abstract and non-abstract methods.
- It can contain static and non-static methods.
- We can't create object of abstract class.
- It can have final methods and constructor.
Use of Abstract class:
Why do we use Abstract class in Java?
Abstract class in java is used to achive Abstaraction that hide complexity.
What is the synatx of Abstract Class in Java?
abstract class A{
}
Abstract method in java:
In this class vehicle is an abstract class that is containg one abstract method. This class implementation is given by Car class.
abstract class Vehicle{
abstract void run();
}
class Car extends Vehicle{
void run(){System.out.println("Car running safely");}
Vehicle obj = new Car ();
obj.run();
}
}
Output:
Car running safely
Abstract class having constructor, data member and methods:
Output:
Another real scenario of abstract class:
The best example of abstract class is Java itself as we know the uses of java class but how these classes internally implemented it is hidden for us.
Can abstract class have constructor?
What is the difference between data hiding and data abstraction?
Where do we use abstract class in real time?