File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Casting

 
Dale DeMott
Ranch Hand
Posts: 515
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay.. so I need some good questions to test me on casting. Some questions that help me think. Oh.. if you can provide the answer too that would help so I can grade myself
-Dale
------------------
By failing to prepare, you are preparing to fail.
Benjamin Franklin (1706 - 1790)
 
Kaspar Dahlqvist
Ranch Hand
Posts: 128
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1) Does this compile?
class A {}
interface D {}
interface C extends D {}
public class T extends A implements C {
public static void main(String[] args) {
T t = new T();
D d = t;
A a = (A) d;
}
}
2) Does this compile? Does it run? What happens?
class A {}
interface D {}
interface C extends D {}
public class T extends A implements C {
static int i = 6;
public static void main(String[] args) {
T t = new T();
C c = t;
D d = c;
A a = (A) d;
System.out.println(((T) a).i);
}
}

If they're too easy, complain!
/Kaspar
 
Cameron Park
Ranch Hand
Posts: 371
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. Does not compile.
class A {}
interface D {}
interface C extends D {}
public class T extends A implements C {
public static void main(String[] args) {
T t = new T();
D d = t;//t is not a D!
A a = (A) d;
}
}
2. Does not compile.
class A {}
interface D {}
interface C extends D {}
public class T extends A implements C {
static int i = 6;
public static void main(String[] args) {
T t = new T();
C c = t;
D d = c;//Again, c is not a D
A a = (A) d;
System.out.println(((T) a).i);
}
}
Commenting out the erroroneous line, the output should be
6
since the object is of type T, and T's i equals to 6.
 
Anonymous
Ranch Hand
Posts: 18944
  • 0
  • send pies
  • Quote
  • Report post to moderator
Hello Cameron Park!
I guess you are some what wrong. Both compile and there are no errors.

The first one:
class A {}
interface D {}
interface C extends D {}
public class T extends A implements C {
public static void main(String[] args) {
T t = new T();
D d = t; // This is correct as long as class T implements
//interface D. which is true in this case.
A a = (A) d; // The casting is a must here
// This is always correct as A is a non-final class and D is an interface.
}
}
Second one:
class A {}
interface D {}
interface C extends D {}
public class T extends A implements C {
static int i = 6;
public static void main(String[] args) {
T t = new T();
C c = t;// ok,as long as class T impelments C.
D d = c;// ok,as C is a subinterface of D
A a = (A) d; // The casting is a must her also. see above.
System.out.println(((T) a).i); // here we are again casting the object back to type t.
//as class T extends A this is valid and cating is a must
}
}

Please do correct me Kaspar Dahlqvist or Cameron Park, if I am wrong.

[This message has been edited by chandrashekar munukutla (edited July 12, 2001).]
 
Cameron Park
Ranch Hand
Posts: 371
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are right. I missed the fact that C is also D. Therefore, when T implements C, T also implements D. Yes, both are compilable.
 
Cameron Park
Ranch Hand
Posts: 371
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are right. I missed the fact that C is also D. Therefore, when T implements C, T also implements D. Yes, both are compilable.
 
Kaspar Dahlqvist
Ranch Hand
Posts: 128
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, they both compile and the second one outputs 6 when run. Chandrashekar, you got it all right! I tried to make the second one a little harder in the 'System.out.println()' - call, but you got that, too!
Great going!
Do you want more of these?
/Kaspar
 
Dale DeMott
Ranch Hand
Posts: 515
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah..that would be great. I love the comments and knowing the pitfalls. Its nice to see this is action. The book I have doesn't go deep into the casting problems.
Some tougher casting questions would be great! These are terrific for practice!
Dale

------------------
By failing to prepare, you are preparing to fail.
Benjamin Franklin (1706 - 1790)
[This message has been edited by Dale DeMott (edited July 13, 2001).]
 
Dale DeMott
Ranch Hand
Posts: 515
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It seems to me that if you draw out a diagram, you can see who is extending or implementing from who. And the other key to remember is that an interface can cast to classes as long as they are a non-final class and implemented. Am I missing anything.
-Dale
------------------
By failing to prepare, you are preparing to fail.
Benjamin Franklin (1706 - 1790)
 
Kaspar Dahlqvist
Ranch Hand
Posts: 128
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmm... Yes...
Assume you have the following relationships:
class A {}
class B extends A implements G {}
class C implements E, F {}
class D extends A{}
interface E {}
interface F {}
interface G extends F {}

1) Would this compile? If it does, what is the output?
public class Q extends C {
public static void main(String[] a) {

F f = new Q();
C c = (C) f;
E e = c;
Q q = (Q) f;
c = (C) (e instanceof Q ? e : new Q());
System.out.println("And the class is: " + c.getClass());
}
}

