java inheritance, constructor, super, this

Java inheritance:
1. If there is an inheritance relationship between two classes, the subclass will automatically inherit the methods and variables of the parent class, and the methods and variables of the parent class can be called in the subclass. public and protected, private not
allowed In java, only single inheritance is allowed, that is to say, a class can only explicitly inherit from one parent class at most. But a class can be inherited by multiple classes, which means that a class can have multiple subclasses.

2. Constructor
  Subclasses cannot inherit the constructors of the parent class, but it should be noted that if the constructors of the parent class have parameters, the super keyword must be explicitly passed in the constructor of the subclass Call the superclass's constructor with the appropriate parameter list.
If the parent class has a parameterless constructor, it is not necessary to use the super keyword to call the parent class constructor in the subclass's constructor. If the super keyword is not used, the system will automatically call the parent class's parameterless constructor.

3.
  There are two main usages of super super:
  1) super. member variable/super. member method;
  2) super(parameter1, parameter2....)
  The first usage is mainly used to call the same name of the parent class in the subclass Member variable or method; the second is mainly used in the constructor of the subclass to explicitly call the constructor of the parent class. It should be noted that if it is used in the constructor of the subclass, it must be the first part of the constructor of the subclass. a statement.

Abstract classes implement interfaces
In Java, the use of abstract classes to implement interfaces is not without effect. On the contrary, having time makes a big difference.
When you only want to implement individual methods (not all methods) in an interface, you can first write an abstract class to implement the interface and implement all the methods except the ones you want (the method body is empty).
Then use your class to inherit this abstract class, and only implement the methods you need in this class, so that you can meet your needs. However, if you implement the interface directly, you need to implement all the methods of the interface.


1. The this keyword mainly has three applications:
 (1) this calls the properties in this class, that is, the member variables in the class;
 (2) this calls other methods in this class;
 (3) this calls the Other constructors should be placed in the first line of the constructor when they are called.
 
 
1. A subclass (non-abstract class) that implements an interface or inherits an abstract class must implement all methods of the interface or all abstract methods of the abstract class.
If it is an abstract class to implement the interface, it can implement some or none of it, and if it is a concrete class, it must implement all the methods

Guess you like

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