A class can have two important types of members: member variables and methods. Some of the members of a subclass are declared and defined by the subclass itself, and the other part is inherited from its parent class. So,
What is inheritance? The so-called subclass inherits the member variable of the parent class as its own member variable, just as they are declared directly in the subclass, and can be operated by any instance method defined by the subclass itself, that is, the member inherited by a subclass should be a fully meaningful member of the class. If the instance method defined in the subclass cannot operate on a member variable of the parent class, the member variable will not be inherited by the subclass; The so-called subclass inherits the method of the parent class as a method in the subclass, as if they were defined directly in the subclass, and can be called by any instance method defined by the subclass itself.
Use inheritance
Write Parent Class
[Access Modifier] class Pet {//Public Attributes and Methods}
Write a subclass and inherit the parent class
[Access Modifier] class Dog extends Pet {//Subclass specific properties and methods}
Inheritance is one of the important means to realize code reuse in Java. Java only supports single root inheritance, that is, a class can only have one direct parent class
Case:
package cn.bdqn.demo04; public class Pet { private String name; private int health; private int love; // Add construction method public Pet() { } public Pet(String name, int health, int love) { this.name = name; this.health = health; this.love = love; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getHealth() { return health; } public void setHealth(int health) { this.health = health; } public int getLove() { return love; } public void setLove(int love) { this.love = love; } public void print() { System.out.println("Pet information: nickname:" + this.getName() + ",Health value:" + this.getHealth() + ",Intimacy:" + this.getLove()); } }
package cn.bdqn.demo04; public class Cat extends Pet { // Define attributes unique to the Cat class private String color; public Cat() { super();// Call the parameterless constructor in the parent Pet class } public Cat(String name, int health, int love, String color) { super(name, health, love); this.color = color; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } }
package cn.bdqn.demo04; public class Dog extends Pet { //The string attribute is a unique attribute in the DOg class. Other classes do not have it, so it should be defined in the Dog class private String strain; public Dog() { } public Dog(String name, int health, int love, String strain) { super(name, health, love); this.strain = strain; } public String getStrain() { return strain; } public void setStrain(String strain) { this.strain = strain; } }
package cn.bdqn.demo04; public class Penguin extends Pet { // Define the unique properties in the Penguin class private String sex; public Penguin() { super();// super represents the parent class Pet object, so this place represents calling the parent class's parameterless constructor } public Penguin(String name, int health, int love, String sex) { super(name, health, love);// Represents calling the parametric constructor of the parent Pet class this.sex = sex; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } }
package cn.bdqn.demo04; public class Test { public static void main(String[] args) { // Create a Dog class object Dog dog1 = new Dog("Prosperity", 99, 99, "Golden hair"); //There is no setName() method in the Dog class, but the DOG class object dog1 can be called. Because the Dog class inherits the Pet class, dog1 can call the setName() method in the parent Pet class dog1.setName("Laifu"); dog1.print(); //Create Penguin class object Penguin penguin1 = new Penguin("QQ", 98, 100, "common"); penguin1.setHealth(100); penguin1.print(); //Create Cat Class Objects Cat cat1 = new Cat("Tom", 100, 80, "blue"); cat1.print(); //hashCode() does not exist in Cat, nor does it exist in the parent class Pet of Cat. hashCode() method exists in the Object class. The Object class is the default parent class of the Pet class. Therefore, Cat class objects can use the methods in the Pet class or the parent class of the Pet class //Declare a class. If the parent class is not specified, the system will let this class inherit the Object class by default int num =cat1.hashCode(); System.out.println(num);//1694203642 } }
super
Subclass Access Parent Class Members
Access the parent class constructor
super(); super(name);
Accessing Parent Class Properties
super.name;
Accessing parent methods
super.print();
super keyword:
(1) Use the super keyword. The super represents the parent object
(2) Called in the subclass construction method and must be the first sentence
(3) The properties and methods defined as private in the parent class cannot be accessed
super keyword to access the members of the parent class
super can only appear in methods and constructors of subclasses
When super calls the constructor, it can only be the first sentence
super cannot access the private member of the parent class
Calling rules of construction methods under inheritance conditions
The subclass constructor does not explicitly call the parent class's parameterized constructor through super, nor does it explicitly call its own other constructor through this
The system calls the parameterless constructor of the parent class by default
The subclass constructor explicitly calls the parent class's parameterized constructor through super
Execute the corresponding constructor of the parent class, but not the parameterless constructor of the parent class
The subclass constructor explicitly calls its own other constructor through this, and applies the above two rules in the corresponding constructor
What does the subclass inherit from the parent class?
Inherit the properties and methods modified by public and protected, regardless of whether the subclass and parent are in the same package
Inherit the properties and methods modified by the default permission modifier, but the subclass and parent must be in the same package
Case:
package cn.bdqn.demo05; public class Father { private String name; private int age; public double height; public Father() { super();//Call the parameterless constructor of the parent Object class of the Father class System.out.println("I am Father Construction method without parameters in class"); } public Father(String name, int age,double height) { super();//Call the parameterless constructor of the parent Object class of the Father class this.name = name; this.age = age; this.height = height; System.out.println("I am Father Parametric construction methods in classes"); } 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; } private void test(){ System.out.println("I am Father Private in class test()method"); } public void print(){ System.out.println("I am Father Public in class print()method"); } }
package cn.bdqn.demo05; public class Son extends Father { private double money; public Son() { super();//The no parameter constructor in the parent class Father class of the called Son class System.out.println("I am Son Construction method without parameters in class"); } public Son(String name, int age, double height, double money) { super(name, age, height);//Call the parameter constructor in the Father class, the parent class of the Son class this.money = money; System.out.println("I am Son Parametric construction methods in classes"); } public double getMoney() { return money; } public void setMoney(double money) { this.money = money; } public void printInfo(){ //Output the attributes in the parent class //super cannot directly call the attribute modified by private in the parent class // System.out.println(super.name); // System.out.println(super.age); System.out.println(super.height); //super cannot directly call the method modified by private in the parent class // super.test(); super.print(); } }
package cn.bdqn.demo05; public class Test { public static void main(String[] args) { // Create objects using the nonparametric construction method of the Son class Son son1 = new Son(); //How many times has the constructor been called to create a Son class object??? 3 times System.out.println("----------------"); Son son2 = new Son("Zhang San", 22, 173, 999); son2.printInfo(); } }
A member of a parent class that cannot be inherited
private member
The child class and parent class are not in the same package, and the member using the default access permission
Construction method
When to use inheritance?
Inheritance is similar to the real world
As long as we say "cats are mammals", many attributes and behaviors of cats are self-evident
Tibetan mastiff is a kind of dog
Designs that conform to the is-a relationship use inheritance
Inheritance is a way of code reuse
Put the properties and behaviors shared by the subclass into the parent class