Presentation is loading. Please wait.

Presentation is loading. Please wait.

Consider the following Which of the following will cause a java.lang.ClassCastException? a)Alpha a = x; b)Foo f= (Delta)x; c)Foo f= (Alpha)x; d)Beta b.

Similar presentations


Presentation on theme: "Consider the following Which of the following will cause a java.lang.ClassCastException? a)Alpha a = x; b)Foo f= (Delta)x; c)Foo f= (Alpha)x; d)Beta b."— Presentation transcript:

1 Consider the following Which of the following will cause a java.lang.ClassCastException? a)Alpha a = x; b)Foo f= (Delta)x; c)Foo f= (Alpha)x; d)Beta b = (Beta)(Alpha)x; 1 interface Foo {} class Alpha implements Foo { } class Beta extends Alpha {} class Delta extends Beta { public static void main(String[] args) { Beta x = new Beta(); // insert code here } } B

2 Consider the following What will happen …? p6 = (CollegeStudent) p3; p7 = (CollegeStudent) p2; 2 class Person { … } class Student extends Person { … } class CollegeStudent extends Student { … } Person p1 = new Person("Michael"); Student p2 = new Student("Dick"); Student p3 = new CollegeStudent("Peter"); CollegeStudent p6, p7; No error (compile ok, run time ok) Run time error if (p2 instanceof CollegeStudent) p7 = (CollegeStudent) p2;

3 Integer Vs int (Wrapper class) Which of the following create an Integer object correctly? Integer intObj1 = new Integer(23); Integer intObj1 = 23; How to convert an Integer object to an int primitive type? 3 autoboxing int x = intObj1.intValue();


Download ppt "Consider the following Which of the following will cause a java.lang.ClassCastException? a)Alpha a = x; b)Foo f= (Delta)x; c)Foo f= (Alpha)x; d)Beta b."

Similar presentations


Ads by Google