java se part review of object-oriented inheritance characteristics

  • Inheritance is one of the three characteristics of the object-oriented, so-called inheritance, it can be simply understood as the subclass inherits the parent class, sub-class has properties and methods of the parent class, subclass can modify the properties and methods of the parent class, subclass can A method of increasing and properties;
  • Through inheritance, we can simplify the design class, which directly owns the methods and properties of a class
  • Through inheritance, we can some of the classes have the same attributes and behavioral characteristics (method) extracted, abstracted as a parent, so the parent class has these properties and methods, so that when defining other subclasses inherit the parent class ways to simplify design, to inherit this feature;
  • For example, many real-life examples, we will certainly see a variety of animals in the zoo, the animals have some features (can be understood as the attribute of java) and behavior (can be understood as a method in java), For example, animals have age, name and other attributes, eat, drink, sleep and other behavioral characteristics, we put these properties and methods defined as a class of animals, then let all kinds of animals (cats, dogs) animals inherit this class to simplify class design, while writing the code can be simplified;
//动物类
public class Animals {
    String name;
    int age;
    String sex;

    public Animals() {
    }

    //父类中的方法
    public void eat(){
        System.out.println(this.name+"正在吃饭");
    }
    public void drink(){
        System.out.println(this.name+"正在喝水");
    }
    public void sleep(){
        System.out.println(this.name+"正在睡觉");
    }

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }

    public Animals(String name, int age, String sex) {
        this.name = name;
        this.age = age;
        this.sex = sex;
    }

    public Animals(String name) {
        this.name = name;
    }
}

	
public class Cat extends Animals {
    public Cat(String name, int age, String sex) {
        super(name, age, sex);
    }

    public Cat(String name) {
        super(name);
    }
    //重写方法
    @Override
    public void eat() {
        super.eat();
    }

    @Override
    public void drink() {
        super.drink();
    }

    @Override
    public void sleep() {
        super.sleep();
    }
}

public class Test {
    public static void main(String[] args) {
        Cat cat = new Cat("波斯猫");
        cat.eat();
        System.out.println("吃饱了,喝点水");
        cat.drink();
        System.out.println("吃饱喝足之后,躺会");
        cat.sleep();
    }
}

Operating results as follows:
Here Insert Picture Description

  • The above examples I personally feel that we can inherit the characteristics of a good understanding of it

  • Features on java inherited

    • java is single inheritance, so-called single inheritance, is a subclass can only inherit a parent, can not inherit multiple parent classes
    • supports multiple inheritance in java, a class can be more than one class of his father, it can be a subclass of class
  • When using inheritance, the subclass should be more detailed and specific parent class, we can use is the keyword to determine whether you can use inheritance

  • The method of rewriting
    in an inheritance relationship, the parent class subclasses coverage;

    • The same method name,
    • The same parameters
    • The return value type is compatible with the type of parent class must be able to accommodate the type of subclass
  • About inheritance

    • Some parent class, subclass must have;
    • No parent class, a subclass can be increased;
    • Some parent class, subclass can override the modified
  • super keyword
    target super keyword represents the parent class reference in the subclass, properties and methods of the parent object can be accessed by super;
    create the Cat class time on it, I used only a constructor parameter name, the parent animals, I did not write the constructor name only, so the error, I found that the constructor has inherited in several places that require special attention

  • Instructions on inheritance constructor

    • In the hierarchy, the configuration method can not be inherited;
    • When the sub-class constructor call, will first call the constructor of the parent class;
    • If the parent class constructor is not specified when calling the constructor in the subclass, no default constructor parameters using the parent class
    • Supper may be used (parameter) is configured to call the parent class specified, super () statement must be performed on the first line
  • Method overloading and method overrides the difference

    • Method overloading is in a class, the same method name, a number of different methods method parameter;
    • The method of rewriting under inheritance ,, subclass overrides the parent class method, requires the same name, the same arguments, return type is less than the parent's return type can be compatible with the type of the return value;
Published 19 original articles · won praise 13 · views 499

Guess you like

Origin blog.csdn.net/qq_45309297/article/details/105067287