javaSE notes-abstract class

  The meaning of the abstract class is to be inherited by the subclass, otherwise the abstract class will be meaningless. The abstract class reflects the template idea , and the template is a universal thing.

Abstract class: A class containing abstract methods.

abstract class class name {

}

 

Abstract methods: methods without a method body

Modifier abstract return value type method name (parameter list);

 

Use of abstract classes

Subclasses that inherit an abstract class must override all abstract methods of the parent class . Otherwise, the subclass must also be declared as an abstract class.

The method rewriting at this time is the realization of the abstract method of the parent class by the subclass. We will also call this method rewriting operation, also called the implementation method.

 

Characteristics of abstract classes

There is: the abstract class has the ability to have abstract methods

Lost: The abstract class loses the ability to create objects

 

Notes on abstract classes

  • Abstract classes cannot create objects.
  • In the abstract class, there can be a constructor, which is used by the subclass to initialize the members of the parent class when creating objects.
  • Abstract classes do not necessarily contain abstract methods, but classes with abstract methods must be abstract classes.
  • Subclasses of abstract classes must override all abstract methods in the abstract parent class, otherwise subclasses must also be defined as abstract classes.
  • The meaning of abstract class is to be inherited by subclasses, and abstract class embodies the idea of ​​template.

 

Template design pattern: driver driving

  1. Define the driving class template: call the overridden method of the subclass in the parent class
  2. Define some fixed content (door opening, ignition, braking, flameout)
  3. Unused content defines abstract methods (different driving postures)

Driver.java

1  public  abstract  class Driver {
 2      // Define the method of driving 
3      public  void drive () {
 4          System.out.println ("Open the door" );
 5          System.out.println ("ignition" );
 6          ziShi (); / / Called the method rewritten by each subclass 
7          System.out.println ("Brake" );
 8          System.out.println ("Turn Off" );
 9      }
 10  
11      // The way new and old drivers drive Different, defined as an abstract method 
12      public  abstract  void ziShi ();
 13 }

 

 

NewDriver.java

// Define the new driver class, which is a kind of driver, inheriting the driver class 
public  class NewDriver extends Driver { 
    @Override 
    public  void ziShi () { 
        System.out.println ( "New driver driving posture: hold the steering wheel with both hands!" ); 
    } 
}

 

 

OlederDriver.java

// Define the old driver class, which is a kind of driver, inheriting the driver class 
public  class OldDriver extends   Driver { 
    @Override 
    public  void ziShi () { 
        System.out.println ( "Old driver driving posture: holding the steering wheel in the right hand and smoking in the left hand!" ) ; 
    } 
}

 

 

Test class Demo01Driver.java

public class Demo01Driver {
    public static void main(String[] args) {
        //创建新司机对象
        NewDriver nd = new NewDriver();
        nd.drive();

        System.out.println("-----------------");

        //创建老司机对象
        OldDriver od = new OldDriver();
        od.drive();
    }
}

 

 

运行结果

 

Guess you like

Origin www.cnblogs.com/love-xiaowu/p/12687140.html