A relationship between species and classes

Create a new class using the definition of an existing class as a base

The definition of a new class can add new data or new functionality, or it can use the functionality of the parent class, but it cannot selectively inherit from the parent class

A subclass can inherit only one parent class

package com.imooc.animal; public class Animal { private String name; // private int month; // month private String species; Public Animal() {} public String getName() {return name; } public void setName(String name) { this.name = name; } public int getMonth() { return month; } public void setMonth(int month) { this.month = month; } public String getSpecies() { return species; } public void setSpecies(String species) { this.species = species; } public void eat() {system.out.print (this.getName()+" eat "); } } package com.imooc.animal; public class Cat extends Animal{ private double weight; Public Cat() {} public double getWeight() {return weight; } public void setWeight(double weight) { this.weight = weight; Public void run() {system.out.print (this.getName()+" is a "+this.getSpecies()+", it is running with joy "); } } package com.imooc.animal; public class Dog extends Animal { private String sex; Public Dog() {} public String getSex() {return sex; } public void setSex(String sex) { this.sex = sex; Public void sleep() {system.out.println (this.getName()+ this.getMonth()+ this.getMonth()+ this.getMonth()+", "); } /* * method overload: * 1. Same class * 2. Same method name, different parameter list (parameter order, number, type) * 3. Private String sleep(String name) {return ""; private String sleep(String name) {return ""; } public void sleep(String name,int month) { } public void sleep(int month,String name) { } * */ } import com.imooc.animal.Animal; import com.imooc.animal.Cat; import com.imooc.animal.Dog; public class Test { public static void main(String[] args) { Cat one=new Cat(); One. Elegantly-named setName (" flower "); SetSpecies (" Chinese Garden cat "); one.eat(); one.run(); Dog two=new Dog(); Two elegantly-named setName (" niu "); two.setMonth(1); two.eat(); two.sleep(); Three =new Animal(); three=new Animal(); //three.run(); // Error}}Copy the code

Methods to rewrite

Grammar rules:

Defined in a subclass

The method name and parameter type, order, and number (excluding parameter names) must be the same as the method inherited from the parent class

Public void eat(String name) {system.out.println (name+" eat "); } public void jump() {system.out.println (" jump "); } /* * method override: * 1. Same method name, same parameter list (parameter order, number, type) * 3. Independent of method parameter name * 4. Public void eat() {system.out.println (this.getName()+" I have no appetite recently "); } public void eat(String month) {system.out.println (month+" have no appetite recently "); } /* Error: Public int jump() {system.out.println (" dog is a running jump "); return 1; } * /Copy the code

You can define attributes in a subclass that have the same name as those in the parent class, and assign values in the subclass first

Access modifier

Public: public: allows access from any location

Private: private: Access is allowed only in this class

Protected: allows calls within the current class, the same class/non-subclass, and across classes; Cross-package non-subclasses are not allowed

Default: allows calls in current class, same class/non-subclass; Calls across steamed buns/non-subclasses are not allowed

The impact of access modifiers on method rewriting

The access modifier needs to be greater than or equal to the access range of the parent class

Use of the super keyword

Super: a reference to a superclass object

super.eat();
super.x=4;
Copy the code

Tip: The constructor of the parent class is not allowed to be inherited or overridden, but does affect the instantiation of the subclass object

The constructor of a subclass must be called during the construction of its parent class

If there is no explicit annotation in the constructor of a subclass, the system calls the parent class’s no-argument constructor by default

If there is no explicit annotation in the subclass constructor and no constructor with no arguments in the parent class, the compilation is bound to fail

Implicit super constructor Parent1() is undefined. Must explicitly invoke another constructor

The implicit parent no-argument constructor was not found, and another constructor must be explicitly called

Super () can be used to specify the parent class constructor, and super() must be placed on the first line of valid subclass constructor code

Super ();

Super (String name, int the month);

Neither this nor super can be called in static methods, and they cannot coexist when constructors are called

Initialization order after inheritance

Class loading: Superclass static members — subclass static members — subclass object instantiation (superclass object constructs, including properties, constructor code blocks, constructors — subclass object constructs)

Access modifiers do not affect the loading order of static members, depending on where they are written

Object class

The Object class is the parent of all classes

A class that does not explicitly identify inheritance using the extends keyword inherits Object classes (including arrays) by default

Every class in Java can use methods defined in Object

Methods in subclasses can override methods in their parent class without being overridden

For example, when inheriting equals() from Object, which compares whether two references refer to the same Object, i.e. to the same piece of memory, subclasses can override equals() to change the comparison

Animal one=new Animal("花花",2); Animal two=new Animal(" flower ",2); boolean flag=one.equals(two); System.out.println(" Reference comparison of one and two "+flag); System.out.println("one ==two "+(one==two)); String str1=new String("hello"); String str2=new String("hello"); flag=str1.equals(str2); System.out.println("str1 and str2 reference comparison "+flag); System.out.println("str1 and str2 reference comparison "+(str1==str2)); public boolean equals(Object obj) { if(obj==null) { return false; Animal temp=(Animal)obj; // String is compared with equals (), If (this.getName().equals(temp.getName())&&(this.getMonth()==temp.getMonth()))) {return true; }else { return false; Public Boolean equals(Animal obj) {if(obj==null) {return false; } if(this.getName().equals(obj.getName())&&(this.getMonth()==obj.getMonth())) { return true; }else { return false; }}Copy the code

Can I compare both equals and both equals in an if statement?

Because equals() is overridden through the Animal type argument, the code runs automatically to locate the type matching method

Public String toString() {return "nickname: "+this.getName()+"; Age: "+ enclosing getMonth (); } /*toString test: * 1. The toString method of the class is called directly by default when the object name is printed. When inheriting the toString method of Object, it outputs the string representation of the Object: type information +@+ address information * 3. */ system.out.println (one.toString())); */ system.out.println (one.toString()); System.out.println(one); System.out.println(str1);Copy the code

== compares values stored in variables. For basic data types, variables store values. == compares whether values are equal. For reference data types (such as strings, custom classes, etc.), variables store the memory address of the object. By default, == compares whether the object refers to the same memory space, i.e. whether the address is the same.

final

Final Class: This class has no subclasses and cannot be inherited as public Final class or final public class

Subclass overrides are not allowed. Final precedes the return value of a method, and can be used by subclass inheritance without modifying the constructor

Variables are divided into the following two categories:

Local variables in final methods: as long as they are assigned before they are actually used, they are not allowed to be modified once assigned

Final class member attributes: Assignment process: 1. Define direct initialization 2. Constructor 3. Construct code blocks

The system will report an error if the value is not assigned

Prompt menu shortcut: Alt +/ can quickly generate rewrite method templates

Final Reference addresses cannot be modified after a reference data type is instantiated, but attribute values can still be changed

Static +final indicates globally disallowed content (methods and variables), such as configuration information

annotations

It can be declared in front of packages, classes, attributes, methods, local variables, method parameters, etc., to describe and comment these elements

These include: source annotations (@override), compile-time annotations (@notnull), and runtime annotations (even affecting the runtime logic @autowired).

Retained in the current phase, discarded in the next phase

By source: annotations from the JDK (@Override), annotations from a third party (@Autowired), and our own annotations

Yuan notes

Define annotations and annotate annotations

A static method of a parent class cannot be overridden

public static Animal create(){
    return new Animal();
}

public static Dog create(){
    return new Dog();
}
Copy the code

The reason for this is that the system considers create to be a method specific to the Dog class, independent of its parent class, and not a method rewrite