Java object-oriented class inheritance notes

Concept of inheritance:

Java allows a new class to be constructed on the basis of an existing class. The new class constructed is called a subclass or a derived class, and the existing class is called a parent class. The constructed subclass automatically has all inheritable properties and methods of the parent class. The keyword used for inheritance is extend.

Examples:

//定义一个Animal类,作为父类
public class Animal {
    
    
    String type;//种类
    String name;//名称
    //定义一个动物跑的方法
    public void run(){
    
    
        System.out.println("动物跑......");
    }
}

//定义Cat类继承Animal类
public class Cat extends Animal {
    
    
    public void print(){
    
    
        System.out.println("品种:" + type + ",名称:" + name);
    }
}
//测试类
public class Example09 {
    
    
    public static void main(String[] args) {
    
    
        Cat cat = new Cat();//创建一个Cat类的实例
        cat.type = "金毛";//为品种属性赋值
        cat.name = "嘟嘟";//为名称赋值
        cat.print();//调用自身的方法
        cat.run();//调用继承父类的方法
    }
}

operation result:
Insert picture description here

There is no run() method in the Cat class, but the method can be called, which is inherited from the parent class. There are several points to note in class inheritance:

  1. Java classes only support single inheritance and do not allow multiple inheritance, which means that a class can only inherit one direct parent class.
    Examples of errors:
class A{
    
    };
class B{
    
    };
class C extend A,B{
    
    }
  1. Multiple classes can inherit the same class.
class A{
    
    };
//类B,C同时继承A类
class B extend A{
    
    };
class C extend A{
    
    };
  1. Multiple inheritance is possible, and a parent class can inherit other parent classes. Subclass and parent class are relative concepts.
class A{
    
    };
class B extend A{
    
    };
class C extend B{
    
    };

Override the method of the parent class:

The subclass will automatically inherit all the properties and methods of the parent class. Sometimes the method of the parent class needs to be modified in the subclass, that is, the method of the parent class is overridden. The method overridden in the subclass needs to have the same name as the overridden method of the parent class, and the parameter types and method return values ​​are the same.

Examples:

//定义一个Animal类,作为父类
public class Animal {
    
    
    String type;//种类
    String name;//名称
    //定义一个动物跑的方法
    public void run(){
    
    
        System.out.println("动物跑......");
    }
} 

//定义Cat类继承Animal类
public class Cat extends Animal {
    
    
    public void print(){
    
    
        System.out.println("品种:" + type + ",名称:" + name);
    }
    //重写父类的run方法
    public void run(){
    
    
        System.out.println("金毛在奔跑");
    }
}
//测试类
public class Example09 {
    
    
    public static void main(String[] args) {
    
    
        Cat cat = new Cat();
        cat.type = "金毛";
        cat.name = "嘟嘟";
        cat.print();
        cat.run();
    }
}

Run result: The
Insert picture description here
result can be seen to rewrite the run() of the parent class, and the output result is the result after rewriting.
Come on! ! !

Guess you like

Origin blog.csdn.net/qq_42494654/article/details/109230444