| Author |
Upcasting of Classes Problem
|
Mohit G Gupta
Ranch Hand
Joined: May 18, 2010
Posts: 634
|
|
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 ?
|
OCPJP 6.0 93%
OCPJWCD 5.0 98%
|
 |
Ankit Garg
Saloon Keeper
Joined: Aug 03, 2008
Posts: 9189
|
|
|
What if I add a method named bark to the Dog class, would you be able to call it using an Animal reference??
|
SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
|
 |
Mohit G Gupta
Ranch Hand
Joined: May 18, 2010
Posts: 634
|
|
i don't think bark method can be called using animal reference.
As,in the code we upcasting d to Animal a1(we are restricting the d to access it's get method)
please,tell is this code restricting d to access it's get method.
if not,then what upcasting does ?
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
|
 |
Ankit Garg
Saloon Keeper
Joined: Aug 03, 2008
Posts: 9189
|
|
|
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...
|
 |
Seetharaman Venkatasamy
Ranch Hand
Joined: Jan 28, 2008
Posts: 5575
|
|
mohitkumar gupta wrote:
1.what's the use of upcasting ?
Hmm,Up-casting is implicit and It is useful. this is used in Type assignment statement.to avoid depending to a particular implementation.
example:
hth
|
 |
Mohit G Gupta
Ranch Hand
Joined: May 18, 2010
Posts: 634
|
|
ankit,you said
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...
Animal reference is referring to a animal object.It cannot ever call bark method unless it is made something like this
how does your statement proves what's written in the book ?
are we upcasting d ?
if yes,then how are we restricting access to methods ,it can call ?
|
 |
Ankit Garg
Saloon Keeper
Joined: Aug 03, 2008
Posts: 9189
|
|
mohit in your code you downcasted a1 to call the bark method. This is what the book is trying to say. If I write this code
When d is upcasted to Animal, we can't call the bark method with it (without using an explicit downcast)...
|
 |
Mohit G Gupta
Ranch Hand
Joined: May 18, 2010
Posts: 634
|
|
When d is upcasted to Animal, we can't call the bark method with it
how to upcast d to Animal ,so that it cannot call the bark method
Is the above code doing upcasting ?
i think that the line 2 is just causing a to refer to d.
Does Animal a=d and a=d mean the same ?
|
 |
Lalit Mehra
Ranch Hand
Joined: Jun 08, 2010
Posts: 369
|
|
yes buddy you can't because there is no bark method in animal ....
|
http://plainoldjavaobject.blogspot.in
|
 |
Ankit Garg
Saloon Keeper
Joined: Aug 03, 2008
Posts: 9189
|
|
mohitkumar gupta wrote:
Is the above code doing upcasting ?
Yes when you assign d to a, there is upcasting.
i think that the line 2 is just causing a to refer to d.
a and d both are reference variables, a reference variable doesn't refer to another reference variable, they always refer to an object, so in the above code both a and d refer to the same Dog object...
|
 |
Mohit G Gupta
Ranch Hand
Joined: May 18, 2010
Posts: 634
|
|
as ankit said we are upcasting d to a.
even if i don't do the upcast still,a cannot access bark method as there is no such method in Animal and d can always access bark method even without the upcast.
so,what's the use of upcasting,it doesnot effect the way d and a function.
|
 |
Ankit Garg
Saloon Keeper
Joined: Aug 03, 2008
Posts: 9189
|
|
mohitkumar gupta wrote:what's the use of upcasting
What type of "use" are you looking for. Upcasting generally results in polymorphic behavior, it helps us to adhere to code-to-interface convention...
|
 |
Mohit G Gupta
Ranch Hand
Joined: May 18, 2010
Posts: 634
|
|
when you
upcast you're implicitly restricting the number of methods you can invoke
ankit said we are upcasting d to a.
even if i don't do the upcast still,a cannot access bark method as there is no such method in Animal and d can always access bark method even without the upcast.
how is d resrticted to invoke methods here?
how would you prove the above point in bold as given in KB book ?
|
 |
Ankit Garg
Saloon Keeper
Joined: Aug 03, 2008
Posts: 9189
|
|
|
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. This in no way means that the bark method cannot be called on this Dog object in any way. I can use the syntax ((Dog)a).bark() to call the bark method. But using the plain a reference object, I can't call the bark method...
|
 |
Lalit Mehra
Ranch Hand
Joined: Jun 08, 2010
Posts: 369
|
|
First of all there are two things ...
look at this code snippet ... here Animal is the superclass of Dog but animal does not contain the method bark
Now read this ...
Animal reference referring to a Dog object cannot access the bark method of type Dog ... because (as here) Animal does not contain bark ...
and when you use references which refer to subclass objects they can only access those instance variables and methods which they know i.e. defined inside the ( as here) Animal class. (compile time polymorphism)
but when you invoke a method which is present in both the methods ... say the height method listed above it calls the Dogs height() (runtime polymorphism)
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)
cheers ... hope this solves your problem
Lucky
|
 |
Sahil Kapoor
Ranch Hand
Joined: Sep 12, 2009
Posts: 316
|
|
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 ?
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.
It is same like the following :-
boolean b=true;
boolean a= !b;
Now a is false but b has not change to false, it is still true.
Thanks !!!
|
SCJP 6.0 96%
(Connecting the Dots ....)
|
 |
Mohit G Gupta
Ranch Hand
Joined: May 18, 2010
Posts: 634
|
|
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.
Ankit say's that DOg object is casted to animal reference while according to sahil,it means that a is pointing to Dog object.
why are the 2 answers different ?
|
 |
sandeep jaiswal
Greenhorn
Joined: Nov 30, 2006
Posts: 6
|
|
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 ?
[SANDEEP]
To answer Your 1st question, use of up casting:
Qus: Y up casting exist in JAVA?
Ans: To achieve run time polymorphism.
Que: Whats that?
Ans: Decide which class's method going to be called actually at runtime.
Que: How do you achieve that?
And:
Best use can be described if you understand observer pattern...
Here is a best link on the NET.
http://java-x.blogspot.com/2007/01/implementing-observer-pattern-in-java.html
Chapter closed
Jai Raam Ji ki
|
JAI HO
|
 |
 |
|
|
subject: Upcasting of Classes Problem
|
|
|