Today’s sharing started, please give us more advice ~

Inheritance and polymorphism are not only the relationship between classes, but also a way of thinking about problems.

inheritance

Java has classes, you can have a Person class for a Person, you can have a Teacher class for a Teacher, you can have a childen class for a child, are there any relationships between these classes?

The answer is yes, for example, as a Teacher, he belongs to both Teacher class and Person class. The dog belongs to the animal family. So we find that for each object, it can be classified into many categories.

We can also think about the scientific community for the division of animals and plants can be from the big to the small group of species in order to layer by layer division, therefore, as object-oriented programming, we can also understand a kind of such as the above division of animals and plants the same inheritance of the idea.

Without further ado, let’s use code to demonstrate, see the following code

Please look at the picture above, I wrote animal, cat and bird. We find that no matter animal, cat or bird, they all have their own names and eat food, but only Bird can fly. We found that the code for names and eating is the same in all three classes, so does this create code redundancy?

Another problem to consider is that whatever animal it is, as long as it is an animal, it must eat and it must have a name, but it does not have to swim or fly. So could we make animal a superclass, cat and bird subclasses, eat and name code inside animal, and then make cat and Bird inherit from animal?

The answer is yes, see the code below

After simplifying the code, we can see that all the code about names and eating is in animal. Remember that Java uses extends to clarify the relationship between children and parents

For example, if A extends B, then A is A subclass of B, and B is A parent class of A, and their relationship is is-A. For example, Teacher extends Person means that A Teacher is A (is A) Person. For example, dog extends animal means that a dog is an (is a) animal. This is inheritance in Java.

(Note: Parent class, also known as superclass, base class. Subclass, also called derived class)

Now, a couple of properties of inheritance

1. We find that in the inheritance relationship, the attributes contained in the parent class are only part of the subclass, and the subclass extends and extends the functions of the parent class.

2. Java supports only single inheritance, that is, a subclass can have only one parent class. (C++/Python supports multiple inheritance)

3. For private fields and methods in the parent class, the subclass inherits them, but cannot access them.

4. Instances of subclasses also contain instances of the parent class. Use the super keyword to get references to instances of the parent class.

polymorphism

If a male Teacher can inherit the attributes of Teacher and husband, when do we need a male Teacher to show the attributes of Teacher and when do we need a male Teacher to show the attributes of husband?

The answer is that if the male teacher is in the school, he has to act as a teacher, and if the male teacher comes home from work, he has to act as a husband. That’s polymorphism. To be clear, polymorphism is implemented based on inheritance.

In order to implement polymorphism from above, we must first understand upward transition, dynamic binding

upcasting

Upward transformation, as the name suggests, is upward transformation. The operation is to receive a subclass object with a superclass reference.

Examples are as follows

This is an upward transition, as shown in the code above. An upward transition can occur in direct assignment (as shown above), method passing, and method returning. The following code demonstrates two cases of method passing and method returning.

Dynamic binding

Dynamic binding is an effect of the upward transition. The following code

I write A return type and method name in subclass B, pass in the eat method whose parameters are exactly the same as those in the parent class A, and then access the eat method of the variable B1 that is upconverted to type A. Theoretically, I have upconverted B1 to type A, so I should also call the eat method in A. But the end result of the program is to call subclass B’s eat method, which is called dynamic binding.

If the subclass has a method with exactly the same return type, method name, and passed arguments, then the subclass’s method will be called

Method override vs. method override

Methods to rewrite

Above we have seen that a subclass has the same method return type, method name, and passed arguments as the parent class. This behavior is called method overwriting.

There are a few things to note about method rewriting:

1. Overrides occur in child and parent classes, and only method overloads are supported within the same class. Overrides need to ensure that the method name, the incoming arguments are exactly the same, and the return types are compatible.

2. Return types must be compatible. The type returned by a subclass must inherit from or be identical to the type returned by the parent class.

In Java, you can treat a subclass as the same class as its parent, but not the other way around. You can’t treat a parent class as a subclass. To understand this, you can understand that many places in Java support subclass-to-parent operations.

3. Private, static, and final parent methods cannot be overridden.

4. A method of a subclass cannot have lower access rights than a method of its parent class.

5. We can use annotations to check for overwriting. Details are shown in the following code

Note that annotations are not annotations and will report errors if your overrides have errors.

Method overloading

The rule for method overloading is that the method name should be the same, the parameters passed in should be different, and the return type should not be required.

Method overloading can occur either in the same class or in a child or parent class.

The difference between overloading and overwriting we can see literally, overloading means that the old method is still there, overwriting means that the old method is overwritten. That’s why when we do dynamic binding we call the upcast parent class reference, but we call the subclass method, because the subclass has overridden the parent class method, and when my object is actually a subclass, it will only call the method overridden by the subclass.

Examples of code are given below

Know the polymorphic

Armed with this knowledge, we can use the idea of polymorphism to design some programs.

Specific point is we can write some focus on the parent class code, but it can be made by different subclasses of incoming (that is the upward transition), and then call the corresponding method, will call to all subclasses of rewriting method dynamic binding (which is), so you can form a set of code, the effect of a variety of forms, this is polymorphic

Here is a concrete simple example to help understand

If you can understand the above code, you must also have some understanding of polymorphism in Java.

An abstract class

To talk about abstract classes, let’s go back to our previous blog post about polymorphism inheritance and polymorphism

We found that in implementing such a polymorphic code, the parent class Worker like didn’t do anything, the main printing work is done by each work function in a subclass, as a result, we can to a more detailed description of the parent class, the parent class that no actual effect of the work function declarations as abstract function, Classes that contain abstract functions are called abstract classes.

Note: both abstract classes and abstract methods are declared with abstract. A class that contains abstract methods must be abstract, but an abstract class may not contain abstract methods.

Examples of code are shown below

Two points to note:

1. Abstract classes cannot be instantiated.

2. Abstract methods have no method body

3. If a class inherits from an abstract class (and if that abstract class also inherits from another abstract class), then that class implements all the abstract methods; otherwise, that class is declared as an abstract class

The main purpose of abstract classes is to be inherited, because abstract classes cannot be instantiated, and if we accidentally instantiate them later in the code, the compiler will report an error, which will serve as a check. So we want to make our code more secure by giving each class restrictions that best meet their needs.

interface

Interface, you can think of it as 100% abstract class, because we can contain ordinary methods in the abstract class, but all the methods in the interface default to abstract methods, all the fields in the interface default to public static final type. The interface keyword is interface, and subclasses implement (inherit) the interface using the keyword implements

A specific code example is shown below

So what are the benefits of implementing interfaces?

The benefit is that the programmer doesn’t care what type the class is, but rather whether the class implements the interface I want, and if it does, I can use it, regardless of what class it is.

Also note that interfaces can inherit from interfaces. A class can implement multiple interfaces, but can only inherit from one parent class.

summary

So much for abstract classes and interfaces, remember that abstract classes and interfaces and all that stuff are designed to serve a flexible way of designing polymorphisms. So when we design some code using polymorphism, we can try to use abstract classes or interfaces.

If you are familiar with the idea of inheritance and polymorphism in Java and the implementation of code, then this article is done.

Today’s share has ended, please forgive and give advice!