Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.help > #2568 > unrolled thread
| Started by | Sai varma <saikondla@gmail.com> |
|---|---|
| First post | 2013-03-07 05:59 -0800 |
| Last post | 2013-03-11 22:35 -0700 |
| Articles | 5 — 4 participants |
Back to article view | Back to comp.lang.java.help
difference between child class object and downcasting object Sai varma <saikondla@gmail.com> - 2013-03-07 05:59 -0800
Re: difference between child class object and downcasting object Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2013-03-07 09:53 -0800
Re: difference between child class object and downcasting object Lew <lewbloch@gmail.com> - 2013-03-10 18:22 -0700
Re: difference between child class object and downcasting object Roedy Green <see_website@mindprod.com.invalid> - 2013-03-09 17:31 -0800
Re: difference between child class object and downcasting object Roedy Green <see_website@mindprod.com.invalid> - 2013-03-11 22:35 -0700
| From | Sai varma <saikondla@gmail.com> |
|---|---|
| Date | 2013-03-07 05:59 -0800 |
| Subject | difference between child class object and downcasting object |
| Message-ID | <46d55c9b-7f17-4927-8b9f-b3acf8a0c407@googlegroups.com> |
hi, i am java fresher, i am a little bit confusion about runtime polymorphism. and what is difference between child class object and downcasting object (i.e.both are access same methods then why we select downcasting). plz give reply. thanks.
[toc] | [next] | [standalone]
| From | Daniel Pitts <newsgroup.nospam@virtualinfinity.net> |
|---|---|
| Date | 2013-03-07 09:53 -0800 |
| Message-ID | <Lu4_s.33037$LX2.28117@newsfe31.iad> |
| In reply to | #2568 |
On 3/7/13 5:59 AM, Sai varma wrote:
> hi,
> i am java fresher,
> i am a little bit confusion about runtime polymorphism.
> and what is difference between child class object and downcasting object (i.e.both are access same methods then why we select downcasting).
> plz give reply.
> thanks.
>
Downcasting gives you access to methods that are on the child class
only. It is often an indication of a bad design, but sometimes it is
unavoidable.
For example, if you have:
class Parent {
public void foo() { System.out.println("parent foo"); }
}
class Child extends Parent {
public void foo() { System.out.println("child foo"); }
public void bar() { System.out.println("child bar"); }
}
Parent o = new Child();
o.foo(); // prints 'child foo';
o.bar(); // Compiler error, no such method on Parent.
((Child)o).bar(); // prints 'child bar'
There is a danger when downcasting though:
Parent o = new Parent;
o.foo(); // prints 'parent foo'.
(Child)o).bar(); // Not a compiler error, but a runtime error.
Downcasting to a class that the object isn't will throw a
ClassCastException.
[toc] | [prev] | [next] | [standalone]
| From | Lew <lewbloch@gmail.com> |
|---|---|
| Date | 2013-03-10 18:22 -0700 |
| Message-ID | <4d91f4f4-b3ba-458b-a944-622a67473b79@googlegroups.com> |
| In reply to | #2569 |
Daniel Pitts wrote:
> Sai varma wrote:
>> i am java [sic] fresher,
>> i am a little bit confusion about runtime polymorphism.
Polymorphism is a compile-time phenomenon, too.
>> and what is difference between child class object and downcasting object (i.e.both are
>> access same methods then why we select downcasting).
An instance of a child class is just an object of the derived type.
A reference to such an object may be of that derived type or of any supertype, up to 'Object'.
So
Object foo = "A string";
defines an instance of the 'String' class (which is therefore also an instance of 'CharSequence',
among other types), and identifies it to the compiler as an 'Object' instance.
That is a compiler view, that 'Object' type. At runtime the object still is what it is, and knows it.
So it is a 'String' no matter what the compiler does or does not know about it.
That is polymorphism. So when you call the 'Object#toString()' method on that 'foo' reference,
foo.toString()
you are calling the instance's own version of that method, which is the 'String' method.
That is polymorphism.
But the compiler doesn't know that. It only knows that it's legal to call 'toString()' on an 'Object'
reference.
But 'foo.substring(1)' would not be legal. Sure, the instance knows it has that method, but 'foo' is
not a 'String' reference, so the compiler rejects the call.
Downcasting (in Java, "down" is from supertype to subtype, "up" is from subtype to supertype) tells
the compiler to treat the instance as a subtype. The instance is not changed. Only the reference (i.e.,
the pointer) is changed.
String bar = (String) foo;
Now the compiler lets you call 'bar.substring(1)'.
However if you point 'foo' to something that is not a 'String', the compiler will let you get away with the
downcast.
foo = new File("foo");
bar = (String) foo;
System.out.println(bar.substring(1));
But the object that 'foo' points to doesn't have such a method, because it is not a 'String'.
So the cast will fail. The instance knows it's not really a 'String' and will force a 'ClassCastException'
when the program executes the bad cast. Happy compiler, sad runtime.
>> plz give reply.
What is this "plz"? It is not an English word.
> Downcasting gives you access to methods that are on the child class
Strictly speaking, it gives the compiler access.
> only. It is often an indication of a bad design, but sometimes it is
> unavoidable.
>
> For example, if you have:
>
> class Parent {
> public void foo() { System.out.println("parent foo"); }
> }
>
> class Child extends Parent {
> public void foo() { System.out.println("child foo"); }
> public void bar() { System.out.println("child bar"); }
> }
>
> Parent o = new Child();
> o.foo(); // prints 'child foo';
> o.bar(); // Compiler error, no such method on Parent.
>
> ((Child)o).bar(); // prints 'child bar'
>
> There is a danger when downcasting though:
>
> Parent o = new Parent;
>
> o.foo(); // prints 'parent foo'.
> (Child)o).bar(); // Not a compiler error, but a runtime error.
Now you can see how this works. 'o' points to an instance of 'Parent'. That instance "knows"
that it is not a 'Child'. The compiler doesn't know this, and believes your downcast. The instance
rejects this claim at runtime with an exception.
> Downcasting to a class that the object isn't will throw a
> ClassCastException.
At runtime.
Happy compiler, sad runtime.
Incidentally, upcasting (called a "widening conversion" in Java) always works and is always safe,
and in fact need not be stated:
Child ch = new Child();
Parent par = (Parent) ch; // unnecessary cast
Parent prent = ch; // perfectly fine
Note that the 'par' and 'prent' variables cannot access methods that exist in 'Child' but
not in 'Parent'.
It is a design principle in Java to push as much validation into the compiler phase as possible.
It is also a best practice to declare the widest (most supertypish) type for a variable that works
for the code.
List<Foo> foos = new ArrayList<>();
'foos' will actually point to an 'ArrayList', but for the code that uses it 'foo' is a 'List' at the compiler
level.
Study this:
http://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html
--
Lew
[toc] | [prev] | [next] | [standalone]
| From | Roedy Green <see_website@mindprod.com.invalid> |
|---|---|
| Date | 2013-03-09 17:31 -0800 |
| Message-ID | <i8onj81dvcu1emlgv2h0f7ptkscq34jfsp@4ax.com> |
| In reply to | #2568 |
On Thu, 7 Mar 2013 05:59:18 -0800 (PST), Sai varma <saikondla@gmail.com> wrote, quoted or indirectly quoted someone who said : >i am java fresher, >i am a little bit confusion about runtime polymorphism. >and what is difference between child class object and downcasting object (i.e.both are access same methods then why we select downcasting). >plz give reply. >thanks. Doing a cast from a base class to a more specific class. The cast does not convert the object, just asserts it actually is a more specific extended object. e.g. Dalmatian d = (Dalmatian) aDog; Most people will stare blankly at you if you use the word downcast. Just use cast. You are giving the assurance this Dog reference actually points to a Dalmatian object, so get ready to use some Dalmatian-only objects on it. This is not a mistake. I am not mindlessly trying to run Dalmatian methods on a Dog reference. It will still check just to make sure. "child class" is not Java terminolgy. We have superclasses and derived classes or subclasses. I presume you mean subclass. Dalmatian is a subclass of Dog. It has the same core with more methods. Every Dalmatian may also be treated as a Dog. Classes are nouns. To cast is a verb. You cannot compare them. -- Roedy Green Canadian Mind Products http://mindprod.com Software gets slower faster than hardware gets faster. ~ Niklaus Wirth (born: 1934-02-15 age: 79) Wirth's Law
[toc] | [prev] | [next] | [standalone]
| From | Roedy Green <see_website@mindprod.com.invalid> |
|---|---|
| Date | 2013-03-11 22:35 -0700 |
| Message-ID | <ojftj89ml9ggp106r6jfh6itjnehs5qdqd@4ax.com> |
| In reply to | #2570 |
On Sat, 09 Mar 2013 17:31:17 -0800, Roedy Green <see_website@mindprod.com.invalid> wrote, quoted or indirectly quoted someone who said : >You are giving the assurance this Dog reference actually points to a >Dalmatian object, so get ready to use some Dalmatian-only objects on >it. This is not a mistake. I am not mindlessly trying to run >Dalmatian methods on a Dog reference. It will still check just to make >sure. oops should read >You are giving the assurance this Dog reference actually points to a >Dalmatian object, so get ready to use some Dalmatian-only METHODS on >it. This is not a mistake. I am not mindlessly trying to run >Dalmatian methods on a Dog reference. It will still check just to make >sure. -- Roedy Green Canadian Mind Products http://mindprod.com Software gets slower faster than hardware gets faster. ~ Niklaus Wirth (born: 1934-02-15 age: 79) Wirth's Law
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.java.help
csiph-web