javaSE inheritance and abstract classes

inherit

  • Overview and benefits of inheritance

    • Improve code reusability

    • That the relationship between the generated class and class

  • Inherited format

  • Inherited cases

  • After inheriting member access features

    • Characters after the inheritance - a member variable

      • Inherit access rules after the member variables

        • The same name

          • Table amount of child members of the same name appears in the parent class, subclass priority access to member variables own object, at this time if you want to access the parent class member variables, need to use the super keyword

            • super keyword: super can be used for variables and methods

        • No duplicate names

          • Does not affect the same name

    • After inherited characteristics - member method

      • After inheriting member methods of access rules

        • The same name

          • When you create a subclass object calls this method, the subclass object will give priority to call your own way

        • No duplicate names

          • When not affect the object method call, you will first look for in a subclass have no corresponding method, if the method will be executed in the presence of a subclass subclass, if the corresponding method in the parent class will subclass do not exist

    • Method overrides

      • Method overrides the method overloading difference

        • Method overrides: method name and the parent method appears sub-method, return type, parameter lists are the same way, there will be coverage results

          • Usage scenarios: the relationship between the parent and then sub happen. Subclass inherits the parent class, but subclass the parent class method that can not meet their needs, re-wrote a subclass method with the parent class of the same name, in order to cover the parent class.

          • Overrider rewrite notes

            • In the above marking method, to improve the readability of the code, the error can be prevented

          • Precautions

            • The method is to rewrite the parent-child relationships between categories in the event

            • Method subclass overrides the parent class method must ensure that the parent class greater than or equal rights permissions

            • Method subclass overrides the parent class method, the return type, function name, and parameter list must be exactly the same

        • Method overloading: Method name appears and return the same value type, a list of the different parameters of the method local process

    • Characters after the inheritance - Constructor

      • Inheritance rules constructor

        • Define the format and function constructor

          • Constructor's name is the class name is the same, so the subclass can not inherit the parent class constructor

          • Action constructor is to initialize the object member variable data, the initialization process subclass, must perform the initializing operation parent class constructor of a subclass of the default a Super (), indicates that the call parent class constructor of after the parent class member variables are initialized before they can use by subclasses (first, my father, only son)

        • Precautions

          • Called in the constructor for subclasses of the parent class constructor must be placed in the first row in the sub-class constructor

          • All sub-class constructor parameter will default empty constructor calls the parent class

            • Constructor subclass first row implies a super () call the parent class area without parameters constructor, super () can be omitted

          • If the constructor of a subclass of manually call the constructor, then it will not call the parent class constructor automatically

    • Useful subclass subclass, the subclass does not find a parent, the parent class has to use the parent, the parent class is not on the error.

  • Inherited characteristics

    • java only supports single inheritance, do not support multiple inheritance

      • Single inheritance: a subclass can have only one parent

      • Multiple inheritance: a subclass can have more than one parent class

    • Although a subclass can have only one parent, but a parent can have multiple children classes

    • You can inherit multiple layers

      • The multilayer Inheritance: subclasses inherit the parent class may be inherited another subclass

  • super and this keyword

  • Inherited memory

    • New heap space inside out, in addition to a method and object space variables subclasses, there is a space for variables and methods of the parent class,

    • When the main method call a method or a variable, the present preference category (partial) according to the principle of proximity, if not, to consider the parent class

Abstract class

  • Overview abstract class

    • Abstract methods and abstract concepts

      • We have no way subject method called abstract method

      • Java syntax requirements includes abstract methods of the class is an abstract class

  • Template mode

  • Definition and use of abstract methods

    • Defines an abstract class: abstract need to use the abstract keyword modification, this class can not be created object. He is used to make the parent class, subclass inherits

    • Format: Permission modifier abstract class class name

  • Notes abstract class

    • Note: All abstract methods of the parent class subclass inherits the abstract class must override, otherwise, the subclass must also be declared abstract

    • Abstract class may not have abstract methods, but there are abstract methods must be abstract class

    • An abstract class can not create an object, the object can be created only non-abstract subclasses

    • An abstract class can have a constructor, is to provide a subclass to create objects, use member initialization

    • Subclass of abstract class must override all the abstract parent class or subclass method must be defined as an abstract class

    • Meaning of existence is to abstract class inherited by subclasses, embodies the abstract idea of ​​a template

final keyword

  • Overview final keyword

    • Can not be changed, it can be used to modify the class, method and variable

    • Categories: modified class can not be inherited

    • Methods: The modified method can not be overridden

    • Variables: modified variables can not be reassigned

Guess you like

Origin www.cnblogs.com/Stack-kai/p/12154375.html