inheritance

Inheritance is one of the three main characteristics of Java object orientation

Inheritance: The extraction of some common attributes and methods into another class, called the parent class. A subclass extends from a parent class and can use properties and methods inherited from the parent class

Class members:

Member variables, common methods, constructorsCopy the code

Inheritable:

  • Member variables
  • Common methods

An inheritance

  • A constructor

Properties or methods modified by private cannot be inherited only from the access level and can be accessed indirectly through inheritance methods

Characteristics of inheritance

  • Single inheritance, with only one direct parent
  • Multiple inheritance
  • A class that has no explicit parent implicitly inherits the Object class

Methods to rewrite

Override annotation: @override, used to verify that the Override succeeded

Overloading vs. overwriting:

  • Overloading: same method name, different parameter list (number, type, order)
  • Overwrite: An extension of a superclass method that requires a subclass to override the parent method if the parent method does not meet the needs of the subclass

Rewriting notes:

  • The method names must be the same
  • The parameters must be consistent
  • Subclass method access modifier >= Superclass method access modifier
  • If the overridden method returns a value, the subclass method returns a value type <= the parent method returns a value type, such as String

Object class

The Object class is the root class of all classes, and each class uses Object as its superclass. [Note: Interface does not matter]

Object common methods:

  • String toString() returns the String representation of the object on which this method was called
  • Boolean equals(Object obj) compares whether two objects are “equal”

We often override toString and equals methods when defining classes to suit our needs

The characteristics of the == comparison operator

  • Basic datatype variables: Compare values for equality
  • Reference datatype variables: Compare the address values of the objects to whether they are the same.

Access modifier

Access sort: private < default do not write (be careful not to add the default modifier) < protected < public

Function: Controls the access permissions of classes and their members

Modify fields, methods, constructors, and inner classes in a class

  • Public Can modify classes by default (inner class, outer class)
  • Private protected cannot modify external classes
  • Can’t modify local variables

Access list

Members of the same class Members in the same package Different packages but with inherited subclasses global
Private (private) Square root
The default don’t write Square root Square root
Protected Square root Square root Square root
public Square root Square root Square root Square root