java inheritance, abstract class

//Inheritance:
After using inheritance, it is convenient to modify the code and reduce the amount of code;
to reuse the code;
use extends to modify;

syntax:
pubilc class subclass name extends parent class name;

in inheritance, one parent class can be inherited;
subclass To access the parent class, use the super keyword, super represents the object of the parent class;
when instantiating a subclass object, the parameterless construction of the parent class will be executed first by default, and then the parameterless construction of the subclass will be executed;
when instantiating a subclass with parameter construction, Use super (parameter) to repeatedly call the parameterized constructor in the parent class. By default, the parameterized constructor of the parent class will be called directly instead of the parameterless structure.

Popular science:
subclasses cannot inherit the constructor of the parent class;
inherited by subclasses The class is called the parent class, base class or super class;
the object class is the parent class of all classes. In JAVA, all java classes directly or indirectly inherit the java.lang.Object class, and the Object class is all java classes. Ancestor of; the
super statement must be in the method or constructor of the subclass, not elsewhere; (if the extends keyword is not used, that is, there is no explicit inheritance of a class, then this class directly inherits the Object class;)
Special Note: If there is a multi-level inheritance relationship, when a subclass object is created, it will be applied to the higher-level parent class multiple times, until one executes the no-argument constructor of the top-level parent class Object class;

syntax: ( Note: If you call the parent class constructor in the subclass constructor, the super statement must be the first sentence;)
Access the parent class constructor:
super(); //When direct super() is called, the parent class's parameterless constructor is called ;
super(name); //When there are parameters in the super(parameter) parentheses, call the parent class's parameterized construction;

access the parent class attribute:
super.name;

access the parent class method:
super.print();

a It is not allowed to use this and super statements to call the constructor at the same time in the constructor (otherwise only the first of the two will be executed (that is, the first one will be executed));
in the class method (static modified method) no The keyword this or super is allowed;

overriding:
in the subclass, the method inherited from the parent class can be rewritten according to the needs, which is called method overriding or method overriding;
method overriding must meet the following conditions:
1. Rewrite The writing method and the overridden method must have the same method name;
2. The overriding method and the overridden method must have the same parameter list;
3. The return value type of the overriding method must be the same as that of the overridden method. The value types are the same;
4. Overriding methods cannot narrow the access rights of the overridden method;

the difference between overloading and overriding:
overloading involves a method of the same name in the same class (must be in the same class), requiring the method The name is the same, the parameter list is different, and it has nothing to do with the return value and access modifier;
when rewriting involves a method with the same name between the parent class and the subclass, the method name is required to be the same, the parameter list is the same, the return type is the same, and the access modifier cannot be greater than Parent class;

abstract class and abstract method:
abstract class and abstract method are both decorated with abstract; (abstract class cannot be instantiated with new)
abstract class can be absent, there are one or more abstract methods, or even all methods can be abstract method;
Abstract methods have only method declarations, no method implementations. A class with abstract methods must be declared as an abstract class, and subclasses must override all abstract methods to be instantiated;
abstract methods can only be in abstract classes, and abstract methods have no method body (that is, { } curly brackets);

syntax:
public abstract void print(); no braces;
abstract cannot be used to modify properties and constructors;
abstract classes can have constructors, and their constructors can be called by other constructors of this class. If the constructors are not privately modified, they can also be quilted Constructor call in a class;

abstract cannot modify a method with private at the same time;
abstract cannot modify a method with static at the same time;
abstract cannot modify a method with final at the same time;

Guess you like

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