final, override, polymorphism, abstraction, interface

Final is a modifier that represents the final and unchangeable
final decorated classes cannot be inherited
  
A member method of a final modifier cannot be overridden

The variable decorated with final cannot be assigned twice. If there is no default value, the assignment must be displayed
Generally, we call static variables modified by final as constants, that is, public static final data type variable name = value; Final is a modifier that represents the final and unchangeable
  
final decorated classes cannot be inherited
  
A member method of a final modifier cannot be overridden
  
The variable decorated with final cannot be assigned twice. If there is no default value, the assignment must be displayed
Generally, we call static variables modified by final as constants, that is, public static final data type variable name = value;

public class Final_01 {
	//The member variable modified by final has no default value and cannot be assigned twice
	//final int age;
	
	//Static variables modified by final have no default value and cannot be assigned twice. Generally, psf will appear together, which is called constant for short
	//Constant names are recommended to be all uppercase
	public final static int AGE=18;
	final static public void main(String[] args){
		//Variables decorated with final cannot be assigned twice
		//final int i=2;
		//i=3;
	}

}
//final decorated classes cannot be inherited
//final class A{}
//class B extends A{}
//A member method of a final modifier cannot be overridden
//classA{public final void m1(){}}
//class B extends A{
//    @Override
//public void m1(){}}

give an example

public class Final_02 {
	public static void main(String[] args){
		final Customer c=new Customer("Zhang San",18);
		//age and name can be changed without final modification
		c.age =19;
        c.name ="Li Si";
		//Because c is decorated with final, it cannot be changed
		//c=null;
		//final int i= 23;
		//i=1;
		System.out .println(c.name);
	}

}
class Customer{
	String name;
	int age;
	
	public Customer(String name,int age){
		super();
		this.name =name;
		this.age =age;
	}
}

2 polymorphism

2.1 what is it

A parent class reference points to a child class object

Parent class reference: refers to a reference type variable declared with a parent type

Point to: the object can be found through the memory address

Subclass object: heap memory object created by new subclass

Subclass variable = new subclass ();

Cat c = new Cat();

Parent type variable name = new subclass ();

Animal a = new Cat();

public class Poly_01 {
	public static void main(String[] args){
		Cat c = new Cat();
		//polymorphic
		Animal a = new Cat();
		a.eat();
		a = new Dog();
		a.eat();
	}

}
class Animal{
	public void eat(){
		System.out.println("Animals eat");
	}
	public void move(){
		System.out.println("Animals are moving");
	}
}
class Cat extends Animal{
	@Override
	public void eat(){
		System.out.println("Cats eat fish");
	}
}
class Dog extends Animal{
	
}

2.2 relevant knowledge

Six principles of software design:

1. Principle of single responsibility: single function embraces only one change

2 Richter substitution principle: when the parent class can be used, the child class must be used, because inheritance, the function of the parent class and the child class all have

3. Dependency Inversion Principle: details should rely on abstraction, while abstraction should not rely on details

4 interface isolation principle:

5 Demeter's Law: the principle of minimum knowledge, and other classes or objects, with as little understanding as possible

6. Opening and closing principle: it is closed for modification and open for extension

2.3 advantages

The same operation, acting on different objects, can have different explanations and produce different results. This is polymorphism. When there are many different implementation methods for an event, we choose to rely on the high level to embrace a variety of changes, or reduce the coupling between classes and details

2.4 disadvantages

Missing subclass specific properties

2.6 centralized form of polymorphism

public class Poly_04 {
        public static void main(String[] args){
            // 1 direct polymorphism
            Sup sup = new Sub();
            //2 formal parameters and arguments, the method parameter list is the parent type, and the calling method is transferred to the subclass object
            m1(new Sub());
            //3. The return value is polymorphic. The return value type is the parent type, but the child object is returned
            Sup result = m2();
        }
        public static Sup m2(){
            return new Sub();
        }
        public static void m1(Sup sup){
            
        }
}
class Sup{
    
}
class Sub extends Sup{
    
}


2.7 Instanceof

Judge whether an object is instantiated by a class

if(sup1 instanceof  SubClass){
//If yes, you can avoid type conversion if you are transforming downward

SubClass sub1 = (SubClass) sup1;

}

3 abstract

3.1 what is it

