-Why do you need abstract classes? How do I define an abstract class?
• is a template mode. Abstract classes provide a generic template for all subclasses, which can be extended on the basis of this template.
· by using abstract classes, you can avoid the arbitrariness of sub-class design. through abstract classes, we can strictly restrict the design of subclasses, so that subclasses are more common.
Points
√ Class-Smart definitions with abstract methods can abstract classes
√ Abstract classes cannot be instantiated, and abstract classes cannot be instantiated with new.
√ Abstract classes can contain properties, methods, and construction methods. However, the construction method cannot be used for new instances, only for the quilt class invocation.
√ Abstract classes can only be used for inheritance.
√ The abstract method must be implemented by the quilt class.
Eg:
Package Study;
Public abstract class Testabstract {
public abstract void run ();
public abstract void Eat ();
public void dosomething () {
System.out.println ("Do Something Good");
Eat ();
}
public static void Main (string[] args) {
Testabstract thing = new Catanimal ();
Thing.run ();
}
}
Class Catanimal extends Testabstract {
public void Run () {
System.out.println ("I can walk the cat step");
This.dosomething ();
}
public void Eat () {
System.out.println ("I can eat cat food");
}
}
Java Learning notes-abstract classes