KB book
118
OCPJP 6.0 93%
OCPJWCD 5.0 98%
SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
upcasting (casting up the inheritance tree to a more general
type) works implicitly (i.e., you don't have to type in the cast) because when you
upcast you're implicitly restricting the number of methods you can invoke, as
opposed to downcasting, which implies that later on, you might want to invoke a
more specific method
OCPJP 6.0 93%
OCPJWCD 5.0 98%
SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
mohitkumar gupta wrote:
1.what's the use of upcasting ?
Animal reference is referring to a animal object.It cannot ever call bark method unless it is made something like this
What I was trying to explain you was if you add a bark method to Dog class, you'll only be able to call it on a Dog reference not an Animal reference. This proves the statement in the book. Using a Dog reference you can call both get and bark method but you can only call the get method from an Animal reference...
OCPJP 6.0 93%
OCPJWCD 5.0 98%
SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
When d is upcasted to Animal, we can't call the bark method with it
OCPJP 6.0 93%
OCPJWCD 5.0 98%
http://plainoldjavaobject.blogspot.in
mohitkumar gupta wrote:
Is the above code doing upcasting ?
i think that the line 2 is just causing a to refer to d.
SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
OCPJP 6.0 93%
OCPJWCD 5.0 98%
mohitkumar gupta wrote:what's the use of upcasting
SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
OCPJP 6.0 93%
OCPJWCD 5.0 98%
SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
1. Accessibility of the code depends on the reference type
2. methods are called relative to the type referred to by the reference variable (if exists in both)
http://plainoldjavaobject.blogspot.in
2.Here d is upcasted to Animal a1 .But,get() method of Dog can still be called using d.Then
what does "when you upcast you're implicitly restricting the number of methods you can invoke" means ?
SCJP 6.0 96%
(Connecting the Dots ....)
Sahil wrote:
Animal a = (Animal) d;
It just means that the reference variable a is now pointing to "Animal part of Dog"
and
It does not mean that d has changed its original pointer to Dog object.
Ankit Wrote:
Animal a = new Dog();
Here I'm upcasting a Dog object to Animal reference, so now I cannot call the bark method on the Dog object. This way I'm restricting the methods that can be called on that Dog object.
OCPJP 6.0 93%
OCPJWCD 5.0 98%
mohitkumar gupta wrote:
KB book
118
upcasting (casting up the inheritance tree to a more general
type) works implicitly (i.e., you don't have to type in the cast) because when you
upcast you're implicitly restricting the number of methods you can invoke, as
opposed to downcasting, which implies that later on, you might want to invoke a
more specific method
1.what's the use of upcasting ?
2.Here d is upcasted to Animal a1 .But,get() method of Dog can still be called using d.Then
what does "when you upcast you're implicitly restricting the number of methods you can invoke" means ?
JAI HO
Did you see how Paul cut 87% off of his electric heat bill with 82 watts of micro heaters? |