What is Abstract Class in Java?
* Java abstract class is a class that can not be instantiated by itself, it needs to be subclassed by another class to use its properties. An abstract class is declared using the "abstract" keyword in its class definition.
*The abstract keyword is a non-access modifier, used for classes and methods:
- Hide the complex and shows the essential features
*Abstract class: is a restricted class that cannot be used to create objects (to access it, it must be inherited from another class).
*Abstract method: can only be used in an abstract class, and it does not have a body. The body is provided by the subclass (inherited from).
- An abstract class can have both abstract and regular methods:
KEYPOINTS:
abstract keyword is used to declare the method as abstract.
You have to place the abstract keyword before the method name in the method declaration.
An abstract method contains a method signature, but no method body.
Instead of curly braces, an abstract method will have a semoi colon (;) at the end.
SYNTAX:
abstract class Animal {
public abstract void animalSound();
public void sleep() {
System.out.println("Zzz");
}
}
Example 1:
* Abstract Class and Method
abstract class Animal {
// Abstract method
abstract void makeSound();
// Concrete method
void sleep() {
System.out.println("Sleeping...");
}
}
class Dog extends Animal {
// Implementing the abstract method
void makeSound() {
System.out.println("Bark");
}
}
public class AbstractExample {
public static void main(String[] args) {
Dog dog = new Dog();
dog.makeSound();
dog.sleep();
}
}
Output:
Bark
Sleeping...
Two types of ABSTRACT CLASS:
- PARTIAL ABSTRACT CLASS
- INTERFACE (OR) 100% ABSTRACTION.
1. PARTIAL ABSTRACT CLASS
*Java Abstract classes can also have non-abstract methods with complete implementations. These methods contribute to partial abstraction, where you provide some default behavior in the abstract class while leaving specific details to be implemented by subclasses
* In Partial Abstract class have a with implementations method and without implementations methods.
2. INTERFACE (OR) 100% ABSTRACTION.
*Java INTERFACE (OR) 100% Abstract classes can only haveing abstract methods without implementations methods.
Real-Life Example of Abstraction:
*The television remote control is the best example of abstraction. It simplifies the interaction with a TV by hiding all the complex technology. We don't need to understand how the tv internally works, we just need to press the button to change the channel or adjust the volum
Example:
// Working of Abstraction in Java
abstract class Geeks {
abstract void turnOn();
abstract void turnOff();
}
// Concrete class implementing the abstract methods
class TVRemote extends Geeks {
@override
void turnOn() {
System.out.println("TV is turned ON.");
}
@Override
void turnOff() {
System.out.println("TV is turned OFF.");
}
}
// Main class to demonstrate abstraction
public class Main {
public static void main(String[] args) {
Geeks remote = new TVRemote();
remote.turnOn();
remote.turnOff();
}
}
Output:
TV is turned ON.
TV is turned OFF.
Referred links :
1.https://www.scholarhat.com/tutorial/java/java-abstraction
2.https://www.geeksforgeeks.org/abstraction-in-java-2/
3.https://www.tutorialspoint.com/java/java_abstraction.htm
Top comments (0)