Inheritance is to derive a new class from an existing class. The new class can absorb the data attributes and behaviors of the existing class, and can extend the new class. ability. Inheritance is often referred to as the is-a relationship. The subclass inherits the characteristics and behaviors of the parent class, so that the subclass has various attributes and methods of the parent class. Or the subclass inherits methods from the parent class so that the subclass has the same behavior as the parent class.
Example:
For example, you can first define a class called a car. The car has the following attributes: body size, color, steering wheel, tires, and two classes, cars and trucks, are derived from the car class. Add a small trunk for a sedan and a large cargo box for a truck.
Inheritance expresses an intersection relationship between object classes, which allows an object of one type to inherit the data members and member methods of another type of object. If class B inherits class A, then the objects belonging to B will have all or part of the properties (data attributes) and functions (operations) of class A. We call the inherited class A a base class, parent class or super class, and we call it a base class, parent class or super class. Inherited class B is a derived class or subclass of A.
Terms indicating parent class and subclass: parent class and subclass, superclass and subclass, base class and derived class, they mean the same thing.
Develop animal classes, where the animals are penguins and mice. The requirements are as follows:
Penguin: attributes (name, id), methods (eating, sleeping, self-introduction) )
Mouse: Attributes (name, id), methods (eating, sleeping, self-introduction)
Penguins and mice are both animals. Can we write an animal class? This will make the code much simpler. Some people say that I just want to create two separate classes to write this attribute. I can only tell you that you can, but it is like your father gave you millions, but you didn’t want it. You threw the money away and had to earn it yourself. , I can’t help it if you want to do this. Since Java provides us with inheritance, we must make good use of it. This will greatly improve our development efficiency. For example: the maintainability is improved, the code is more concise, and the reusability of the code is also improved (reusability means that it can be used multiple times Use, no need to write the same code multiple times).
1. Inheritance can reduce duplicate code. For example, methods already provided by the parent class can be used directly by the subclass without having to implement them.
2. Inheritance is the prerequisite for polymorphism. Of course, using inheritance also improves the coupling of classes.
When you don’t need the attributes of the parent class, you can override the original attributes.
Inheritance is divided into single inheritance and multiple inheritance. Single inheritance means that a subclass can have at most one parent class. Multiple inheritance means that a subclass can have more than two parent classes. Since multiple inheritance can bring ambiguity, single inheritance should be used as much as possible in practical applications. Classes in the Java language only support single inheritance, while interfaces support multiple inheritance. The function of multiple inheritance in Java is implemented indirectly through interface. Inheritance implementation
Inherited initialization sequence:
If there is a constructor method: execute the attribute first, and then execute the constructor method
If the name attribute is not assigned a value in the constructor method, then The value of name is the value assigned to the class attribute
extends keyword
/*动物类*/ 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 keyword
public interface A { public void eat(); public void sleep(); } public interface B { public void show(); } public class C implements A,B { }
super and this keyword
super keyword: We can use the super keyword to access the members of the parent class, which is used to reference the parent of the current object. kind. Note:
1. The super keyword can only be used in constructors or instance methods, but the super keyword cannot be used in static methods and static code blocks
2. If If the member variables and methods in the parent class are defined as private types, then the subclass can never access them. If you try to access the private type var variable of the parent class in the form of super.var, it will cause a compilation error
: A reference pointing to itself, indicating the object reference that is currently calling this method. Note:
1. When there are multiple overloaded constructors, and one constructor needs to call another to construct it, use this (param) form to call it in the first line, and only Can be in the first line;
2. When a method in the object needs to call other methods in the object, use this as the main call, or you don’t need to write it. In fact, the default is this as the main call;
3.当对象属性和方法中的局部变量名称相同时,在该方法中需要显式的使用this作为主调,以表示对象的属性,若不存在此问题,可以不显式的写this。
其实,其牵涉到的一个问题就是变量的查找规则:先局部变量 => 当前类中定义的变量 => 其父类中定义的可以被子类继承的变量 => 父类...
这块要完全理解需要看上面我写的“继承的初始化顺序”从这里可以看到程序是如何初始化的。
/** * 父类 * @author gacl * */ class FatherClass { public int value; public void f() { value=100; System.out.println("父类的value属性值="+value); } } /** * 子类ChildClass从父类FatherClass继承 */ class ChildClass extends FatherClass { /** * 子类除了继承父类所具有的valu属性外,自己又另外声明了一个value属性, * 也就是说,此时的子类拥有两个value属性。 */ public int value; /** * 在子类ChildClass里面重写了从父类继承下来的f()方法里面的实现,即重写了f()方法的方法体。 */ public void f() { super.f();//使用super作为父类对象的引用对象来调用父类对象里面的f()方法 value=200;//这个value是子类自己定义的那个valu,不是从父类继承下来的那个value System.out.println("子类的value属性值="+value); System.out.println(value);//打印出来的是子类自定义的那个value的值,这个值是200 /** * 打印出来的是父类里面的value值,由于子类在重写从父类继承下来的f()方法时, * 第一句话“super.f();”是让父类对象的引用对象调用父类对象的f()方法, * 即相当于是这个父类对象自己调用f()方法去改变自己的value属性的值,由0变了100。 * 所以这里打印出来的value值是100。 */ System.out.println(super.value); } } /** * 测试类 */ public class TestInherit { public static void main(String[] args) { ChildClass cc = new ChildClass(); cc.f(); } }
运行结果:
父类的value属性值=100
子类的value属性值=200
200
100
分析:
执行 ChlidClass cc = new ChlidClass();
首先在栈空间里面会产生一个变量cc,cc里面的值是什么这不好说,总而言之,通过这个值我们可以找到new出来的ChlidClass对象。由于子类ChlidClass是从父类FatherClass继承下来的,所以当我们new一个子类对象的时候,这个子类对象里面会包含有一个父类对象,而这个父类对象拥有他自身的属性value。这个value成员变量在FatherClass类里面声明的时候并没有对他进行初始化,所以系统默认给它初始化为0,成员变量(在类里面声明)在声明时可以不给它初始化,编译器会自动给这个成员变量初始化,但局部变量(在方法里面声明)在声明时一定要给它初始化,因为编译器不会自动给局部变量初始化,任何变量在使用之前必须对它进行初始化。
子类在继承父类value属性的同时,自己也单独定义了一个value属性,所以当我们new出一个子类对象的时候,这个对象会有两个value属性,一个是从父类继承下来的value,另一个是自己的value。在子类里定义的成员变量value在声明时也没有给它初始化,所以编译器默认给它初始化为0。即(父类的value为0,子类的value为0;)
执行第二句话: cc.f();
当new一个对象出来的时候,这个对象会产生一个this的引用,这个this引用指向对象自身。如果new出来的对象是一个子类对象的话,那么这个子类对象里面还会有一个super引用,这个super指向当前对象里面的父对象。所以相当于程序里面有一个this,this指向对象自己,还有一个super,super指向当前对象里面的父对象。
这里调用重写之后的f()方法,方法体内的第一句话:“super.f();”是让这个子类对象里面的父对象自己调用自己的f()方法去改变自己value属性的值,父对象通过指向他的引用super来调用自己的f()方法,所以执行完这一句以后,父对象里面的value的值变成了100。接着执行“value=200;”这里的vaule是子类对象自己声明的value,不是从父类继承下来的那个value。所以这句话执行完毕后,子类对象自己本身的value值变成了200。
The above is the detailed content of Detailed explanation of examples of inheritance in Java object-oriented. For more information, please follow other related articles on the PHP Chinese website!