Abstract method and abstract class

1. Abstract class
The purpose of an abstract class is to mark certain classes that cannot be initialized, but can only be inherited and polymorphic, such as Animal is an animal class, there is no need to initialize, what is an animal? What animal? . Identify abstract classes with keywords abstract keywords.
1. The compiler does not initialize the abstract class, and the syntax does not pass. The abstract class represents cannot have instantiated objects, but it can still be used to declare reference types for polymorphic use without creating objects.
2. In addition to being inherited, abstract classes are useless, valueless and purposeless
3. Abstract method

< div style="white-space: pre-wrap; text-indent: 28px; text-align: left; line-height: 1.75; font-size: 14px;"> 1. An abstract class means that this class must be inherited, and an abstract method indicates that this method must be overridden (overridden). For example, in the Animal class, an abstract eat() method is written in it, and the method body is not written. Used in subclasses to rewrite.

2 .If you declare an abstract method, then that class must also be abstract. But abstract classes can contain concrete and non-abstract methods.
3. The concrete class must implement all the methods of the abstract class. Of course, it can be transferred to the next implementation of the inheritance tree by marking it as abstract.
4. Why is there an abstract method? So there is no public code
When there is no way to give a common meaningful code in the parent class, the abstract method means that even if the content of the method cannot be realized, a group of children can still be defined Class common agreement (to ensure that there are some methods in the subclasses)
5. What are the benefits of abstract methods?
polymorphism, The goal is to use the type of the parent class as a method parameter, return type, or array flag. Through this mechanism, it is possible to add new subclasses to the program without having to rewrite or modify the processing of these types of programs. For example, Animal is a parent class, with dozens of subclasses, and a method with parameters. If polymorphism is used, the parent class reference is directly passed in when the method is defined, and the method is called When you pass in the subclass object. If there is no polymorphism, you need to write dozens of methods to pass in the objects of each subclass.
 1 public< /span> abstract class Person {

2 public abstract void eat();
3 public abstract void print();
4 }
5 class man extends Person{
6 public void eat() {
7 System.out.println("man");
8 }
9 public void print() {
10 System.out.println("man");
11 }
12 public void solo() {
13 System.out.println("men");
14 }
15 }
16 class woman extends Person{
17 public void eat() {
18 System.out.println("woman");
19 }
20 public void print() {
21 System.out.println("man");
22 }
23 public void show() {
24 System.out.println("woman");
25 }
26 }
27
28 public class PersonText {
29 public static void main(String[] args) {
30 personManner per=new personManner();
31 man ma=new man();
32 woman wo=new woman();
33 per.peek(ma);
34 per.peek(wo);
35 }
36 }
37 class personManner{
38 public void peek(Person pe) {
39 if (pe instanceof man) {
40 ((man) pe).solo();
41 }
42 if (pe instanceof woman) {
43 ((woman)pe).show();
44 }
45 pe.eat();
46 pe.print();
47 }
48 }

6. Points to note
Abstract class: through abstract Modification type (cannot appear at the same time as final)
Features: 1 .Abstract class cannot be instantiated
2. There can be a constructor in the abstract class (the constructor is for the subclass, and the subclass construction method will be used)
3. Abstract classes can have abstract methods and non-abstract methods.
4. The abstract class has a constructor, but it cannot be instantiated // In line with object-oriented thinking, abstract classes cannot be instantiated because they have no actual objects.
5. Abstract The specific methods in the class can be called by subclass objects through the inheritance relationship of the subclass, or the specific methods in the abstract class can be modified with static, and then can be called directly with the class name
Abstract method: A method of modification through abstract
Features: 1. Abstract methods cannot have method bodies
2. The abstract method must be overridden by the subclass (unless the method of the subclass is also an abstract method)
3. Abstract methods cannot be modified with private, because abstract methods are used to be inherited. If they are modified by private, they cannot be inherited by subclasses.
4. Abstract methods cannot be modified with static, because abstract methods If there is no method body and no practical meaning, the call is meaningless. If it is modified with static, although the abstract class cannot instantiate the object, it can be directly called with the class name after being modified with static.

