Abstraction
·
Mohammad-Ali Bandzar
An introduction to Abstraction with Java.
Abstract classes are classes that are declared as abstract. Abstract classes can only be used in sub classes. An abstract class is created without any actual functionality, with its main purpose being to reserve the class name for later use.
Any class that contains abstract methods must be declared as an abstract class.
Abstract classes are used when you want to share methods amongst multiple classes. The subclass declaration must have the same name as the class declaration and will determine the method’s functionality.
// abstract parent class
abstract class Animal {
// abstract method
public abstract void sound();
}
class Cat extends Animal {
public void sound() {
System.out.println("Meow");
}
public static void main(String args[]) {
Animal cA = new Cat();
cA.sound();
}
}
THANKS FOR READING