[Easy-to-understand explanation! ] Reference type type conversion in Java

1. Prior knowledge

-[Latest] How to build Java environment on Ubuntu

-[Even beginners can do it! ] How to create a Java environment on Windows 10 (JDK14.0.1)

-[Easy-to-understand explanation! ] How to use Java instance

-[Even beginners can do it! ] How to install Eclipse on Windows 10 (Java environment construction)

As prior knowledge, the contents of the above link are required.

2. What is reference type conversion?

--There are upcast and downcast in reference type conversion. --Upcast treats an instance of a subclass as a superclass type object. --Downcast treats an instance of a superclass as a subclass type object. --Downcast is performed using the cast operator" () ".

3. Basic writing

Upcast


public class main class name{
    public static void main(String[] args) {
        //Instance generation
Subclass name Variable name 1=new subclass name();
        //Upcast
Superclass name variable name 2=Variable name 1;
    }
}

Downcast


public class main class name{
    public static void main(String[] args) {
        //Instance generation
Superclass name variable name 1=new subclass name();
        //Upcast
Subclass name Variable name 2= (Subclass name)Variable name 1;
    }
}

--Basic reference type conversion is described as above.

4. Preparation

01.png

  1. Start Eclipse and select [File (F)]-> [New (N)]-> [Java Project]. 02.png
  2. Enter Test1 for the project name and click the Done button. 03.png
  3. Select [File (F)] → [New (N)] → [Class]. 05.png
  4. Enter Test1 for the package and name and click the Done button. 06.png
  5. Confirm that Test1.java has been created. 001.png Enter Test1 in the package and Hello in the name in the same procedure as in 6.3, and click the Finish button. 002.png Enter Test1 in the package, GoodMorning in the name, and Hello in the superclass in the same way as in 8.3, and click the Done button. 003.png Enter Test1 in the package, GoodEvening in the name, and GoodMorning in the superclass in the same way as in 9.3, and click the Finish button. 004.png
  6. Success if Test1.java, Hello.java, GoodMorning.java, GoodEvening.java are created.

5. Description example

--Reference type conversion is used to method call and method definition range that a superclass or subclass has.

Test1.java


package Test1;
public class Test1 {
    public static void main(String[] args) {
        //Instance generation
        GoodEvening ge1 = new GoodEvening("A");
        ge1.showGoodMorning();

        //Upcast
        Hello hello = ge1;
        hello.showHello();

        //Downcast
        GoodEvening ge2 = (GoodEvening) hello;
        ge2.showGoodEvening();
        //((GoodEvening) hello).showGoodEvening();
    }
}

Hello.java


package Test1;
public class Hello{
	//Variable definition
	String name;

	//constructor
	public Hello(String name) {
		this.name = name;
	}

	//Display greetings
	void showHello() {
		System.out.println(name + "Hey,.");
	}
}

GoodMorning.java


package Test1;
public class GoodMorning extends Hello {
	//constructor
	public GoodMorning(String name) {
		super(name);
	}

	//Display greetings
	void showGoodMorning() {
		System.out.println(name + "Good morning, Mr.");
	}
}

GoodEvening.java


package Test1;
public class GoodEvening extends GoodMorning {
	//constructor
	public GoodEvening(String name) {
		super(name);
	}

	//Display greetings
	void showGoodEvening() {
		System.out.println(name + "Good evening, Mr.");
	}
}

Copy the above sentence, specify S-JIS as the character code, save the file name as Test1.java, Hello.java, GoodMorning.java, GoodEvening.java, and save it. When executed, it will be like this. ↓ ↓ 005.png

important point

--Since the actual state of after upcast is subclass reduction, subclass has priority if the same method exists in the superclass.

Test1.java


package Test1;
public class Test1 {
    public static void main(String[] args) {
        //Instance generation
        TestB b = new TestB();
        b.view();//B is displayed

        //Upcast
        TestA a = (TestA) b;
        a.view();//B is displayed
    }
}
class TestA {
    public void view() {
        //Superclass display
        System.out.println("A");
    }
}
class TestB extends TestA{
    public void view() {
        //Subclass display
        System.out.println("B");
    }
}

Copy the above sentence, specify S-JIS as the character code, save the file name as Test1.java, and execute it. ↓ ↓ 006.png

6. Related

-[Useful to remember !!!] Easy creation of constructor and getter / setter in Eclipse -[Useful to remember !!!] Easy creation of inherited class in Eclipse -[Even beginners can do it! ] How to write Javadoc -[Easy-to-understand explanation! ] How to use Java encapsulation -[Easy-to-understand explanation! ] How to use Java overload

Recommended Posts

[Easy-to-understand explanation! ] Reference type type conversion in Java
Type determination in Java
[Java] Date type conversion
[Java] List type / Array type conversion
Try functional type in Java! ①
[Java] Precautions for type conversion
[Java] Type conversion speed comparison
Java Primer Series (Type Conversion)
About returning a reference in a Java Getter
Java date data type conversion (Date, Calendar, String)
Regarding String type equivalence comparison in Java
Java reference to understand in the figure
[Java ~ Variable definition, type conversion ~] Study memo
Java study # 3 (type conversion and instruction execution)
How to do base conversion in Java
[Easy-to-understand explanation! ] How to use Java instance
[Easy-to-understand explanation! ] How to use Java polymorphism
[Basic knowledge of Java] About type conversion
Immutable (immutable) List object conversion function in Java8
[Easy-to-understand explanation! ] How to use ArrayList [Java]
Java 8 LocalDateTime type conversion stuff (String, java.util.Date)
[Java] Calculation mechanism, operators and type conversion
Type conversion from java BigDecimal type to String type
[Easy-to-understand explanation! ] How to use Java overload
[Easy-to-understand explanation! ] How to use Java encapsulation
[Java] Difference between equals and == in a character string that is a reference type
[Introduction to Java] About type conversion (cast, promotion)
[JAVA] Stream type
Partization in Java
The intersection type introduced in Java 10 is amazing (?)
[Java] Enumeration type
Java Optional type
Changes in Java 11
Java8 method reference
About var used in Java (Local Variable Type)
Rock-paper-scissors in Java
Java double type
[Easy-to-understand explanation! ] How to use Java inheritance [Override explanation]
Organized memo in the head (Java --Data type)
Java type conversion (String, int, Date, Calendar, etc.)
Java-automatic type conversion
java8 method reference
Pi in Java
JAVA reference materials
My Java reference
FizzBuzz in Java
Java review ③ (Basic usage of arrays / reference type)
[Java] Precautions when referencing a reference type in a parent class and instantiating it in a child class
I wrote about Java downcast in an easy-to-understand manner
About Java basic data types and reference type memory
[Java] Things to note about type inference extended in Java 10
About full-width ⇔ half-width conversion of character strings in Java
Interpreter implementation in Java
Make Blackjack in Java
Rock-paper-scissors app in Java
Constraint programming in Java
Put java8 in centos7
[Java] Full-width ⇔ half-width conversion
NVL-ish guy in Java
Combine arrays in Java
"Hello World" in Java