2) Does this compile? If it does, what will be the output?
public class Q extends B {
public static void main(String[] args) {

A a = new B();
F f = (F) a;
a = new Q();
G g = (G) a;
g = (G) f;
Q q = (Q) g;
System.out.println("And the class is: " + q.getClass());
}
}

Good luck!!
/Kaspar
 
Terence Doyle
Ranch Hand
Posts: 328
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI,
Casting is not exactly my strong point but I'll be brave and treat this as a learning experience.
Both compile and the output is class Q and B respectively.
Or at least that is what I have been able to figure out using a diagram.
OK: shoot me down.... :-)
Terry

 
Kaspar Dahlqvist
Ranch Hand
Posts: 128
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'll be leaving the civilized world for two weeks or so now, but I won't give you the right answers!
Talk it out and test the code. You did not get everything right, I'm afraid..!
Have a great time!
/Kaspar
 
Dale DeMott
Ranch Hand
Posts: 515
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great questions! If anyone else wants to add some, that would be great!
------------------
By failing to prepare, you are preparing to fail.
Benjamin Franklin (1706 - 1790)
 
April.Johnson
Ranch Hand
Posts: 48
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. Compiles and prints Q as the class.
// F is interface; Q is class that implements F; ok
F f = new Q();
// C is class; f's actual object (Q-type) is subclass of C; ok
C c = (C) f;
// E is interface; c implements E; ok
E e = c;
// Q is class; f's actual object (Q-type) IS a Q; ok
Q q = (Q) f;
// e's acutal object is Q, so code that would be executed is
// c = (C)e
// c is class; e's actual object (Q-type) is subclass of C; ok
c = (C) (e instanceof Q ? e : new Q());
// c's actual object is of type Q
System.out.println("And the class is: " + c.getClass());

2. Compiles, but causes (runtime) ClassCastException
// A is class; B is class that extends A; this is ok
A a = new B();
System.out.println("The class is: " + a.getClass()); // B
// F is interface; a's actual object is class that implements F; ok
F f = (F) a;
System.out.println("The class is: " + f.getClass()); // B
// A is class; Q is class that extends A; ok
a = new Q();
System.out.println("The class is: " + a.getClass()); // Q
// G is interface; a's actual object is class that implements G; ok
G g = (G) a;
System.out.println("The class is: " + g.getClass()); // Q
// G is interface; f's actual object (B-type) is class that implements g; ok
g = (G) f;
System.out.println("The class is: " + g.getClass()); // B
// Q is class; g's actual object (B-type) is superclass of Q; Class cast exception
Q q = (Q) g;
System.out.println("And the class is: " + q.getClass());
[This message has been edited by April.Johnson (edited July 13, 2001).]
 
Anonymous
Ranch Hand
Posts: 18944
  • 0
  • send pies
  • Quote
  • Report post to moderator
good one April.Johnson!
Actually I guess the terminology you are using isn't that correct. Let me clear it up.
The following is from RHE. Wonderfully said.
suppose we say A a = new A(); for creating an Object of class A. what actually happens is The argument to 'new' determines the true 'class' of the object. Here 'a' is not an object. It is a variable that contains the reference( something similar to address) to the object of class A. References are stored in variables, and Variables have 'types' ('type' for single variable) that are specified by the programmer at Compile time.
So here, 'Object reference variable types' can be classes, interfaces, or arrays.
When an object is created, throughout that object's lifetime its 'class' remains same. While the Object's class is unchanging, it may be referenced by variables of many different types.
Reference conversion takes place at compile time as the compiler has all the information it needs to determine whether the conversion is legal. General rule of Thumb: Object reference conversion is permitted wehn the direction of the conversion is "up" the inheritance hierarchy. we can say converting to a superclass is permitted and converting to a subclass is not permitted.
For Casting the Compiler checks both at compile time and run time. The type of the reference variable is obvious at compile time. However, the class of an object referenced by such a variable cannot be known until runtime.
In our case the compilation went through for the second example.
But the compiler issued a runtime error as the class of the object to which the reference variable is pointing to is not compatible.
For futher study and clear understanding refer RHE converting and casting.
Thanks
chandu!
 
Rashmi Hosalli
Ranch Hand
Posts: 50
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
The first one compiles with class Q as the output while the second one compiles fine and then throws a ClassCastException during the runtime due to the line A a = new B();
Remember...B extends A!
 
Rashmi Hosalli
Ranch Hand
Posts: 50
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry..I was wrong...good explanation,April Johnson and chandrashekar...thanks.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic