35 lines
698 B
Java
Raw Normal View History

2015-04-20 15:36:01 -07:00
//: polymorphism/RTTI.java
2015-05-29 14:18:51 -07:00
// <20>2015 MindView LLC: see Copyright.txt
2015-04-20 15:36:01 -07:00
// Downcasting & Runtime type information (RTTI).
// {ThrowsException}
class Useful {
public void f() {}
public void g() {}
}
class MoreUseful extends Useful {
2015-05-05 11:20:13 -07:00
@Override
2015-04-20 15:36:01 -07:00
public void f() {}
2015-05-05 11:20:13 -07:00
@Override
2015-04-20 15:36:01 -07:00
public void g() {}
public void u() {}
public void v() {}
public void w() {}
2015-05-18 23:05:20 -07:00
}
2015-04-20 15:36:01 -07:00
public class RTTI {
public static void main(String[] args) {
Useful[] x = {
new Useful(),
new MoreUseful()
};
x[0].f();
x[1].g();
// Compile time: method not found in Useful:
//! x[1].u();
((MoreUseful)x[1]).u(); // Downcast/RTTI
((MoreUseful)x[0]).u(); // Exception thrown
}
} ///:~