Java se review and consolidation

Day 5: object oriented inheritance final abstract class interface polymorphism
1. Inheritance: when multiple classes (subclasses, also known as derived classes) have the same attributes and methods, we extract these commonalities and put them into another class (parent class, also known as base class). This class is called parent class (base class). These multiple classes generate a child parent relationship (i.e. son and father relationship) with another class through the extends key word.
The subclass inherits the parent class. It is commonly understood that the son inherits the father's things (property)
Inherited features in java:
1. Only single inheritance is supported in Java (a subclass can inherit only one parent class and only one grandfather)
2. Support multi-level inheritance
 
Characteristics of member variables in inheritance relationship:
1. In inheritance, a subclass can only inherit non private members of the parent class
2. In the child parent relationship, when the child has the same name as the parent member variable, the child's own member variable is used
super: the reference of the parent class (can not be directly regarded as the parent object, it is equivalent to a root)
Pointer to parent class)
example:
 
public class ExtendsTest {
    public static void main(String[] args) {
        Zi zi = new Zi();
        System.out.println(zi.name);

    }
}
class Zi extends Fu{
    String name="Son's name";
    public void method(){
        String name="Name of local variable";
        System.out.println(name);//Name of local variable
        System.out.println(this.name);//The reference of the current object, and the name of the current object
        System.out.println(super.name);//The reference of the parent class, pointing to the name in the parent class
    }

}
class Fu{
    String name="Dad's name";
}
 
Characteristics of member method in inheritance relationship:
1. In inheritance, a subclass can only inherit non private member methods of the parent class
2. When the method of the parent class cannot meet the needs of the child class itself, we will rewrite the method of the parent class (method body) to meet the needs of the child class itself (this is method rewriting)
3. When the child parent class member method has the same name, the method overridden by the child class is used
(caused by method rewriting - premise (when the method of the parent class cannot meet the needs of the child class itself))
Method overload?
In the same class, the method name is the same and the parameter list is different (number, order and type), which is independent of the return value
Method override? In the child parent relationship, when the method of the parent class cannot meet the needs of the child class itself, the child class rewrites the method of the parent class, in which the method body meets the needs of the child class itself.
4. Premise of method Rewriting: when the method of the parent class cannot meet the needs of the child class itself
5. Flag of method Override: @ Override
As like as two peas, the method of rewriting subclasses is exactly the same as the method declaration of the parent class.
 
 
Characteristics of construction method in inheritance relationship:
1. The constructor of the parent class is not directly inherited by bytes, but the constructor of the child class will call the constructor of the parent class through super()
2. In the child parent relationship, why do you call the constructor of the parent class first when calling the constructor of the child class? (Extended)
In the inheritance relationship, because there may be member variables in the parent class, and the child class can inherit and directly use the non private member variables of the parent class, the non private member variables of the parent class must be initialized or assigned first by relying on the parameterless or parameterless construction
3. The construction method of manually calling the parent class should be placed in the first line of valid code. Only one construction method of the parent class can be called at a time
4. Each constructor of a subclass can call the constructor of the parent class
 
Advantages and disadvantages of inheritance:
1. Advantages
1. Improve code reusability
2. Improve program maintainability
 
Disadvantages: low cohesion and high coupling
 
Development principle: high cohesion and low coupling
Cohesion: the ability of each class to accomplish things independently
Coupling: association between classes
 
 
2. final: final, used to modify variable method classes
Modified variables, variable constants, modified methods cannot be overridden, and modified classes cannot be inherited
Constants can be inherited
Constant name: every word should be capitalized. If there are multiple words to use_ link
final int AGE=20; final MAX_AND_MIN
final modified variables are generally used in combination with static
If static modification is used, the subclass object, subclass name and parent class name can be called
 
3. Abstract class
Abstract method: when there are similar methods in multiple subclasses, the commonness (method declaration) is extracted from the parent class - only the method declaration without a specific method body is an abstract method.
Abstract class: a class with abstract methods is an abstract class, which is decorated with the absrct keyword
 
When a class inherits an abstract class, it is either an abstract class or implements (overrides) all the abstract methods in the abstract class.
example:
public abstract class AbstractTest {
    String name;

    public  void eat() {
        System.out.println("Eat eat eat");
    }
    public abstract void run(); 
     
}
class A extends AbstractTest{
    @Override
    public void run() {
        System.out.println("Running,,,,,");
    }
}

 

characteristic:
1. Abstract classes cannot create objects (cannot be instantiated)
Why not instantiate?
1. Because the abstract class may find an abstract method, if it can be instantiated, the object can directly call the abstract method, and the abstract method has no method body and the call has no meaning.
2. Abstract classes and abstract methods need to be modified with the abstract keyword
3. Abstract methods can only be in abstract classes (or interfaces)
4. Non abstract methods can exist in abstract classes (convenient for subclass inheritance)
5. Must there be abstract methods in abstract classes? not always
6. When a single class inherits an abstract class, it either overrides all the abstract methods in the abstract class or becomes an abstract class itself.
 
What members can exist in an abstract class?
1. Abstract methods 2. Non abstract methods 3. Member variables 4. Constants 5. Construction methods exist. The purpose of construction methods is to assign initial and secondary values to member variables. 6. Static method
What keywords cannot abstract coexist with?
1. Cannot coexist with private
2. Cannot coexist with final
3. It cannot coexist with statc. If it coexists with these keywords, the abstract method is meaningless
4. Interface
Interface: in JAVA programming language, it is an abstract type and a collection of abstract methods. Interfaces are usually declared as interfaces. A class inherits the abstract methods of the interface by inheriting the interface.
Interfaces are not classes. The way of writing interfaces is very similar to classes, but they belong to different concepts. Class describes the properties and methods of an object. Interface contains the methods to be implemented by the class.
Interface: a class that is more abstract than an abstract class
Why is there an interface?
Because the previous inheritance can only be a single inheritance. All interfaces appear to solve this problem.
Interface format:
Interface interface name{
 
}
Before JDK 1.8, only abstract methods and constants could exist in the interface
JDK1.8 default, static modification methods can have specific methods
JDK1.9 private modification methods can have specific methods
 
