Preface
In Java, there are times when you need to extract the behavior of a class, so there is the concept of abstract classes and interfaces, this blog, a small piece of the abstract classes and interfaces in Java.
Abstract methods and abstract classes
The rules for abstract methods and abstract classes are as follows:
1. Abstract classes must be decorated using the abstract modifier, which must also be decorated with the abstraction modifier, and the extraction method cannot have a method body
2. Abstract classes cannot be instantiated, and the constructor of an abstract class cannot be invoked using the New keyword to create an instance of an abstract class. Even if an abstract class cannot contain abstract methods, this abstract class cannot be created as an instance.
3. Abstract classes can contain member variables, methods (both ordinary and abstract methods can), constructors, initialization blocks, inner classes (interfaces, enumerations) 5 components. The constructor of an abstract class cannot be used to create an instance, primarily for invocation by its subclasses.
4. Classes that contain abstract methods (including the direct definition of an abstract method, or inheritance implements an abstract parent class, but does not fully implement the abstract method that the parent class contains, or implements an interface, but does not fully implement the abstract method that the interface contains three cases) can be defined as abstract classes.
These four points are particularly important and need to be read over and over again.
Note: It is important to note that abstract methods and empty method bodies are not the same concept, such as public abstract void test (); Is an abstract method that does not have a method body at all, that is, there is no pair of curly braces behind the method definition, but public void Test () {} method is a common method, it has defined the method body, but the method body is empty, so this method can not be decorated with abstract.
And when using the abstract modifier class, it indicates that the class can only be inherited; When using the abstract modification method, it is indicated that this method can be overridden only by implementation. While the final modified class cannot be inherited, the decorated method cannot be overridden. So final and abstract can never be used at the same time.
And when static modifies a method, it indicates that the method relies on the class to exist and that the method can be accessed through the class, so static cannot exist with abstract at the same time.
Let's take a look at an example and understand the theoretical knowledge described above
Shape abstract class
Package Com.test;public abstract class Shape{{system.out.println ("Perform the initialization block of Shape ..."); Private String color= "color";//defines an abstract method for calculating the perimeter public abstract double calperimeter ();//Defines an abstract method that returns a shape public abstract String GetType ();//The constructor that defines shape, which is not used to create a Shape object,//instead of a quilt class called public shape () {}public shape (String color) { System.out.println ("Execute shape's constructor ..."); this.color = color;} Omit color setter and getter method public void SetColor (String color) {this.color = color;} Public String GetColor () {return this.color;}}
Circle Class
Package Com.test;public class Circle extends Shape{private double radius;public Circle (String color, double radius) {Super (color); this.radius = radius;} public void Setradius (double radius) {This.radius = radius;} Abstract method for overriding the compute perimeter of the Shape class public double Calperimeter () {return 2 * Math.PI * RADIUS;} Overrides the abstract method of the return shape of the Shape class public String GetType () {return getColor () + "Circle";} public static void Main (string[] args) {Shape s2 = new Circle ("Yellow", 3); System.out.println (S2.gettype ()); System.out.println (S2.calperimeter ());}}
Java reading note 14 (abstract class in Java)