I'm not sure why the Java upcast will do the type conversion automatically.
I also don't know if the downcast has to be explicitly cast.
nothing special
Applicable source codeclass A {
void dispA() {
System.out.println("A");
}
}
class B extends A {
void dispB() {
System.out.println("B");
}
}
class Test {
public static void main(String[] args) {
B b = new B();
A a = b;
a.dispA();
}
}
What I tried
nothing special
Supplementary information (FW/tool version, etc.)Tool used: eclipse4_8.0
In type conversion of basic data (primitive) type, when substituting a small type for a large type, type conversion is automatically performed,
I learned that when assigning a large type to a small type, you have to cast explicitly.
In the above source code, it looks like subclass .b (large type) is assigned to superclass .a (small type).
On the contrary, I don't understand the reason why the type conversion must be done explicitly when subclass .a (large type) is assigned to superclass .a (small type).
Is it handled differently than when assigning basic data (primitive) types?
It didn't come out even if I searched on the Internet because there was no one stumbled here.
Could you teach me
-
Answer # 1
-
Answer # 2
It looks like you're assigning subclass .b (large type) to superclass .a (small type)
House,What can be putFrom that point of view, the super class is the "bigger type".
Related articles
- java - i can't understand the transition relationship of html files in spring boot
- java: about addition algorithms without carry
- java - can't understand the test items written in the unit test specifications
- i want to automatically display the text file created by the java program on the screen
- java - i do not understand the repetition of for please tell me the role of ""
- java - i want to display katakana characters read from a file without duplication
- java - i can't really understand object orientation
- java - i don't understand the problem of double for statement with array
- java - i can't automatically number ids with springboot+doma2+mysql (docker build)
- java - compile error i can't understand the meaning of illigal start of expression
- css in html,css:form tag is no longer reflected if you can understand java a little, please give me instructions
- i want to create a game in java that can be created without programming knowledge
- i don't really understand the java environment
- java - i want to get the date and time continuously by calculation without using sleep or timer
- java - how to increase the number of elements without using arraylist?
- i don't understand the cause of the java nosuchelementexception error
- java - i can't debug class files without using the ide
- java - i want to perform search evaluation of board games without using recursive expressions
- java - i don't understand the method call inside the class type method
- python - you may need to restart the kernel to use updated packages error
- dart - flutter: the instance member'stars' can't be accessed in an initializer error
- php - coincheck api authentication doesn't work
- php - i would like to introduce the coincheck api so that i can make payments with bitcoin on my ec site
- [php] i want to get account information using coincheck api
- the emulator process for avd pixel_2_api_29 was killed occurred when the android studio emulator was started, so i would like to
- javascript - how to check if an element exists in puppeteer
- sh - 'apt-get' is not recognized as an internal or external command, operable program or batch file
- i want to check the type of a shell script variable
- i want to call a child component method from a parent in vuejs
Because subclass is created by adding fields and methods to superclass,
It is a class that has more information and can do compared to the super class.
Turn it over,Only subclass types that have stricter restrictions than superclass can be put,It means that. In this sense, subclasses are "smaller" types than superclasses.