When creating a new class, if you can use the existing class, you can create the class efficiently. This can be achieved by Java "inheritance".
For example, when making a new product by improving an off-the-shelf product, one method is to create a new blueprint from a completely empty state, but the blueprint of the new product is improved based on the off-the-shelf design drawing. When you create, you only have to design the changes and additions, so you can efficiently create the blueprint. The same is true for creating Java classes. You can create a new class by extending only the part to be extended or added when extending the function of an existing class or adding another function. This is called ** inheritance **, and the existing class for inheritance is called ** superclass ** or ** parent class **. On the other hand, new classes are called ** subclasses ** or ** child classes ** or ** derived classes **. You can create multiple child classes from one parent class. However, a child class cannot have more than one parent class. In Java, you can't mix two or more blueprints to create a new blueprint. This is sometimes called "single inheritance".
Use extends for class inheritance.
class class name extends superclass name{
Instance variables / methods
}
Inheritance does not inherit only the constructor. This is because the object to be initialized may have been added or removed in the subclass, or the type may have changed. Even if the constructor is the same in the superclass, it must be in the subclass. super() By creating a new constructor when inheriting, the constructor can be used again, but it is troublesome to have to write the same constructor in the subclass, and the advantage of inheritance is halved. I will end up. Inherit the common parts in the constructor, such as initializing the inherited part using the superclass constructor and then initializing the added part in the subclass, and only the different parts in the subclass constructor. It would be convenient if you could describe it. In Java this can be achieved using ** super **. super calls the constructor of the superclass for that class. Always use this super to call whatever the super class name is. Please note that even if you enter the name of the actual super class, it will not work as a constructor. Also, even if there is an argument, you can call the constructor of the super class corresponding to it by writing the argument in the parentheses of super (). Note that this super () must be written at the very beginning of the subclass's constructor in order to first execute the superclass's constructor and then perform subclass-specific processing. Also, even if you do not specify the call to the constructor of the super class by super (), if the class does not have a constructor with no arguments, the compiler will automatically refer to the constructor of the super class and create the constructor.
Inheritance inherits all the properties of the superclass, and in some cases inherits unnecessary methods. In such a case, let's redefine the method on the inheriting side. This redefinition is called ** override **. Be careful not to confuse it with "overlord", which has a similar name. The override replaces the already defined method with the one described in the subclass, so the old method is no longer used.
The method of overriding is no different from the definition of a normal method. However, to prevent accidental overriding of a super class method but defining another method, it is clearly stated that the method is overriding by writing @Override
. To do. This is called an override annotation and can prevent override failure.
Recommended Posts