Let's say you have something like:
class Parent{
}
class Child extends Parent{
}
class main{
public static void main(String[] args){
Parent p=new Parent();
Child c=new Child();
p=(Parent)c; //upcast-implicit, referes to a Child type variable
as it is a Parent type.
if(p instanceof Child)
c=(Child)p;//downcast-you need to type it and can throw
ClassCastExcepion if you don't make the is-a test before doing it, and
refers to a Parent type variable as it is a Child type
}
Casting is needed for being able to apply, for example Child methods
to a Parrent type variable.
On Jun 18, 3:38 am, Moses <[email protected]> wrote:
> Hello,
>
> Could someone please help me understand what it means to "cast an
> object".
>
> Thank-you.
--~--~---------~--~----~------------~-------~--~----~
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/javaprogrammingwithpassion?hl=en
-~----------~----~----~----~------~----~------~--~---