[Java] Class inheritance

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".

What is class 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".

Inheritance

Use extends for class inheritance.

class class name extends superclass name{
Instance variables / methods
}

Inheritance and constructor

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.

Redefinition: Override

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

[Java] Class inheritance
[Java] Inheritance
Java inheritance
Java inheritance
java (inheritance)
[Java] What is class inheritance?
Java class methods
java Scanner class
Java HashMap class
About class inheritance.
java (abstract class)
[Java] Nested class
Java anonymous class
About Java class
About java inheritance
[java] abstract class
[Java] Object class
Java local class
[Java] Inheritance and structure of HttpServlet class
[Java] Overload / Override / Inheritance
About class division (Java)
About Java StringBuilder class
[Java] About Singleton Class
Summarize Java inheritance (Java Silver 8)
Java inner class review
Java class type field
About inheritance (Java Silver)
Java programming (class method)
About Java String class
Java programming (class structure)
[Java] Implicit inheritance memo
Java learning memo (inheritance)
About java abstract class
Java Basic Learning Content 6 (Inheritance / Abstract Class / Interface)
Java and Swift comparison (3) Class implementation / Class inheritance / Class design
[Java] Integer wrapper class reference
Advanced inheritance abstract, interface -java
Java memo (standard class) substring
JAVA learning history interface inheritance
Java tips --StaticUtility class modifier
Inheritance
Write class inheritance in Ruby
Java inflexible String class substring
Java memo (standard class) length
Inheritance
[Implementation] Java Process class notes
About Java class loader types
How to use java class
[Java] Comparator of Collection class
Java class definition and instantiation
Java
Java learning memo (abstract class)
Java starting from beginner, inheritance
Summary of Java Math class
java (inheritance of is-a principle)
About Java class variables class methods
Java
[Java basics] What is Class?
Muscle Java Object Oriented Day 2 ~ Inheritance ~
Why are class variables needed? [Java]
[Java] Object-oriented syntax --class method / argument