Object-oriented 3 major features: encapsulation, inheritance, polymorphism - inheritance (overriding and initialization order of inheritance methods, final & super keywords, Object class)

Inheritance and method overriding

Inherited grammar rules:

Inheritance in java is single inheritance, a class has only one parent class

Benefits of inheritance:

The subclass has all the properties and methods of the parent class (the modifiers of the properties and methods cannot be private , otherwise they are invalid)

Implement code reuse

class subclass extends parent class

例:class Dog extends Animal{

  } //Dog inherits the Animal class

What is method overriding?

If the subclass is not satisfied with inheriting the method of the parent class, it can override the method inherited by the parent class, and the method of the subclass will be called first when called.

Grammar rules:

The return value type , method name, parameter type and number must be the same as the method inherited by the parent class, which is called method overriding.

 

Inherited initialization order

1. Initialize the parent class first and then initialize the subclass

2. Initialize the properties first and then initialize the constructor

 

public class Animal {
    public int age=10;
    public String name;
    public void eat(){
        System.out.println( "Animals have the ability to eat" );
    }
    public Animal(){
        System.out.println( "Animal class executed" );
        age=20;
    }
}
public class Dog extends Animal {
    public void eat(){
        System.out.println( "Dogs have the ability to eat" );
    }
    public Dog(){
        System.out.println( "Dog class executed" );
    }
}
public class Initial {
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Dog dog=new Dog();
        dog.eat();
        System.out.println(dog.age);
    }
}

 

final keyword

Using the final keyword as a marker has a "final" meaning  

final can modify classes, methods, properties, variables

final modified class, the class is not allowed to be inherited

final modified method, the method is not allowed to be overridden (overridden)

Final modification attribute, the attribute of the class will not be implicitly initialized (the initialization attribute of the class must have a value), or assigned in the constructor (but only one of them can be selected)

final modified variable, the value of the variable can only be assigned a value once, that is, a constant

 

super keyword

Used inside an object, it can represent the parent class object

1. Access the property super.age of the parent class

2. Access the super.eat() method of the parent class

During the construction of the subclass, the super() method of the superclass must be called;

If the constructor of the subclass does not explicitly call the constructor of the parent class, the system calls the constructor of the parent class without parameters by default.

If the constructor is called explicitly, it must be on line 1 of the subclass constructor

If the constructor of the subclass does not display the constructor of the parent class, and the parent class has no parameterless constructor, a compilation error occurs.

 

 

public class Animal {
    public int age=10;
    public String name;
    public void eat(){
        System.out.println( "Animals have the ability to eat" );
    }
    public Animal(int age){
        this.age=age;
    }
}
public class Dog extends Animal {
    public static int age=20;
    public void eat(){
        System.out.println( "Dogs have the ability to eat" );
    }
    public Dog(){
         // Call the parent class's parameterized constructor 
        super (age); // The age attribute of the input parameter must be added static 
        System.out.println("Dog class is executed" );
    }
    public void method(){
        System.out.println( super .age); // The property of the parent class is called 
        super .eat(); // The method of the parent class is called 
    }
}

 

Object class

The Object class is the parent class of all classes. If a class is not explicitly marked to inherit another class using the extends keyword, then this class inherits the Object class by default. Methods in the Object class, available to all subclasses.

Methods in the object class:

1toString()

When the toString() method is defined in the object class , it returns the hash code of the object (object address string)

Object properties can be represented by overriding the toString() method

Menu bar: source generate toString   automatically generates an overridden toString() method

 

 

@Override
    public String toString() {
        return "Dog [age=" + age + ", name=" + name + "]";
    }

 

2equals()

The comparison is whether the reference to the object points to the same memory address Dog dog=new Dog()

public static void main(String[] args) {
        // TODO Auto-generated method stub
        Dog dog=new Dog();
        Dog dog2=new Dog();
        if(dog.equals(dog2)){
            System.out.println( "Even objects are the same" );
        }else{
            System.out.println( "The two objects are different" );
        }
    }
>>> two objects are different

In general, when comparing two objects, compare whether their values ​​are consistent, so rewrite it!

Menu bar: source generate hashCode() and equals()  

Automatically generate overridden hashCode() and equals() methods

Notice:

The getClass() method is to get the class object, which is different from the class object (the created object is the class object). The class object obtains the attributes of the class, and the object of the class pays attention to the attribute value of the class! !

 

@Override
     public  boolean equals(Object obj) {
         if ( this == obj)
             return  true ;
         if (obj == null )
             return  false ;
         // Check if class objects are equal 
        if (getClass() != obj.getClass())
             return  false ;
        Dog other = (Dog) obj; // Force conversion to Dog type
         // Determine whether the object values ​​are equal 
        if (age != other.age)
             return  false ;
         return  true ;
    }

 

*** END

 

Guess you like

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