Java inheritance, polymorphism, overloading and rewriting of subclass methods, running process of parent and child class constructors

1. Polymorphism

1. The three premises of polymorphism

        1), there is an inheritance relationship

        2), the subclass overrides the parent class method

        3), the reference of the parent class data type points to the child class object

2. Features of polymorphic member access

    Animal am = new Cat();

        1) Member variables: compile to see the left (parent class), run to the left (parent class)

        2), member method : compile to see the left (parent class), run to the right (subclass)

        3), static method : compile to see the left (parent class), run to the left (parent class)

  Overload the corresponding compile time, rewrite the corresponding runtime

(Static is related to classes, not rewriting, so access is still on the left)

To sum up, there are only non-static member methods, compile on the left, run on the right

3. Disadvantages of polymorphism

        After polymorphism, subclass-specific properties and methods cannot be used. If you want to use subclass properties and methods, type coercion must be performed.

Cat ct = (Cat)am;
ct.catchMouse();

4. The advantages of polymorphism

        Reduced creation of redundant objects, needless to say that a subclassed object must be recreated on the heap in order to use a subclassed method

Second, method rewriting

    Rules for overriding methods:

        1) The overriding method cannot have a stricter access level than the overridden method .

        2) The parameter list must be the same as that of the overridden method .

        3) The return type must be the same as the return type of the overridden method .

        4) The overridden method cannot throw a new exception or a checked exception wider than the checked exception declared by the overridden method. But can throw fewer, more limited or no exceptions.

        5), can not override the method marked as final.

        6) If a method cannot be inherited, it cannot be overridden.

Remember, polymorphism only looks at the methods referenced by the parent class, not the methods of the child class object!

3. Method overloading

    Rules for overloading methods.

        1) The overloaded method must change the parameter list .

        2) The overloaded method has nothing to do with the return type. That is, overloaded methods cannot be distinguished by their return type

         3), the overloaded method can change the access modifier.

        4) The overloaded method can declare new or wider checked exceptions.

        5), methods can be overloaded in a class or in a subclass.

4. Constructor

1, the rules of the construction method

        1) The constructor can use any access modifier .

        2) The name of the constructor must be the same as the class name .

        3) The constructor cannot have a return type .

        4) If you do not create your own constructor in the class, the compiler will automatically generate a default constructor without parameters .

        5) If only a constructor with parameters is created, the compiler will not automatically add a constructor without parameters.

        6)、在每个构造方法中,如果使用了重载构造函数this()方法,或者父类的构造方法super()方法,那么this()方法或者super()方法必须放在第一行。而且这两个方法只能选择一个,因此它们之间没有顺序问题。

        7)、除了编译器生成的构造方法,而且没有显式地调用super()方法,那么编译器会插入一个super()无参调用。

        8)、抽象类有构造方法。

2、继承中的构造方法要遵循哪些原则 

        1)、子类的构造过程中必须调用其基类的构造方法(也就说在运行子类构造方法时肯定是先运行父类的构造方法)。

        2)、子类可以在自己的构造方法中使用super(argument_list)调用基类的构造方法。

                2.1、使用this(argument_list)调用本类的另外构造方法。

                2.2、如果调用super,必须写在子类构造方法的第一行。

        3)、如果子类的构造方法中没有显示的调用基类的构造方法,则系统默认调用基类的无参数构造方法。 

        4)、如果子类构造方法中既没有显示调用基类构造方法,而基类又没有无参数的构造方法,则编译出错。




Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325703041&siteId=291194637