-[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.
--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" () ".
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.
[File (F)]-> [New (N)]-> [Java Project]
.
Test1
for the project name and click the Done
button.
[File (F)] → [New (N)] → [Class]
.
Test1
for the package and name and click the Done
button.
Test1.java
has been created.
Enter Test1
in the package and Hello
in the name in the same procedure as in 6.3, and click the Finish
button.
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.
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.
Test1.java
, Hello.java
, GoodMorning.java
, GoodEvening.java
are created.--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. ↓ ↓
--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. ↓ ↓
-[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