Upcast and super type / subtype

When reading the Java source code, you may see the following description.

Superclass variable=new subclass();

It is written as. I wanted to know the term for this description. *** The advantages of making this description will not be covered in this article **.

Upcast (super class)

I often see it, so when I muttered on Twitter that this notation doesn't have a "cast" name, a kind person told me that there is a ** upcast ** (thanks). ). If you know the term, you can search it or ask others.

//Upcast
Superclass variable=new subclass();

Reference: [Introduction to Java] Cast (type conversion) and upcast, downcast Since the type of the variable to be declared and the type of the instance to be generated are ** classes that have a parent-child relationship, **, this description itself does not seem so strange.

Upcast (interface)

However, the Java upcast also has the following description.

Interface type variable=new implementation class();

It is written as. ** The type of the declared variable is an interface, but the type of the instance to be assigned is a class **. Even though it is an implemented class, I feel a lot of discomfort because I recognize that the interface and the class are different types. It feels like a normal feeling to feel uncomfortable here, but it doesn't seem to be the case. If you continue to feel uncomfortable, your understanding will be slow. The only solution I can think of is to reduce the discomfort by repeating the implementation and familiarizing it with my hands. However, if you still know the name ** upcast ** and its description method and behavior, and know that it is also used in the interface type, the object itself that feels strange will be clear, so it will be easier to overcome. think.

Super type / subtype

In addition, [Head First Design Pattern-Basics of Design Patterns Learned by Head and Body](https://www.amazon.co.jp/Head-First Design Patterns-Basics of Design Patterns Learned by Head and Body-Eric- If you read Freeman / dp / 4873112494), there is a description of ** supertype / subtype **.

Superclass variable=new subclass();
Interface type variable=new implementation class();

In the above case, the "super class type" and "interface type" on the left correspond to ** super type (type) **. On the other hand, "new subclass ()" and "new implementation class ()" in the right section correspond to ** instances of subtype (type) **. I didn't feel uncomfortable with this name because I personally recognize that both the class and the interface are types. However, most of the search by super type / subtype is about generics, and Head First design pattern-the basics of design patterns that can be learned by the head and body Head-First Design Patterns --- Basics of Design Patterns Learned by Head and Body-Eric-Freeman / dp / 4873112494) may not be common.

Recommended Posts

Upcast and super type / subtype
Upcast, Dowcast and Constructor
Basic data type and reference type
Difference between primitive type and reference type