Interview question: the difference between abstract classes and methods? Five stars
5. Polymorphism
Polymorphism: the same thing has different manifestations. For example, the forms of water are solid, liquid and gaseous
Water (liquid) --- water vapor (gaseous) --- ice (solid)
Teacher --- primary school teacher, middle school teacher, university teacher
Animals -- cats, dogs, pigs and sheep
Polymorphism has one premise:
1 there is an inheritance relationship (the implementation interface can also be regarded as a special inheritance)
2. Method rewriting (when inheriting abstract classes and implementing interfaces)
3.
Reference of parent class Animal a
Point=
Subclass object new (CAT)
example:
 
polymorphic
If a class we use later is an abstract class or interface, the abstract class and interface cannot be instantiated directly
However, we can access members of the parent class through polymorphism
example:
public class Demo5 {
    public static void main(String[] args) {
        //A reference to a parent class points to a child class object
        //A cat is an animal( a Don't just be a cat)
        Animal   a=new Cat();
        //Using member variables
        System.out.println(a.name);
        //Call method
        a.eat();
        a.method1();


    }
}
class Animal{
    String  name="Big dog";

    public  void eat(){
        System.out.println("very popular....");
    }

    public static  void method1(){
        System.out.println("This is the static method of the parent class...");
    }
}
class Cat extends Animal{
    String  name="Little flower cat";
    @Override
    public  void  eat(){
        System.out.println("Eat fish...");
    }

    //Static method

    public static void method1(){
        System.out.println("This is a static method of a subclass....");
    }

If you want to use members of subclasses, you can use cast types (up and down)

public class Demo6 {
    public static void main(String[] args) {
        //Polymorphism: the reference of the parent class points to the object of the child class
        Anima a=new Dog();//Upward transformation(Small to large)
        System.out.println(a.name);//In the parent class called here name attribute
        Dog dog=(Dog)a;//Downward transformation(Large to small)
        System.out.println(dog.name);//In the subclass called here name attribute
        a.eat();//Because dynamic binding(Override the method of the parent class)So here is to call the method in the subclass
        /*Summarize the advantages and disadvantages of polymorphism
         *       Disadvantages: you cannot directly access subclass specific members
         *  advantage:
         *        1.The advantage of inheritance is the advantage of polymorphism
         *        2.Improve program scalability
         * */
        //Production of Xiaomi mobile phone
        Factory  f=new Factory();
         /*  f.createXiaoMiNote(new XiaoMiNote());
           //Actual production of red rice
        f.createRedMiNote(new RedMiNote());*/
        f.createPhone(new XiaoMiNote());
        f.createPhone(new RedMiNote());
    }
    }
//Parent class
class Anima{
    String name="animal";
    //Because dynamic binding
    public void eat(){
        System.out.println("Eat eat eat.........");
    }
}
//Subclass
class Dog extends Anima{
    String name="Xiao Huang";
    public void eat(){
        System.out.println("Eat bones.........");
    }
}
//Mobile phone factory----Production of mobile phones
class   Factory{

   /* //Produce a batch of Xiaomi mobile phones
    public  void  createXiaoMiNote(XiaoMiNote xm){
        xm.call();
    }
    //Produce a batch of red rice mobile phones
    public  void  createRedMiNote(RedMiNote rm){
        rm.call();
    }*/
    //polymorphic
    public void  createPhone(Phone  p){
        p.call();
    }
}
//Mobile phone class
class  Phone{
    //Call function
    public void  call(){
        System.out.println("Phone call...");
    }
}
//Mi phones
class  XiaoMiNote extends Phone{

    //Call function
    public void  call(){
        System.out.println("Millet phone call...");
    }

}
//Red rice
class  RedMiNote extends Phone{
    //Call function
    public void  call(){
        System.out.println("Red rice phone call...");
    }
}
Characteristics of members in polymorphism:
1. Member variables
If it is only a child class, it cannot be used directly through the reference of the parent class
When compiling, you see the parent class, and when running, you see the parent class
2. Membership method
If it is only a child class, it cannot be used directly through the reference of the parent class
When compiling, you still see the parent class, and when running, you see the child class
(dynamic binding -- Method rewriting)
Review inheritance
Cat c=new Cat()
When the word parent class member variable has the same name, the member variable of the child class is used
When the child parent class member method has the same name, the member method of the child class is used (method override)
3. Static method
If it is only a child class, it cannot be used directly through the reference of the parent class
The parent class is still viewed at compile time, and the parent class is still viewed at run time
Animal a -------a or animal
The most important characteristics of polymorphism:
Child specific members cannot be accessed directly through a reference to the parent class.
Must access subclass specific members? Conversion of data types
Summarize the advantages and disadvantages of polymorphism
Disadvantages: you cannot directly access subclass specific members
advantage:
1. The advantage of inheritance is the advantage of polymorphism
2. Improve program scalability
 
Today's learning experience: there are many learning contents. You need to grasp the core, focus on learning, practice more, master the knowledge to be mastered, and deepen understanding. Learning is sometimes very boring, but if you study hard and master the technology, you still have a sense of achievement. The last sentence is life and struggle!

Keywords: Java JavaSE

Added by bigsid on Sat, 04 Dec 2021 20:47:15 +0200