This article brings you an introduction to what Java inheritance is? How to use inheritance in Java? , teach novices how to use inheritance and understand the concept of inheritance. Master access modifiers and master the usage of final keyword. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
First let’s take a look at these two classes:
public class Teacher { private int teachingAge; private String name; private int age; public void teach() { } public void seyHi() { System.out.println("我是:"+this.name); } }
public class Student { private int studentNo; private String name; private int age; public void learn() { } public void seyHi() { System.out.println("我是:"+this.name); } }
The Student class and the Teacher class have some similarities Properties and methods, these are duplicate codes. When there are a large number of classes in a program, a large amount of duplicate codes will be generated. Can these repeated codes be extracted and used by other classes to simplify them? That is to use to inherit .
After using inheritance optimization:
Create the inherit package
Parent class: (public code class)
package inherit; public class People { private String name; private int age; public void sayHi() { 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; } }
Subclass: Student.java
package inherit; public class Student extends People{ private int studentNo; public void learn() { System.out.println("学习课程"); } public int getStudentNo() { return studentNo; } public void setStudentNo(int studentNo) { this.studentNo = studentNo; } }
Subclass: Teacher.java
package inherit; public class Teacher extends People{ private int teachingAge; public void teach() { System.out.println("教授课程"); } public int getTeachingAge() { return teachingAge; } public void setTeachingAge(int teachingAge) { this.teachingAge = teachingAge; } }
Test class:
package inherit; public class TestInherit { public static void main(String[] args) { //创建Student对象 Student stu=new Student(); stu.setName("张三");//父类中继承过来的方法 stu.learn();//子类中特有的方法 stu.sayHi(); //创建Teacher对象 Teacher teacher=new Teacher(); teacher.setName("汤尼"); teacher.setTeachingAge(2);//子类中特有的方法 teacher.sayHi(); } }
Observing the above example code we found:
1. All public codes of the subclass can be placed In the parent class
2. The subclass can have its own unique methods and attributes
3. Once the subclass inherits the parent class, it will have the attributes and methods of the parent class
4. Put the public code into the parent class to make it easier to modify the code uniformly
Keywords: extends
1. Write a parent class
public class 父类{ //公共的属性和方法 }
2. Write a subclass and inherit the parent class
public class 子类 extends 父类{ //子类特有的属性和方法 }
The subclass can only inherit one parent class
If a subclass wants to access members of the parent class, it must use the super
keyword. super
represents the parent class object
to access the parent class structure Method:
super();//访问无参构造 super(参数);//访问有参构造
Access parent class attributes:
super.name;
Access parent class method:
super.print();
Access parent class constructor, must be called in child class constructor method, Must be the first sentence
super can only appear in methods and constructors of subclasses
super cannot access private members of the parent class
Knock knock: visit parent class members
Create the package visitparent and create the following class under the report
Parent class
package visitparent; public class Animal { private String name; private int legs; public Animal() { this.name="无名"; this.legs=4; } public Animal(String name,int legs) { this.name=name; this.legs=legs; } public void eat(String food) { System.out.println(name+" 吃食物:"+food); } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getLegs() { return legs; } public void setLegs(int legs) { this.legs = legs; } }
Subclass
package visitparent; public class Cat extends Animal{ private String hairColor;//毛发颜色 private int age; public Cat () { super();//调用父类无参 } public Cat(String name,int legs,String hairColor,int age) { super(name, legs);//这里调用相当于重用父类构造方法了 this.hairColor=hairColor; this.age=age; //super(name, legs);//去掉注释试试 //this.name="无名";//去掉注释试试 } public void catchMouse() { System.out.println(super.getName()+":抓老鼠"); } public void paly() { System.out.println(super.getName()+" 玩累了。"); super.eat("小鱼干"); } public String getHairColor() { return hairColor; } public void setHairColor(String hairColor) { this.hairColor = hairColor; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
Subclasses cannot inherit private members of parent classes
Subclasses cannot inherit members of different packages that use default access permissions (the default access permissions do not write access modifiers )
Subclasses cannot inherit the constructor of the parent class
When creating a subclass, the parent class doing what?
Create three classes below to observe the execution process. Class C inherits class B, and class B inherits class A.
A.java
public class A { public A() { System.out.println("A类的无参构造函数执行"); } }
B.java
public class B extends A{ public B() { System.out.println("B类的无参构造函数执行"); } }
C.java
public class C extends B{ public C() { System.out.println("C类的无参构造函数执行"); } }
TestRunFlow.java test class, showing the running results
public class TestRunFlow { public static void main(String[] args) { C c=new C(); } }
The running result is:
The no-parameter constructor of class A is executed
The no-parameter constructor of class B is executed
The no-parameter constructor of class C is executed
If the subclass constructor explicitly calls the corresponding constructor of the parent class through super, the parent class's parameterless constructor will not be executed.
The subclass constructor will call the parent class's parameterless constructor by default. Method
Call the parameterless constructor of the parent class until the parameterless constructor of the top-level parent class Object class is executed
According to the above rules, judge Can the following code be compiled and passed
parent class
public class Pet { private String name; public Pet(String name) { this.name=name; } }
subclass
public class Dog extends Pet{ }
The answer is no. There are only parameterized constructors and no parameterless constructors in the parent class. No code in the class has an implicit parameterless constructor by default. The parameterless constructor of the subclass calls the parameterless constructor of the parent class by default. However, there is no parameterless constructor in the parent class, so an error is reported in the child class.
Solution: 1. Explicitly add a parameterless constructor in the parent class, 2. Explicitly call the parent class's parameterized constructor in the subclass constructor.
The access modifier protected can modify properties and methods. After modification, this class, subclasses, and the same package can access them.
Access modifier | This class | Same package | Subclass | Others |
---|---|---|---|---|
private | √ | |
||
√ | √ | |
||
√ | √ | √ | ||
√ | √ | √ | √ |
The above is the detailed content of What is inheritance in Java? How to use inheritance in Java?. For more information, please follow other related articles on the PHP Chinese website!