Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #27268

Re: question about casting and inheritance

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!news.stack.nl!aioe.org!.POSTED!not-for-mail
From Ricardo Palomaes <rpmdisguise-java@nowhere.com>
Newsgroups comp.lang.java.programmer
Subject Re: question about casting and inheritance
Date Thu, 09 Jan 2014 23:24:06 +0100
Organization Aioe.org NNTP Server
Lines 43
Message-ID <lan7i2$4qs$1@speranza.aioe.org> (permalink)
References <5dfabbeb-9201-488d-b56b-e1cef3913c92@googlegroups.com>
NNTP-Posting-Host slL9sg8FQ1JGRZMftNBN5Q.user.speranza.aioe.org
Mime-Version 1.0
Content-Type text/plain; charset=ISO-8859-1
Content-Transfer-Encoding quoted-printable
X-Complaints-To abuse@aioe.org
User-Agent Mozilla/5.0 (X11; Linux i686; rv:26.0) Gecko/20100101 Firefox/26.0 SeaMonkey/2.23
X-Notice Filtered by postfilter v. 0.8.2
Xref csiph.com comp.lang.java.programmer:27268

Show key headers only | View raw


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

Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


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