Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #27262 > unrolled thread
| Started by | marc.at.compass@gmail.com |
|---|---|
| First post | 2014-01-09 10:48 -0800 |
| Last post | 2014-01-11 08:01 -0800 |
| Articles | 10 — 9 participants |
Back to article view | Back to comp.lang.java.programmer
question about casting and inheritance marc.at.compass@gmail.com - 2014-01-09 10:48 -0800
Re: question about casting and inheritance leif.roar@invalid.com (Leif Roar Moldskred) - 2014-01-09 13:03 -0600
Re: question about casting and inheritance Eric Sosman <esosman@comcast-dot-net.invalid> - 2014-01-09 14:07 -0500
Re: question about casting and inheritance markspace <markspace@nospam.nospam> - 2014-01-09 13:00 -0800
Re: question about casting and inheritance Ricardo Palomaes <rpmdisguise-java@nowhere.com> - 2014-01-09 23:24 +0100
Re: question about casting and inheritance Ricardo Palomares <rpmdisguise-java@nowhere.com> - 2014-01-12 12:20 +0100
Re: question about casting and inheritance Patricia Shanahan <pats@acm.org> - 2014-01-10 03:04 -0800
Re: question about casting and inheritance Arved Sandstrom <asandstrom2@eastlink.ca> - 2014-01-11 09:38 -0400
Re: question about casting and inheritance Roedy Green <see_website@mindprod.com.invalid> - 2014-01-10 16:36 -0800
Re: question about casting and inheritance marc.at.compass@gmail.com - 2014-01-11 08:01 -0800
| From | marc.at.compass@gmail.com |
|---|---|
| Date | 2014-01-09 10:48 -0800 |
| Subject | question about casting and inheritance |
| Message-ID | <5dfabbeb-9201-488d-b56b-e1cef3913c92@googlegroups.com> |
Hello,
I'm preparing for Java 7 OCA exam. I have a q about casting..
I learned you can cast a variable of a class to another object only if there's a hierarchical relation between the variable's class _and_ the object's class you cast to. And also, the varibla should point to an object og the class you're casting to or you get a ClassCastException during runtime.
So I made up this example:
class A {
String uniqueMethodForA(){
return ("uniqueMethodForA");
}
}
class B extends A {
String uniqueMethodForB(){
return ("uniqueMethodForB");
}
}
public class LearnJava{
public static void main(String[] args) throws Exception {
LearnJava LJ = new LearnJava();
A a = new A();
B b = new B();
System.out.println(a.uniqueMethodForA());
System.out.println(b.uniqueMethodForB());
a=b;
b=(B)a;
System.out.println("=============");
System.out.println(a.uniqueMethodForA());
System.out.println(b.uniqueMethodForB());
}
}
outputs:
uniqueMethodForA
uniqueMethodForB
=============
uniqueMethodForA
uniqueMethodForB
As you see variable a points to an instance of A and not to (an instance of) B. I effectively say "treat a as if it is b". I can even call a method on b that is unique for B. But a didn't point to an instance of B in the first place at all!
Why is it the line b=(B)a; doesn't give me a ClassCastException during runtime?
[toc] | [next] | [standalone]
| From | leif.roar@invalid.com (Leif Roar Moldskred) |
|---|---|
| Date | 2014-01-09 13:03 -0600 |
| Message-ID | <K_KdnW6bqt0Lb1PPnZ2dnUVZ8tSdnZ2d@giganews.com> |
| In reply to | #27262 |
marc.at.compass@gmail.com wrote: > > As you see variable a points to an instance of A and not to (an instance of) > B. I effectively say "treat a as if it is b". I can even call a method on b > that is unique for B. But a didn't point to an instance of B in the first > place at all! > > Why is it the line b=(B)a; doesn't give me a ClassCastException during runtime? Because on the line before you do the cast, you set /a/ to point to the same instance of /B/ as /b/ points to: a = b; You no longer have an object of class /A/ in /a/, but one of class /B/ and _that_ can safely be cast to /B/ without error. -- Leif Roar Moldskred
[toc] | [prev] | [next] | [standalone]
| From | Eric Sosman <esosman@comcast-dot-net.invalid> |
|---|---|
| Date | 2014-01-09 14:07 -0500 |
| Message-ID | <lams0q$rk9$1@dont-email.me> |
| In reply to | #27262 |
On 1/9/2014 1:48 PM, marc.at.compass@gmail.com wrote:
> Hello,
> I'm preparing for Java 7 OCA exam. I have a q about casting..
> I learned you can cast a variable of a class to another object only if there's a hierarchical relation between the variable's class _and_ the object's class you cast to. And also, the varibla should point to an object og the class you're casting to or you get a ClassCastException during runtime.
> So I made up this example:
>
> class A {
> String uniqueMethodForA(){
> return ("uniqueMethodForA");
> }
> }
>
> class B extends A {
> String uniqueMethodForB(){
> return ("uniqueMethodForB");
> }
> }
>
> public class LearnJava{
> public static void main(String[] args) throws Exception {
> LearnJava LJ = new LearnJava();
> A a = new A();
> B b = new B();
> System.out.println(a.uniqueMethodForA());
> System.out.println(b.uniqueMethodForB());
> a=b;
> b=(B)a;
> System.out.println("=============");
> System.out.println(a.uniqueMethodForA());
> System.out.println(b.uniqueMethodForB());
> }
> }
> outputs:
> uniqueMethodForA
> uniqueMethodForB
> =============
> uniqueMethodForA
> uniqueMethodForB
>
> As you see variable a points to an instance of A and not to (an instance of) B. I effectively say "treat a as if it is b". I can even call a method on b that is unique for B. But a didn't point to an instance of B in the first place at all!
> Why is it the line b=(B)a; doesn't give me a ClassCastException during runtime?
Because "a" refers to the object originally pointed to by "b",
that is, to the object created by "new B()". That object is a B
object, so converting its A-type reference "a" to a B-type reference
is no problem. For contrast, try
A a = new A();
B b = new B();
...
A a2 = b;
B b2 = (B) a; // ClassCastException here
--
Eric Sosman
esosman@comcast-dot-net.invalid
[toc] | [prev] | [next] | [standalone]
| From | markspace <markspace@nospam.nospam> |
|---|---|
| Date | 2014-01-09 13:00 -0800 |
| Message-ID | <lan2l6$657$1@dont-email.me> |
| In reply to | #27262 |
On 1/9/2014 10:48 AM, marc.at.compass@gmail.com wrote: > a=b; > b=(B)a; > Why is it the line b=(B)a; doesn't give me a ClassCastException during runtime? Because the line above assigns 'b' to 'a'. It's actually the only interesting thing inside the program. The second line with the cast is a tautology: it casts 'b' to a 'B'. A more explicit naming scheme for your variables would help a lot to understand this.
[toc] | [prev] | [next] | [standalone]
| From | Ricardo Palomaes <rpmdisguise-java@nowhere.com> |
|---|---|
| Date | 2014-01-09 23:24 +0100 |
| Message-ID | <lan7i2$4qs$1@speranza.aioe.org> |
| In reply to | #27262 |
El 09/01/14 19:48, marc.at.compass@gmail.com escribió: > As you see variable a points to an instance of A and not to (an > instance of) B. In fact, variable "a" is a reference to an object of class A. Since class B extends A, an object of class B is also an object of class A (it is "just a more specific type of A"). Thus, when you do: a = b; a is referencing the same object than b, ie., an object of class B. While accessing that object from "a", it only can behave as a class A object, and you couldn't do a.uniqueMethodForB(). However, as soon as you access the object from b again, you can access again the methods exclusive of a class B object. Note that, during this sequence: a = b; b = (B) a; the object itself has not been touched nor modified at all; you only have modified the references "a" and "b" (and "b" has been modified only to leave exactly as it was). > I effectively say "treat a as if it is b". I can > even call a method on b that is unique for B. But a didn't point to > an instance of B in the first place at all! Yes, it did. At least, since you did a = b. :-) > Why is it the line b=(B)a; doesn't give me a ClassCastException > during runtime? Because a holds a reference to a class A object that happens also to be a class B object, so it is not wrong to reference as such. HTH
[toc] | [prev] | [next] | [standalone]
| From | Ricardo Palomares <rpmdisguise-java@nowhere.com> |
|---|---|
| Date | 2014-01-12 12:20 +0100 |
| Message-ID | <lattp2$2d7$1@speranza.aioe.org> |
| In reply to | #27268 |
El 11/01/14 18:40, Stefan Ram escribió: > Ricardo Palomaes <rpmdisguise-java@nowhere.com> writes: >> class B extends A, an object of class B is also an object of class A > > I try to say »/instance/ of class A« in such a case. I was about to change it before sending it, indeed, :-) but I thought it would be clearer to the OP to see "object".
[toc] | [prev] | [next] | [standalone]
| From | Patricia Shanahan <pats@acm.org> |
|---|---|
| Date | 2014-01-10 03:04 -0800 |
| Message-ID | <dsmdnYjx4MdbTlLPnZ2dnUVZ_sSdnZ2d@earthlink.com> |
| In reply to | #27262 |
On 1/9/2014 10:48 AM, marc.at.compass@gmail.com wrote:
> Hello,
> I'm preparing for Java 7 OCA exam. I have a q about casting..
> I learned you can cast a variable of a class to another object only if there's a hierarchical relation between the variable's class _and_ the object's class you cast to. And also, the varibla should point to an object og the class you're casting to or you get a ClassCastException during runtime.
> So I made up this example:
>
> class A {
> String uniqueMethodForA(){
> return ("uniqueMethodForA");
> }
> }
>
> class B extends A {
> String uniqueMethodForB(){
> return ("uniqueMethodForB");
> }
> }
>
> public class LearnJava{
> public static void main(String[] args) throws Exception {
> LearnJava LJ = new LearnJava();
> A a = new A();
> B b = new B();
> System.out.println(a.uniqueMethodForA());
> System.out.println(b.uniqueMethodForB());
> a=b;
> b=(B)a;
> System.out.println("=============");
> System.out.println(a.uniqueMethodForA());
> System.out.println(b.uniqueMethodForB());
> }
> }
> outputs:
> uniqueMethodForA
> uniqueMethodForB
> =============
> uniqueMethodForA
> uniqueMethodForB
>
> As you see variable a points to an instance of A and not to (an instance of) B. I effectively say "treat a as if it is b". I can even call a method on b that is unique for B. But a didn't point to an instance of B in the first place at all!
> Why is it the line b=(B)a; doesn't give me a ClassCastException during runtime?
>
There are two ways of thinking about Java that, in combination, make
this type of issue very simple and clear:
1. Distinguish between the type of an expression and the class of an object.
2. Every non-null reference expression is a pointer to an object whose
class is matches the expression's type, or extends it, or implements it.
Every expression has a type that is determined at compile time. For a
variable or parameter the type is directly declared. The type of
variable a is A, and the type of variable b is B. Other expressions have
types that can be determined from the nature of the expression and the
types of its operands. The type of "(B)a" is B.
Each object has a class that is determined by how the object is created,
through clone() or new. The class of the object created by "new B()" is
B, and remains B regardless of what variables or expressions point to it.
Applying this thinking to the code above, "a=b" assigns to variable a
the value of b, which is a pointer to an object of class B. Because B
extends A, the compiler knows that variable b either is null or is a
pointer to an object that variable a is allowed to point to, so no cast
is needed.
"b=a" would be rejected at compile time because variable a can point to
objects that variable b cannot point to.
"b=(B)a" is accepted by the compiler because variable a can point to an
object that variable b can also point to. It would cause a
ClassCastException at run time if, in fact, variable a pointed to an
object variable b cannot. It points to an object that was created by
"new B()", an object whose class is B, so the cast is valid.
Patricia
[toc] | [prev] | [next] | [standalone]
| From | Arved Sandstrom <asandstrom2@eastlink.ca> |
|---|---|
| Date | 2014-01-11 09:38 -0400 |
| Message-ID | <wPbAu.255231$Uv.58179@fx30.iad> |
| In reply to | #27269 |
On 01/10/2014 07:04 AM, Patricia Shanahan wrote: [SNIP]> > There are two ways of thinking about Java that, in combination, make > this type of issue very simple and clear: > > 1. Distinguish between the type of an expression and the class of an > object. > > 2. Every non-null reference expression is a pointer to an object whose > class is matches the expression's type, or extends it, or implements it. > > Every expression has a type that is determined at compile time. For a > variable or parameter the type is directly declared. The type of > variable a is A, and the type of variable b is B. Other expressions have > types that can be determined from the nature of the expression and the > types of its operands. The type of "(B)a" is B. > > Each object has a class that is determined by how the object is created, > through clone() or new. The class of the object created by "new B()" is > B, and remains B regardless of what variables or expressions point to it. > > Applying this thinking to the code above, "a=b" assigns to variable a > the value of b, which is a pointer to an object of class B. Because B > extends A, the compiler knows that variable b either is null or is a > pointer to an object that variable a is allowed to point to, so no cast > is needed. > > "b=a" would be rejected at compile time because variable a can point to > objects that variable b cannot point to. > > "b=(B)a" is accepted by the compiler because variable a can point to an > object that variable b can also point to. It would cause a > ClassCastException at run time if, in fact, variable a pointed to an > object variable b cannot. It points to an object that was created by > "new B()", an object whose class is B, so the cast is valid. > > Patricia "Simple and clear" to a person that knows OO quite well already, Patricia. :-) AHS -- When a true genius appears, you can know him by this sign: that all the dunces are in a confederacy against him. -- Jonathan Swift
[toc] | [prev] | [next] | [standalone]
| From | Roedy Green <see_website@mindprod.com.invalid> |
|---|---|
| Date | 2014-01-10 16:36 -0800 |
| Message-ID | <6e41d9hugqo84v8pvel4kokse7o8pbqcv4@4ax.com> |
| In reply to | #27262 |
On Thu, 9 Jan 2014 10:48:34 -0800 (PST), marc.at.compass@gmail.com wrote, quoted or indirectly quoted someone who said : >I'm preparing for Java 7 OCA exam. I have a q about casting.. see http://mindprod.com/jgloss/cast.html for material to help you understand casting in general. Others will point out what you did wrong in this case. -- Roedy Green Canadian Mind Products http://mindprod.com Unlike many machines, computers require no water once they are manufactured.
[toc] | [prev] | [next] | [standalone]
| From | marc.at.compass@gmail.com |
|---|---|
| Date | 2014-01-11 08:01 -0800 |
| Message-ID | <d6e5d48e-01b6-4bc4-a29b-f8b272c8b91a@googlegroups.com> |
| In reply to | #27262 |
Thanks all for the replies! Very informative. I'm a further on the path of casting and definitely will follow up on the link about casting given. thanks again! Marc
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.java.programmer
csiph-web