bstract: the class modified by the modifier is an abstract class, and the modified method is an abstract method
Abstract classes cannot instantiate objects
Abstract methods have no method body, only define functions, no implementation of functions, and abstract methods must be in abstract classes
Conversely, there can be no abstract methods in an abstract class
abstract and final cannot appear at the same time

3.2 usage syntax

abstract class Animals{
    //Animal eating function
    public abstract void eat();
    public void m1(){
        
    }
}
class Bird extends Animals{
 
    @Override
    public void eat() {
        System.out.println("Birds eat worms");
        
    } 
}
class Fish extends Animals{
 
    @Override
    public void eat() {
        System.out.println("Big fish eat small fish");
        
    }
    
}


4 Interface

4.1 what is it

An interface can be understood as a completely abstract class with only abstract methods and constants

However, starting from 1.8, static methods and default methods are allowed

Syntax modifier interface interface name {}

Abstract methods in the interface do not need to be modified with abstract. By default, all methods are public abstract

In the interface, there are no variables, only constants, and public static final can be omitted

Classes and interfaces are no longer inheritance relationships, but implementation relationships. Extensions are replaced by implements

Interface name variable = new subclass () is also polymorphic

A class can only inherit one class, but it can implement N interfaces separated by commas, which can solve the problem of weakening the function of single inheritance

Class class name implements interface 1, interface 2, interface 3 {}

Between interfaces, there are multiple inheritance, which are separated by commas

Interface interface name extends parent interface name 1, parent interface name 2 {}

If a class implements an interface, it must implement all abstract methods in the interface Otherwise, this class needs to be modified with abstract

An abstract class implements an interface and can implement 0~N abstract methods

1.7 only abstract methods can be used

1.8 there can be either static methods or default methods (it can be understood as member methods)

Static method, which can be called with the interface name

The default method needs to be called by the same sub implementation class, and can also be overridden

1.9 start supporting private methods

4.2 application method

interface A{
        public static final String name = "xx";
        //psf can be omitted
        int age =3;
        
        public abstract void m1();
        //public and abstract can be omitted
        void m2();
        public default void m3(){
            System.out.println("Default method");
        }
        public static void m4(){
            System.out.println("Static method");
        }
}
interface B{
    
}
//Multiple inheritance
interface C extends B,A{
    
}
//Multiple implementations, all abstract methods need to be implemented
class D implements A,B,C{
 
    @Override
    public void m1() {
        // TODO Auto-generated method stub    
    }
    @Override
    public void m2() {
        // TODO Auto-generated method stub    
    }
    }
//Abstract class, which can implement 0~N abstract methods
abstract class E implements A,B,C{
    
}


5 Object

5.1 Equals

Object is the ancestor of all classes and the root class provided in java
When a class does not inherit from another class, it inherits object by default
Object xx = new xxx(); Polymorphism can occur
= = when comparing basic types, compare the straight size, but when comparing reference types, compare the memory address
Comparing memory addresses is of no value. We generally compare the attribute values of two objects to see whether they are consistent, rather than whether the addresses of two objects are consistent
equals(): this method is designed to compare whether two objects are equal, but the default comparison address is
For the equals method in Object in java, the default comparison memory address (= =) needs to be filled in according to our needs

public static void main(String[] args){
        Student s1  = new Student(1,"Zhang San");
        Student s2  = new Student(1,"Zhang San");
        //false
        System.out.println(s1 == s2);
        System.out.println(s1.equals(s2));
    }
    
}
class Student{
    int id;
    String name;
    public Student(int id, String name){
        super();
        this.id = id;
        this.name = name;
    }
        // Requirement: if two students have the same ID, they are considered to be the same student
        // Requirement: if two students have the same ID and name, they are considered to be the same student
    public boolean equals(Object obj){
        //Judge whether it is the same object
        if(this == obj){
            return true;
        }
        //Downward transformation
        if(obj instanceof Student){
            Student s2 = (Student) obj;
            // The equals method is overridden in the String class, and the values are compared
            if(this.id == s2.id&&this.name.equals(s2.name)){
                return true;
            }
        }
        return false;
    }
}


5.2Finalize

finalize: this method will be called automatically when the garbage is collected, and the unordered programmer will call it manually
Garbage: when an object has no more references to it, the object is regarded as garbage data (that is, if an object is created, no one can find it)
 protected void finalize() throws Throwable { }
The finalize method in Object does nothing and needs to be rewritten as required
 

Keywords: Eclipse

Added by suave4u on Wed, 12 Jan 2022 22:29:03 +0200