编程知识 cdmana.com

Java inheritance

{tabs-pane label="private"}

Inheritance is a very powerful mechanism in object-oriented programming , It can first reuse code . When we let Student2 from Student inheritance ,Student2 You get it Student All functions of , We just need to Student2 Write new functions . By inheritance ,Student2 Just write additional functions , There is no need to repeat the code .

stay OOP In terms of , We put Student It's called superclass (super class), Parent class (parent class), Base class (base class), hold Student2 Called subclass (subclass), The extension class (extended class).

Java Only one is allowed class Inherited from a class , therefore , A class has and only has one parent class . Only Object special , It has no parent class .

Student stu=new Student();
       stu.setName("admin");
       System.out.println(stu.getName());
       Student2 stu2=new Student2();
       stu2.setName("admin");
       stu2.setAge(123);
       stu2.setClas("Iot1903");
       stu2.setId(1001);
       System.out.println(stu2.getName()+" "+stu2.getAge()+" "+stu2.getclas()+" "+stu2.getId());
// Inherit 
class Student{
    private String name;
    private int age;
    private String clas;
    public String getName(){return this.name;}
    public void setName(String name){this.name=name;}
    public int getAge(){return this.age;}
    public void setAge(int age){this.age=age;}
    public String getclas(){return this.clas;}
    public void setClas(String clas){this.clas=clas;}
}
class Student2 extends Student{
    // Inherited Student All the functions in , We just need to add new features 
    private int id;
    public int getId(){return this.id;}
    public void setId(int id){this.id=id;}
} 

{/tabs-pane} {tabs-pane label="private effect "}

{/tabs-pane}

{tabs-pane label="protected"} Inheritance has a characteristic , Is that the child class cannot access the parent class Student Field or private Method . for example ,Student2 Class cannot access Student Class name and age word , This weakens the role of inheritance . In order for the subclass to access the fields of the parent class , We need to take private Change it to protected. use protected The decorated fields can be accessed by subclasses :

class Student {
    protected String name;
    protected int age;
}

class Student2 extends Student {
    public String hello() {
        return "Hello, " + name; // OK!
    }
}

therefore ,protected Keywords can control the access rights of fields and methods within the inheritance tree , One protected Fields and methods can be subclassed , And the subclass of the subclass . {/tabs-pane} {tabs-pane label="super"} super Keyword represents parent class ( Superclass ). When a subclass references a field of a parent class , It can be used super.fieldName.

class Student2 extends Student {
    public String hello() {
        return "Hello, " + super.name;
    }
}
// Use here super.name, perhaps this.name, perhaps name, The effect is the same . The compiler will automatically locate the parent class name Field .

At some point , Must use super

public class Main {
    public static void main(String[] args) {
        Student2 s = new Student2("lq", 12, 89);
    }
}

class Student {
    protected String name;
    protected int age;

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

class Student2 extends Student {
    protected int score;

    public Student(String name, int age, int score) {
      
      // Because in Java in , whatever class Construction method of , The first line of statements must call the constructor of the parent class . If the constructor of the parent class is not explicitly called , The compiler will automatically add a sentence for us super();
        //super(); Because in Student There is no parameterless constructor in class , So this sentence will report an error    Should be replaced by the following :
        super(name,age);      
        this.score = score;
    }
}

If the parent class does not have a default constructor , Subclasses must explicitly call super() And give the parameters so that the compiler can locate a suitable construction method of the parent class . That is, the subclass does not inherit any of the parent's construction methods . The default constructor for subclasses is generated automatically by the compiler , It's not inherited .

{/tabs-pane} {tabs-pane label="final"} final Subclasses are not allowed to override methods of the parent class

    final Modified classes cannot be inherited   final class Student{}  uninheritable 
    final Modified fields cannot be modified after initialization ,
        public final String name=" I can't be modified ";
         Fields can be initialized in the constructor 
         Ensure that once the instance is created , Its final The field cannot be changed 
// Class cannot be inherited 
final class Student {
    protected String name;
}

// Methods cannot be overridden by subclasses 
class Student {
    protected String name;
    public final String hello() {
        return "Hello, " + name;
    }
}
Student extends Student {
    // compile error:  Override not allowed 
    @Override
    public String hello() {
    }
}

class Student {// Field 
    public final String name = "Unamed";
}
// Field reassignment 
Student p = new Student();
p.name = "New Name"; // compile error!

{/tabs-pane}

This article altogether 635 Number of words , Average reading time ≈ 2 minute

Participation of this paper Tencent cloud media sharing plan , You are welcome to join us , share .

版权声明
本文为[await]所创,转载请带上原文链接,感谢
https://cdmana.com/2021/09/20210909125824200p.html

Scroll to Top