Abstraction in Java16
Abstraction in Java16
Topperworld.in
Abstraction in Java
Java provides many in-built abstractions and few tools to create our own.
Abstract classes
Interfaces
An abstract class can have both the regular methods and abstract
methods. For example,
// abstract method
abstract void method1();
// regular method
void method2() {
System.out.println("This is regular method");
}
}
by ;.
If a class contains an abstract method, then the class should be declared
abstract. Otherwise, it will generate an error.
For example,
// error
// class should be abstract
class Language {
// abstract method
abstract void method1();
Java Programming
The major advantage of hiding the working of the brake is that now
the manufacturer can implement brake differently for different
motorbikes, however, what brake does will be the same.
class Main {
public static void main(String[] args) {
MountainBike m1 = new MountainBike();
m1.brake();
SportsBike s1 = new SportsBike();
Java Programming
s1.brake();
}
}
Output:
MountainBike Brake
SportsBike Brake
method brake().
Advantage of Abstraction :
Disadvantage of Abstraction :