Objective
Let's take a look at the final keyword before we talk about abstract classes.
The final modifier can modify classes, properties, methods
When a class is decorated, it means that the class cannot be inherited by other features as normal classes
Modifying a property means that the property cannot be changed and must be assigned an initial value
Modifying a method means that the method cannot be overridden
What is an abstract class?
Why use abstract classes?
When we do not use the parent class when we write inheritance, we do not need to instantiate the object, we define the parent class as abstract class.
That is, the abstract class itself has no practical meaning, but when you write an inheritance or polymorphic program, you have to define the parent class all at this time, we define the parent class as abstract class, commonly known as abstract parent class
Abstract classes are characterized by abstract classes that cannot instantiate objects, but abstract classes can be inherited as well as other classes.
What is an abstract method?
Methods in abstract classes are generally inherited and polymorphic when abstract classes cannot create objects all the methods within the abstract class are not very useful, but it is not possible to define a method in an abstract class this time we define the method as an abstract method.
Three characteristics of an abstract method
1. There is no method body for abstract method
2. Abstract methods must exist in abstract classes
3. The abstract method in the parent class must be overridden in the subclass
Let's look at an example:
Abstract class Pet{public abstract void Eat ();} Class Dog extends Pet{public void eat () {System.out.println ("Dog eats Bones");}} Class Cat extends Pet{public void eat () {System.out.println ("Cat eats Fish");}} Class Nature{public void Law (Pet p) {p.eat ();}} Class testab{public static void Main (string[] args) {Nature Nature = new Nature (); Nature.law (new Dog ()); Nature.law ( New Cat ());}}
Java Abstract class explanation