Inheritance, super and this, abstract class

First, inheritance

1. Introduction to inheritance

① Multiple classes have the same properties and behaviors. Extract these properties and behaviors into a single class, and then inherit the class. There is no need to define these properties and behaviors.
② Inheritance is that the child class inherits the non private properties and methods of the parent class.
Advantages
    *Improved code reusability
    *Inheritance is the premise of polymorphism

2. Use of inheritance

① format
    public class Parent class { ··· }
    public class Subclass extends Parent class { ··· }
② Member variables
    * It doesn't matter if you don't have the same name. Whoever you use is who you are.
    * Duplicate name new Who is who ( In general, it will not be renamed in development ),In this case, if you want to use the non private member variable of the parent class, you need to use the super.The name of the parent class member variable.
③ Member method
    * It doesn't matter if you don't rename it. That method is the one you use.
    * If the duplicate name is a subclass, the method of the parent class will be overridden (overridden), declared unchanged, and implemented again.
④ Construction method
    * The constructor name is the same as the class name, so it cannot be inherited.
    * The constructor is used to initialize member variables, so in the process of subclass initialization, the parent class must be initialized before the child class. That is, when calling the constructor, the constructor of the parent class must be called first.
    * By default, the empty parameter construction of a subclass will write super(),Indicates that the constructor of the parent class is called. The member variables of the parent class can only be used by the child class after initialization.
⑤ Example
    //Parent class, mobile class
    public class Phone {
        //Member variables
        private String brand;

        private String price;
 
        //Construction method
        public Phone() {
        }
 
        public Phone(String brand, String price) {
            this.brand = brand;
            this.price = price;
        }
 
        //Member method
        public void call() {
            System.out.println("? Phone!");
        }
 
        public void send() {
            System.out.println("? Send message!");
        }
 
        public void show() {
            System.out.println(brand + "Brand mobile phone, price" + price);
        }
    }
 
    //Subcategory, Huawei Mobile
    public class HonorPhone extends Phone{
        //Construction method
        public HonorPhone() {
            //super() by default;
        }
 
        public HonorPhone(String brand, String price) {
            super("Lenovo P30 pro","9999");
        }
 
        //Override parent method
        @Override
        public void call() {
            super.call();   //Call the original call() method of the parent class
 
            System.out.println("new? Display name! ");
 
            System.out.println("new? display picture! ");
        }
 
        //Subclass specific methods
        public void inter() {
            System.out.println("new? Surf the Internet!");
        }
    }
 
    //Test class
    public class test {
        public static void main(String[] args) {
            //new object
            HonorPhone hp = new HonorPhone();
 
            //Call parent method
            hp.send();
            hp.call();
            //Call subclass override method
            hp.show();
            //Call subclass specific methods
            hp.inter();
        }
    }

3. Precautions

① The method of subclass overriding parent class is to ensure that the permission of subclass method is greater than that of parent class.
Subclasses cover the parent class method to ensure that the permissions, return values, method names, and parameter lists are exactly the same.
③ Class inheritance only supports single inheritance, not multiple inheritance
④ The parent of all classes is Object

2, super and this

1. meaning

① super 
    Representing parent class
② this  
    Represents the current class

2. usage

① super
    * Calling parent class member variables in subclasses
        super.Member variables.
    * Call the member method of the parent class in the child class
        super.Legal name of member(). 
    * Calling the parent class construction method in the subclass construction method
        super( parameter list );
② this
    * Call member variable of this class
        this.Member variables
    * Call member methods of this class
        this.Member method();
    * Calling this class construction method in the construction method
        this.Class name( parameter list );
③ Example
    //Parent class, mobile class
    public class Phone {
        //Member variables
        private String brand;

        //Construction method
        public Phone() {
        }
 
        public Phone(String brand, String price) {
            this.brand = brand;
        }
 
        //Member method
        public void call() {
            System.out.println( brand + "? Phone!");
        }
 
    }
 
    //Subclass, Lenovo Mobile
    public class LenovoPhone extends Phone{
        //Subclass empty parameter construction call parent class parameter construction
        public LenovoPhone() {
            super("Lenovo Z90");
        }
    }
 
    //Subcategory, Huawei mobile line
    public class HonorPhone extends Phone{
        //This class of empty parameter construction calls this class of parameter construction
        public HonorPhone() {
            this("Lenovo P30 pro","9999");
        }
 
        //Subclass has parameter construction call parent class has parameter construction
        public HonorPhone(String brand, String price) {
            super(brand, price);
        }
    }

3, Abstract class

1. Introduction to abstract classes

① Abstract method
    *Methods without method bodies decorated with abstract are called abstract methods.
② Abstract class
    *A class that contains abstract methods is called an abstract class.

2. Use of abstract classes

Format 1.
    *Modifier class abstract class name{
        Modifier abstract returns the value type method name (); 
      }
        Example: public class Abstract abstractdemo{
                public atstract void method();
           }
Attention
    *Abstract classes cannot create objects.
    *Abstract classes can have construction methods, which are used to initialize the member variables of the parent class when the child class creates objects.
    *A subclass needs to override all the abstract methods of the parent class, unless the subclass is also an abstract class.
    *Abstract methods must be in abstract classes, which do not have to have abstract methods.
    *Finally, there must be a subclass to implement all the abstract methods of the parent class.
Examples
    //Parent class
    public abstract class Fu {
        public abstract void num();
    }

    //Subclass, implement parent
    public class Zi extends Fu{
        public void num() {
            System.out.println("subclass");
        }
 
    }

Keywords: Programming Mobile

Added by ssmK on Wed, 12 Feb 2020 15:09:29 +0200