Java Review (XIII, object-oriented ---- abstract class)


If you move from bottom to top in the inheritance hierarchy of class, located in the upper class more versatile, even more abstract. From one perspective, a more common ancestor, but it as a derived base class for other classes, rather than as a specific instance of the class you want to use.


Abstract methods and classes

Abstract methods and classes must use the abstract modifier to define, there are abstract methods of the class can only be defined as an abstract class, abstract class can be no abstract methods.

Abstract methods and rules of the following class.

  • Abstract class abstract modifier must be modified, abstract methods must be modified to use the abstract modifier abstract method can have a method thereof.
  • An abstract class can not be instantiated, you can not use the new keyword to call the constructor of an abstract class creates an instance of an abstract class. Even abstract class does not contain abstract methods, abstract classes can not create an instance.
  • An abstract class can contain fields, methods (conventional method and the method can be abstract), constructor, initialization block, an internal class (interface, enum) five components. Abstract class constructor can not be used to create instances, be invoked primarily for its subclasses.
  • Abstract class contains methods (including direct defines an abstract method; or inherit an abstract superclass, but included in the abstract parent class is not fully realized; or implements an interface, but does not fully implement the abstract interface contains three methods case) can be defined as an abstract class.
Abstract class instance
public class Main {
    public static void main(String[] args) {
        Person p = new Student();
        p.run();
    }
}

//抽象类Person
abstract class Person {
    //抽象类
    public abstract void run();
}

//抽象类子类
class Student extends Person {
    //抽象类子类必须实现父类抽象方法
    @Override
    public void run() {
        System.out.println("Student.run");
    }
}

The role of abstract class

An abstract class can not create an instance, only as a parent class to be inherited.

From the semantic point of view, an abstract class is abstracted from the plurality of particular classes parent class, it has a higher level of abstraction. Having the same class from the plurality of abstract features an abstract class, an abstract class as a template to which this subclass, the subclass of the design so as to avoid randomness. Abstract class embodies a kind of template pattern design, abstract class as a universal template multiple sub-classes, subclasses to extend, on the basis of the transformation of the abstract class, subclass but will generally retain the overall behavior of an abstract class.

If you write an abstract superclass, parent class provides a common method for multiple subclasses, and the one or more methods left to achieve its subclasses, this is a model template, template mode is also very common and simple design pattern One.

Here again is an example of a template pattern, in this example the abstract parent class, the common parent class depends on an abstract method, the method is deferred to abstract subclasses provide an implementation.

Examples of template pattern - abstract superclass
public abstract class SpeedMeter{
   // 转速
   private double turnRate;
  public SpeedMeter() {}
  // 把计算车轮周长的方法定义成抽象方法
   public abstract double calGirth() ;
   
   public void setTurnRate(double turnRate){
     this.turnRate = turnRate ;
   }
      
  //定义计算速度的通用算法
  public double getSpeed(){
    // 速度等于周长*转速
    return calGirth () * turnRate;
  }

}  

Template mode Example - subclass
public class CarSpeedMeter extends SpeedMeter{
   private double radius;
   public CarSpeedMeter(double radius){
     this.radius = radius ;
   }   
   //实现父类抽象方法
   public double calGirth(){
    return radius * 2 * Math.PI ;
   }
    
   public static void main(String[] args){
     //既可创建CarSpeedMeter 类的对象,也可通过该对象来取得当前速度 
    CarSpeedMeter csm = new CarSpeedMeter(O.34) ;
    csm.setTurnRate(15) ;
    System.out.println(csm . getSpeed()) ;
   }

} 

SpeedMeter class provides general algorithm speedometer, but some specific implementation details are deferred to a subclass of class CarSpeedMeter implemented. This is also a typical template mode.

Template model is commonly used in object-oriented software, the principle is simple, implementation is very simple. Here are some simple rules to use template pattern.

  • Abstract parent class defines only some of the processes require the use of, the portion can not be achieved abstracted abstract methods, leaving subclasses to achieve.
  • The method may include the parent class needs to call another series of methods, these methods can be transferred either implemented by the parent, it can also be implemented by its subclasses. The method provides only the parent class defines a general method, which may not be achieved entirely by itself, but must rely on an auxiliary subclass thereof.

When using an abstract idea: the caller only concerned with defining abstract methods, we do not care about the specific implementation subclasses.



Reference:
[1]: "crazy Java handouts"
[2]: "Java core technology Volume I"
[3]: https: //www.liaoxuefeng.com/wiki/1252599548343744/1260456371027744

Published 136 original articles · won praise 36 · views 30000 +

Guess you like

Origin blog.csdn.net/sinat_40770656/article/details/102762121