- class SampleExec1 {
- void run() {
- System.out.println("SampleExec1 run");
- }
- }
- class SampleExec2 extends SampleExec1 {
- void run() {
- System.out.println("SampleExec2 run");
- }
- }
- public class SampleExecution {
- public static void main(String[] args) {
- SampleExec2 ex = new SampleExec1();
- ex.run();
- }
- }
Output: compile time error at line 13. Type mismatch: cannot convert from SampleExec1 to SampleExec2
--------------------------------------------------------------------
--------------------------------------------------------------------
- class SampleExec1 {
- void run() {
- System.out.println("SampleExec1 run");
- }
- }
- class SampleExec2 extends SampleExec1 {
- void run() {
- System.out.println("SampleExec2 run");
- }
- }
- public class SampleExecution {
- public static void main(String[] args) {
- SampleExec2 ex = (SampleExec2 )new SampleExec1();
- ex.run();
- }
- }
Output:
Exception in thread "main" java.lang.ClassCastException: SampleExec1 cannot be cast to SampleExec2 at SampleExecution.main(SampleExecution .java:14)
--------------------------------------------------------------------
- class SampleExec1 {
- void run() {
- System.out.println("SampleExec1 run");
- }
- }
- class SampleExec2 extends SampleExec1 {
- void run() {
- System.out.println("SampleExec2 run");
- }
- }
- public class SampleExecution {
- public static void main(String[] args) {
- SampleExec1 ex = new SampleExec2();
- ex.run();
- }
- }
Output: SampleExec2 run
No comments:
Post a Comment