Getting Started with Java Lesson 57 - Inheritance

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/houjunkang363/article/details/90547957

1.1 Inheritance

1.1.1 generalization process

image.png

1.1.2 extends keyword

    ·  Inheritance can be achieved by the extends keyword class

    ·  Sub-class (Sub class) can inherit (Super class) member variables and member methods, but can also define your own member variables and member methods;

    ·  The Java language does not support multiple inheritance, a class can only inherit from a parent, but a parent can have multiple children classes

extends keyword (Continued from Page 1)

    public class Tetromino{
        Cell[] cells;
        public Tetromino(){
            cells=new Cell[4];
        }
        public void drop(){...}
        public void moveLeft(){...}
        public void moveRight(){...}
        public void print(){...}
    }

extends keyword (Continued)

    public class TetrominoT extends Tetromino{
        public TetrominoT(int row,int col){
            cells[0]=new Cell(row,col);
            cells[1]=new Cell(row,col+1);
            cells[2]=new Cell(row,col+2);
            cells[3]=new Cell(row+1,col+1);
        }
    }

    Through inheritance, TetrominoT have Cell [] cells as well as members drop, moveLeft, moveRight other methods.

1.1.3 Inheritance Constructors

    ·  Constructor subclass must call the parent class constructor of the super keyword, it can be properly initialize member variables inherited from the parent class.

    ·  If the parent class constructor does not call the constructor subclass, Java compiler will automatically call the constructor without the addition of a reference to the parent class (if the parent is not no-argument constructor, there will be a compile error).

    public TetrominoT(int row,int col){
        super();//编译器自动加入
        cells[0]=new Cell(row,col);
        cells[1]=new Cell(row,col+1);
        ......
    }//super关键字必须位于子类构造方法的第一行

Inheritance Constructors (Continued from Page 1)

image.png

1.1.4 parent class reference pointing object subclass

    ·  Object subclass can shape up the type of parent class. That is, the definition of the parent type reference may be to an object subclass.

image.png

Parent class object reference points to subclass (Continued)

    ·  Reference to the parent class can point to an object subclass, but a reference to the parent class members are accessible only defined by the parent class, subclass can not access the extended section.

    image.png

1.2 Rewrite

1.2.1 method of rewriting

    ·  Subclass can override (cover) method inherited from the parent class, which is the same method name and method parameter list with the parent class; but different implementation methods.

    ·  When overridden method of subclass object is invoked (either by reference or call calling subclass by reference to the parent class), running a version rewritten subclass.

Rewriting method (Continued from Page 1)

image.png

1.2.2 rewritten using the super keyword

    ·  Subclass the parent class method when rewriting, you can call the parent class version of the super keyword

image.png

1.2.3 rewritten and overloading difference

    ·  Overloading and rewriting is completely different grammatical phenomena, differences are as follows:

         Overload refer to the same methods but with different parameter list of a plurality of methods in a class name is defined, at compile time, to determine which method of binding according to the number and type of parameters.

         rewriting means in subclasses and superclasses are defined in exactly the same way, when the program is run, according to the different type of object (instead of reference type) invoked different versions.

Rewrite differences and overloaded (Continued from Page 1)

image.png

Rewrite differences and overloaded (Continued)

    ·  Overloaded follow the so-called "binding compiler" that at compile time which method should be called variable parameters depending on the type of judgment; because: obj variable of type Super, therefore: Goo of g (Super) method is called.

    ·  Rewrite follow the so-called "run-binding", ie the actual object type method invocation at run time based on a reference variable points; because: obj actually points to an object subclass, therefore: the subclass overrides method f It is called.

Bo Main Comments:

   The same behavior and methods to extract defined as a parent class, the subclass inherits, you can use the parent class member variables and methods, do not write.

   Examples of this class enumerated overloaded method is written p3.

Scan code concern me:

 

Guess you like

Origin blog.csdn.net/houjunkang363/article/details/90547957