Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.glorb.com!border3.nntp.dca.giganews.com!Xl.tags.giganews.com!border1.nntp.dca.giganews.com!nntp.giganews.com!local2.nntp.dca.giganews.com!nntp.earthlink.com!news.earthlink.com.POSTED!not-for-mail NNTP-Posting-Date: Fri, 10 Jan 2014 05:04:38 -0600 Date: Fri, 10 Jan 2014 03:04:43 -0800 From: Patricia Shanahan User-Agent: Mozilla/5.0 (Windows NT 5.2; WOW64; rv:24.0) Gecko/20100101 Thunderbird/24.2.0 MIME-Version: 1.0 Newsgroups: comp.lang.java.programmer Subject: Re: question about casting and inheritance References: <5dfabbeb-9201-488d-b56b-e1cef3913c92@googlegroups.com> In-Reply-To: <5dfabbeb-9201-488d-b56b-e1cef3913c92@googlegroups.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Message-ID: Lines: 77 X-Usenet-Provider: http://www.giganews.com NNTP-Posting-Host: 70.230.195.188 X-Trace: sv3-ZKi1tmRRM6Al+MdVtgoRrCZHA3+v33BBBUvTkLyUNbz8QXRxVXc+LV5vntNkFxgQiBzmy3k9ZEcIJks!37NbWIwBCoNRx4ZhUEmUxXApNzskWvAPh1WXiWNqtzY4/URC3Xl0N74fwjD3cunTHpVRY2Be7FPH!Sy7UywVQGna9LLvUpcB1xBPGifex0g/IKDEcKGr20SNuoDc= X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly X-Postfilter: 1.3.40 X-Original-Bytes: 4417 Xref: csiph.com comp.lang.java.programmer:27269 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