There are two cases of type casting in Java.
First of all, the basic type does not require any additional work when putting values from a small data type to a large data type, but in the opposite case, typecasting must be done explicitly. Please refer to the code below.
class CodeRunner{
public static void main(String[] args){
int intNum = 1000;
//You do not need to specify type casting when you put the value of int, which is a small bowl, in a large bowl, long.
long longNum = intNum;
//If you do not explicitly type cast with int as below,
//posible lossy conversion from long to int error
intNum = (int)longNum;
}
}
The typecasting process does not change the memory of the original value, but the value is copied and stored in the new memory.
For more information, see Basic Type Conversion Lecture .
The type casting of the object is possible even if it is inherited. For example, you can type casting as shown in the code below.
class CodeRunner{
public static void main(String[] args){
//Human who inherited Animal can type casting with Animal
Human humanObject1 = new Human();
Animal animalObject1 = humanObject1;
// However, on the contrary, Human cannot be typecasted as Animal
// Because Human includes all the functions of Animal
// You can say, "Human is Animal"
// You can't say "Animal" is "Human"
// The code below has an error
Animal animalObject2 = new Animal();
Human humanObject2 = animalObject2;
}
}
class Animal{
int age;
}
class Human extends Animal{
String name;
}
The object's type casting does not copy its contents from memory. However, animalObject1 will only be able to use the function that corresponds to Animal among the functions of Human Instance.
For more information, see Class Transformation Lecture .
Assuming that I know the basics of type casting, I will explain what I know. It would be easier to understand if you look at the code rather than explaining the process of type casting in writing.
You can follow the main method first.
public class shape {
public void draw(){}
public static void doosomthing {
shape.draw();
/*
Subclass circle type parameters
There is no compilation error because it was upcast and received as a super class shape.
A subclass is equal to or larger than a superclass.
Subclasses are created by inheriting superclasses, so the things that exist in superclasses are basically in subclasses
It doesn't matter if you omit the type transformation, and there is no compilation error.
Determines whether a particular method in a subclass should be executed at the time (runtime) the method is executed (dynamic binding)
You will then run the method of the requested subclass.
*/
}
public static void main(String[] args) {
One circle = new();
triangle = new triangle();
DoSomthing(circle); // Execution Result : Draw a circle
DoSomthing (triangle); // Execution Result: Draw a triangle
/*
Pass parameters to the type of subclass circle, triangle.
The doSomething method upcasts the parameters received into a superclass shape.
*/
}
}
class one extensions shape {
@Override
public void draw() {
System.out.println ("draw a circle");
}
}
class triangle extensions shape {
@Override
public void draw() {
System.out.println ("draw a triangle");
}
}
The basic principle of object-oriented programming is a simple summary of object-oriented programming, and I hope it helps.
© 2024 OneMinuteCode. All rights reserved.