Code: Select all
A a = (B)new A(); // where B extends from A
Given that is the case, the following also holds true:
Code: Select all
A a = (B)new B(); // no issues at compile time or runtime
Is that accurate?
Regards,
Seán.
Moderator: admin
Code: Select all
A a = (B)new A(); // where B extends from A
Code: Select all
A a = (B)new B(); // no issues at compile time or runtime
Code: Select all
interface I{}
class A implements I{}
class B extends A {}
class C extends B{}
public class TestClass {
public static void main(String[] args){
A a = new A();
B b = new B();
a=b;
//b=(B)(I)a;
b=a; // if "a" is a "b" now, why this line of code is wrong?
}
}
Correct.
Code: Select all
a = (B)(I)b;
a = (B)b;
a = b;
Code: Select all
public class Test {
public static void main(String[] args) {
I i = null;
Integer s = (Integer) i;
}
}
interface I{}
Users browsing this forum: marpiva and 34 guests