What we're going to talk about today is the concept of inheritance for classes in Java.
When it comes to inheritance, I believe we are not strangers. In life, children inherit their parents ' property, which is inheritance. In fact, the same is true of inheritance in Java. For a class, its data members and methods are its property, and the other class receives the property of the class, which is the inheritance in Java.
Next, I will talk about inheritance in a few aspects of the relevant knowledge.
First, the basic form and meaning of inheritance
In the above, we understand what is called inheritance. So, how do you use inheritance?
The key word for inheritance is extends. The general format of inheritance is: public class class Name extends class name {};
Assuming there are two classes, a, B, when declaring Class B, we require Class B to inherit the data members and methods of Class A.
The specific implementation is as follows:
Inheritance has two major meanings. One is to improve the reusability of code, and the other is to improve the extension of the program. Inheritance can make our code simpler and more flexible.
Second, when to use inheritance
As we mentioned above, inheritance can improve the reusability of code and improve the extensibility of the program. Well, we can't help but ask, when can we use to inherit?
My understanding is that when there are two classes that contain relationships, we can use inheritance to simplify the program.
For example, a car is a class, and a bus is a class. There are two classes that contain a relationship. Well, when it comes to declaring a bus class, for some of the car's common data members and methods, we can let it inherit the class of cars to get these properties and methods. For some bus-specific properties and methods, we can be added in the bus class. For example, define a car class that has three attribute values, length, width, and weight. Instead of defining a bus class, we need to add the previous Takepeople method.
The specific implementation is as follows:
Iii. contents of the Inheritance
One of the things you might notice is that the code I wrote above, whether it's a data member or a method, is public for the access modifier I gave it. We know that in Java there are four types of access modifiers, so can subclasses inherit all the properties and methods of the parent class? In fact, subclasses can receive all properties and methods of the parent class, that is, the subclass can receive from the parent class, regardless of whether the access modifier is public or private. However, for private domains, subclasses are not able to access them. The definition of inheritance in Java is to obtain accessible properties and methods, so we say that the private amount is not inherited.
Iv. Method Rewriting
What do you mean by method rewriting? The override of a method is the process of overriding a method of its inherited parent class in a subclass. The premise of rewriting is that there must be at least two classes that have inheritance relationships first. Second, in the process of overriding a method, the access modifier for the method must be greater than or equal to the access modifier of the method in the parent class. Overrides cannot change the method name of the method, the return value type, the parameter, it can only change the contents of the method body. Note that a method override cannot be the same as the original method, otherwise it cannot be called an override.
For example, affirm a student class, a college student class, let the student class inherit the student class, rewrite the learning method, the specific code is as follows:
This is the specific example of a method rewrite.
V Super keyword
Next, let's get to know the Super keyword.
When it comes to super, we can get in touch with the This keyword discussed earlier. As for this, we can use this method (data member) to invoke the properties and methods of the current class, or to invoke the constructor of the current class in such a way as this (parameter type argument name). This is a pointer-like reference to the current class, and super is similar to this, which points to the parent class of the current class. We notice that when your parent class has a constructor that doesn't have an empty argument, it's often the case that your child class will get an error. At this point, we need to use super to invoke the constructor of the parent class. To continue the above-mentioned students and college students as an example, we add a method of construction with parameters in the student class. The specific code is as follows:
Vi. automatic transformation and forced transformation
With inheritance and method rewriting, let's take a look at automatic transformation and forced transformation.
Before that, let's recall the instantiation of a class. The format of the instantiated class is: Class Name Object name =new constructor method name (parameter type parameter ...) )。
There are two types of automatic transformations, the first: the parent class name object name =new Subclass constructor Method (parameter type argument name ...) )。
Second type: public void method (parent class name parameter name) {
}
Subclass Name Object Name a = new Subclass construction Method (parameter value,...);
Parent class Name Object Name B = New Subclass construction Method (parameter value,...);
Method (object name a);
Method (object name B);
We know that after an automatic transformation, we will not be able to invoke the methods and data members added by the subclass, then we can not help asking whether the method that the transformed object calls, is the method of the parent class or the subclass after the overridden method? We might as well do a test with the following code:
Let's take a look at the results of the operation:
Yes, the results show that we call the study method after the subclass rewrite, why?
We have previously discussed the process of instantiating classes. Class Name Object name, create a piece of memory in the stack, and then record the object name. New constructor Method name (parameter type argument name ...) ), which is a data member and method that creates a piece of memory in the heap and stores the class under which the constructor is constructed. =, a connection is established between two blocks of memory, and the location of the heap where the data resides is added to the object name in the stack. In the process of automatic transformation, because we call the constructor of the subclass, we get the study method of the subclass. In fact, the new properties and methods in the subclass are also present in memory, and because the object is a parent object, the new properties and methods are hidden and cannot be called.
Recognizing the automatic transformation, let's take a look at forced transformation. In the process of flipping through other people's blogs, I see an example that I like very much, so I will also use this example to explain to you the forced transformation. In my opinion, inheritance generally exists between two and more classes that have a containing relationship. Animals and dogs, for example. Dogs are the children of animals. What is automatic transformation, automatic transformation is to affirm an animal reference to a dog. In short, it's about transforming a dog into an animal. Obviously, dogs are animals, so in the process of transformation, there will be no problem. However, forced transformation, it is easy to have some problems. Animals are transformed into dogs, in other words, animals are dogs. This is obviously not true. So unless you decide that the animal is a dog, you can use a forced transformation, otherwise the computer will error. For example, first you define a dog, and then you transform it into an animal by automatic transformation, and then you re-convert it back to the dog by forcing it, and that's a forced transformation that's allowed. In auto-transformation, the properties and methods added by subclasses are hidden, and the hidden content is restored after a forced transformation. In addition to this, we can also determine whether the animal is not a dog after the decision to make a forced transformation of the method. Here's a new keyword, instanceof. We can use it to determine whether the animal is a dog or not. Use such as: Animal instanceof Dog.
Seven, interface
Finally, I'm going to talk about some of the knowledge about interfaces. We know that Java differs from C++,java in that only one parent is allowed to inherit. Obviously, in some cases, only inheriting a parent class is not able to meet our needs. So, Java introduces another concept, an interface. Inherit a class, which we call inheritance. For interfaces, we implement an interface using implementations. We know that there is an abstract class inside the class, and in my understanding, the interface is something more abstract than an abstract class. Define the format of an interface:
For a property inside an interface, it must be a static data, and the method defined in the interface is necessarily an abstract method. This is the definition of the interface.
The keyword that implements the interface is implements. It is important to note that when you implement an interface and your class is not an abstract class, you need to rewrite all the abstract methods within the interface. Good understanding, because you inherit the abstract method, and you are not an abstract class, will naturally error.
On the inheritance of class knowledge, I discuss so much for the time being, I look forward to everyone's correction, welcome to discuss together.
Inheritance of classes in Java programming