1. Abstract class

The purpose of abstract classes is to mark that certain classes cannot be initialized, but can only be inherited and polymorphic. For example, Animal is an animal class and there is no need for initialization. What is an animal? What animal? . Identify abstract classes with keywords abstract keywords.

1. The compiler will not initialize the abstract class and the syntax will not pass. The abstract class represents cannot have instantiated objects, but it can still be used to declare reference types for polymorphic use without creating objects.

2. Apart from being inherited, abstract classes are useless, valueless and purposeless

3. Abstract methods

1. Abstract classes represent certain types of To be inherited, the abstract method indicates that this method must be overridden (overridden). For example, in the Animal class, an abstract eat() method is written in it, and the method body is not written. It can only be used to override in subclasses.

2 .If you declare an abstract method, then That class must also be abstract. But abstract classes can contain concrete and non-abstract methods.

3. Specific category All methods of abstract classes must be implemented. Of course, it can be transferred to the next implementation of the inheritance tree by marking it as abstract.

4. Why is there an abstract method? So there is no common code

When there is no way to give a common meaningful code in the parent class, the abstract method means that even if it cannot The content of the method is realized, but it is still possible to define a set of protocols common to subclasses (to ensure that there are some methods in the subclasses)

5. What are the benefits of abstract methods?

Polymorphism, the goal is to use the type of the parent class as a method parameter, return type, or array flag. Through this mechanism, it is possible to add new subclasses to the program without having to rewrite or modify the processing of these types of programs. For example, Animal is a parent class, with dozens of subclasses, and a method with parameters. If polymorphism is used, the parent class reference is directly passed in when the method is defined, and the method is called When you pass in the subclass object. If there is no polymorphism, you need to write dozens of methods to pass in the objects of each subclass.

 1 public abstract class Person {

2 public abstract void eat();
3 public abstract void print();
4 }
5 class man extends Person{
6 public void eat() {
7 System.out.println("man");
8 }
9 public void print() {
10 System.out.println("man");
11 }
12 public void solo() {
13 System.out.println("men");
14 }
15 }
16 class woman extends Person{
17 public void eat() {
18 System.out.println("woman");
19 }
20 public void print() {
21 System.out.println("man");
22 }
23 public void show() {
24 System.out.println("woman");
25 }
26 }
27
28 public class PersonText {
29 public static void main(String[] args) {
30 personManner per=new personManner();
31 man ma=new man();
32 woman wo=new woman();
33 per.peek(ma);
34 per.peek(wo);
35 }
36 }
37 class personManner{
38 public void peek(Person pe) {
39 if (pe instanceof man) {
40 ((man) pe).solo();
41 }
42 if (pe instanceof woman) {
43 ((woman)pe).show();
44 }
45 pe.eat();
46 pe.print();
47 }
48 }

6. Points to note

Abstract class: modify the class through abstract (cannot appear at the same time as final)

Features: 1. Abstract class cannot Be instantiated

2. The abstract class can have a constructor (the constructor is for the subclass, and the subclass construction method will be used)

3. The abstract class can There are abstract methods and non-abstract methods.

4. Abstract classes have constructors, but they cannot be instantiated // conforms to object-oriented thinking, Abstract classes have no actual objects so they cannot be instantiated.

5. The concrete methods in the abstract class can be called with subclass objects through the inheritance relationship of the subclass, or the concrete methods in the abstract class can be modified with static, and then the class can be used directly Name call

Abstract method: a method modified by abstract

Features: 1. An abstract method cannot have a method body

p>

2. The abstract method must be overridden by the subclass (unless the method of the subclass is also an abstract method)

3. The abstract method cannot be modified with private, because the abstract method is used to be inherited, If it is modified by private, it cannot be inherited by subclasses.

4. Abstract methods cannot be modified with static either, because abstract methods have no method body and have no practical meaning, and the call has no meaning. If modified with static, although abstract A class cannot instantiate an object, but it can be called directly with the class name after being modified with static.

WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 2814 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

Leave a Comment

Your email address will not be published.