• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Upcasting of Classes Problem

 
Mohit G Gupta
Ranch Hand
Posts: 634
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?
 
Ankit Garg
Sheriff
Posts: 9521
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What if I add a method named bark to the Dog class, would you be able to call it using an Animal reference??
 
Mohit G Gupta
Ranch Hand
Posts: 634
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 9521
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 634
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 9521
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 634
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 384
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes buddy you can't because there is no bark method in animal ....


 
Ankit Garg
Sheriff
Posts: 9521
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 634
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



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
Sheriff
Posts: 9521
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 634
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 9521
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 384
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 316
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 !!!
 
Mohit G Gupta
Ranch Hand
Posts: 634
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic