
Abstract class

An abstract class is a class that is declared by using the abstract keyword. It may or may not have abstract methods. Abstract classes cannot be instantiated, but they can be extended into sub-classes.An abstract class can be extended into sub-classes, these sub-classes usually provide implementations for all of the abstract methods.
The key idea with an abstract class is useful when there is common functionality that's like to implement in a superclass and some behavior is unique to specific classes. So you implement the superclass as an abstract class and define methods that have common subclasses. Then you implement each subclass by extending the abstract class and add the methods unique to the class.
Points of abstract class :
1)Abstract class contains abstract methods. 2)Program can't instantiate an abstract class. 3)Abstract classes contain mixture of non-abstract and abstract methods. 4)If any class contains abstract methods then it must implements all the abstract methods of the abstract class.
Example of Abstract Class:
abstract class Animal {
void runAnimal() {
System.out.println("runAnimal() method called form Animal class"); // this
// statement will override by the subclass method runAnimal
}
void printAnimal() {
System.out.println("printAnimal() method called form Animal class");
}
}
class BuzzwordAnimal extends Animal {
void printAnimal() {
System.out.println("printAnimal() method called form BuzzwordAnimal class");
}
void runAnimal() {
super.runAnimal();
// super keyword helps to call method of superclass ie Animal
printAnimal(); // this invoke the buzzwordAnimal class method
}
}
class AbstractExample {
public static void main(String[] args) {
BuzzwordAnimal b = new BuzzwordAnimal();
b.runAnimal(); // overriding will take place as it has similar method
// name exits both is sub and superclass
}
}
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.