Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #27276
| From | Arved Sandstrom <asandstrom2@eastlink.ca> |
|---|---|
| Newsgroups | comp.lang.java.programmer |
| Subject | Re: question about casting and inheritance |
| References | <5dfabbeb-9201-488d-b56b-e1cef3913c92@googlegroups.com> <dsmdnYjx4MdbTlLPnZ2dnUVZ_sSdnZ2d@earthlink.com> |
| Message-ID | <wPbAu.255231$Uv.58179@fx30.iad> (permalink) |
| Organization | Public Usenet Newsgroup Access |
| Date | 2014-01-11 09:38 -0400 |
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
Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
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
csiph-web