1. Extends

Inheritance: A subclass inherits the attributes and behaviors of its parent class, making the subclass object have the same attributes and behaviors as the parent class. Subclasses have direct access to non-private properties and behaviors in their parent class.

Java supports only single inheritance, not multiple inheritance.

Advantages:

  1. Improve code reuse.
  2. Relationships arise between classes, which is the premise of polymorphism.

1. Characteristics after inheritance

Duplication of names is generally avoided in development.

  1. Member variable, same name

    When a member variable of the same name appears in a child class, the super keyword is used to modify the parent class member variable (super) when a child class needs to access a non-private member variable of the parent class. Superclass member variable name)

  2. Member method with the same name, override

Note:

    1. Subclass methods override superclass methods. Ensure that the permissions are greater than or equal to the permissions of the superclass.
    1. The subclass method overrides the parent method, with the same return value type, function name, and argument list
  1. A constructor
    1. The constructor name is consistent with the class name. So a subclass cannot inherit a superclass constructor.
    1. The constructor is used to initialize a member variable. Therefore, the initialization of a subclass must first perform the initialization action of the parent class. By default, there is a super() in the constructor of a subclass, which means that the constructor of the superclass is called. After the member variable of the superclass is initialized, it can be used by the subclass.

2. Super and this

Superclass space takes precedence over subclass objects

Super and this

  • Super: represents the storage space identifier of the parent class (interpreted as a reference to the parent).
  • This: represents a reference to the current object (whoever calls it represents it).

Abstract class

  1. An abstract class cannot create an object. If it does, compilation fails and an error is reported. Only objects whose subclasses are not abstract can be created.

    Understanding: Suppose you create an abstract class object and call an abstract method that has no concrete method body and makes no sense.

  2. An abstract class can have a constructor that a subclass uses to initialize a member of its parent class when creating an object.

    Understanding: Subclasses have a default super() constructor that requires access to the superclass constructor.

  3. An abstract class does not necessarily contain abstract methods, but a class with abstract methods must be an abstract class.

    Understanding: An abstract class that does not contain abstract methods for the purpose of preventing the caller from creating objects of that class.

  4. A subclass of an abstract class must override all of the abstract methods in the abstract parent class, otherwise the compiler will fail and an error will be reported. Unless the subclass is also abstract.

    Understanding: Classes may contain abstract methods, assuming that you do not override all of them. So it doesn’t make sense to call an abstract method after you create an object