answer : C
Conversion of Reference Types
Downcasting
It’s the casting from a superclass to a subclass.
An explicit cast is required.
llegitimate narrowing results in a ClassCastException.
ac = (AnotherClass)sc; throw runtime ClassCastException
ac = sc; Compilation eror error: incompatible types:
SomeClass cannot be converted to AnotherClass C.
public class q58 {
public static void main(String[] args) {
AnotherClass ac = new AnotherClass();
SomeClass sc = new AnotherClass();
ac = sc;
// sc = ac;
sc.methodA();
ac.methodA();
}
}
class SomeClass {
public void methodA() {
System.out.println("Some Class #methodA ()");
}
}
class AnotherClass extends SomeClass {
public void methodA() {
System.out.println("AnotherClass#methodA() ");
}
}
c , child type cannot hold parent type reference , incompatible types .... but a parent type reference can hold up child type reference
upvoted 1 times
...
Log in to ExamTopics
Sign in:
Community vote distribution
A (35%)
C (25%)
B (20%)
Other
Most Voted
A voting comment increases the vote count for the chosen answer by one.
Upvoting a comment with a selected answer will also increase the vote count towards that answer by one.
So if you see a comment that you already agree with, you can upvote it instead of posting a new comment.
ASPushkin
7 months, 1 week agoaruni_mishra
8 months, 3 weeks agomrc61
11 months, 3 weeks agomendjijet
1 year, 1 month agod7bb0b2
1 year, 3 months agoOmnisumem
1 year, 6 months agotmuralimanohar
1 year, 8 months agoStavok
1 year, 9 months agoRP384
2 years, 1 month agoAnkit1010
2 years, 1 month agoTADIEWA
2 years, 2 months ago