java: object-oriented-inheritance

concept

Inheritance is that the subclass inherits the characteristics and behavior of the parent class

Writing

class 父类 {
    
    
}
 
class 子类 extends 父类 {
    
    
}

For example

Public parent class:

public class Animal {
    
     
    private String name;  
    private int id; 
    public Animal(String myName, int myid) {
    
     
        name = myName; 
        id = myid;
    } 
    public void eat(){
    
     
        System.out.println(name+"正在吃"); 
    }
    public void sleep(){
    
    
        System.out.println(name+"正在睡");
    }
    public void introduction() {
    
     
        System.out.println("大家好!我是"         + id + "号" + name + "."); 
    } 
}

Penguins:

public class Penguin extends Animal {
    
     
    public Penguin(String myName, int myid) {
    
     
        super(myName, myid); 
    } 
}

Rats:

public class Mouse extends Animal {
    
     
    public Mouse(String myName, int myid) {
    
     
        super(myName, myid); 
    } 
}

Note: A class can only have one parent class, that is, java does not support multiple inheritance

Inheritance scope

The private modified properties or methods of the parent class cannot be inherited by the subclass.

Inherited keywords

Inheritance can use the two keywords extends and implements to achieve inheritance, and all classes are inherited from java.lang.Object, when a class does not inherit the two keywords, it inherits object by default (this class is in the java.lang package, so No need to import) ancestor class.

extends:

public class Animal {
    
     
    private String name;   
    private int id; 
    public Animal(String myName, String myid) {
    
     
        //初始化属性值
    } 
    public void eat() {
    
      //吃东西方法的具体实现  } 
    public void sleep() {
    
     //睡觉方法的具体实现  } 
} 
 
public class Penguin  extends  Animal{
    
     
}

implements:
Use the implements keyword to make java have multiple inheritance characteristics in disguise, the scope of use is the case of class inheritance interface, you can inherit multiple interfaces at the same time (the interface and the interface are separated by a comma).

public interface A {
    
    
    public void eat();
    public void sleep();
}
 
public interface B {
    
    
    public void show();
}
 
public class C implements A,B {
    
    
}

The super keyword:
Represents the reference of the parent class object.
To achieve access to the members of the parent class, used to refer to the parent class of the current object.
super() calls the constructor of the parent class, which must be in the first line

class Animal {
    
    
  void eat() {
    
    
    System.out.println("animal : eat");
  }
}
 
class Dog extends Animal {
    
    
  void eat() {
    
    
    System.out.println("dog : eat");
  }
  void eatTest() {
    
    
    this.eat();   // this 调用自己的方法
    super.eat();  // super 调用父类方法
  }
}

this keyword:
a reference to oneself, the example is the same as above.

Final keyword:
You can define a class as non-inheritable, that is, the final class; or it can be used to modify a method, which cannot be overridden by subclasses:

// 声明类
final class 类名 {
    
    //类体}
// 声明方法
修饰符(public/private/default/protected) final 返回值类型 方法名(){
    
    //方法体}

Note: Instance variables can also be defined as final, and variables defined as final cannot be modified. A method declared as a final class is automatically declared as final, but instance variables are not final

Constructor inheritance

father:

class SuperClass {
    
    
  private int n;
  SuperClass(){
    
    
    System.out.println("SuperClass()");
  }
  SuperClass(int n) {
    
    
    System.out.println("SuperClass(int n)");
    this.n = n;
  }
}

Subclass

// SubClass 类继承
class SubClass extends SuperClass{
    
    
  private int n;
  
  SubClass(){
    
     // 自动调用父类的无参数构造器
    System.out.println("SubClass");
  }  
  
  public SubClass(int n){
    
     
    super(300);  // 调用父类中带有参数的构造器
    System.out.println("SubClass(int n):"+n);
    this.n = n;
  }
}

run:

SubClass sc1 = new SubClass();
SubClass sc2 = new SubClass(100); 
// SuperClass()
// SubClass
// SuperClass(int n)
// SubClass(int n):100

Method override (override)

The method in the subclass has the same name, parameter list, and return type as the method in the parent class. We say that the subclass overrides the method of the parent class

  • The method is contained in the parent class
  • Same method name
  • The parameter list is the same
  • The return type is the same or subclass
  • Access modifiers cannot be reduced
  • The child class cannot throw an exception larger than the parent class
class SubClass extends SuperClass{
    
    
  // @override改注解表示当前的方法是一个重写方法,可有可无
  @override
  cry () {
    
    
    System.out.println("我是子类,重写了父类的cry方法。。。");
  }
}

Guess you like

Origin blog.csdn.net/weixin_43972437/article/details/113801756