Hi Dan

The type of casting you have shown here is almost certainly implicit
casting, this is because Student either implements the Person interface or
inherits from the person class. In short a Student is a Person so the
compiler won't grumble on this.

The reason you might want to use this is to allow you to write code that
will work with "People" i.e. Students, Teachers, Footballers, you name it,
your code might not care who or what the person is, just that they are a

Say you had the following method

Public void BeNiceTo(Person p)

You could do the following:

Person p1 = new Student();

Person p2 = new Teacher();

And then do this:


and both will work. I should point out that you can also do this:

this.BeNiceTo(new Student());
this.BeNiceTo(new Teacher());

The compiler is smart enough to figure this out.

Finally for implicit casting you could do this:

Public Person GetRandomPerson()
Return new AnyThingYouLikeAsLongAsItImplementsThePersonInterface();

And then do


the benefit of all of the above is you can write code that can be used by
any future object that decides to be a person.

The other type of casting is explicit, this is where you have to tell the
compiler what to do. This usually arises when you have specific knowledge
about an object.

Say you do this;

Object o1 = new Person();
Object o2 = new Object();

You can get the object back to a person by explicitly casting back to the
Person interface.

Person p1 = (Person)o1;

Note if you did

Person p2 = (Person)o2;

This would fail at runtime because o2 is not a Person.

One final thing is that when casting objects, the object itself never
changes type, just the variable you use to reference the object changes.

This is not the case for value type however,

Hope this info helps a little, and hasn't just made the water a little



-----Original Message-----
From: midrange-l-bounces@xxxxxxxxxxxx
[mailto:midrange-l-bounces@xxxxxxxxxxxx] On Behalf Of Dan
Sent: 28 October 2009 22:23
To: Midrange Systems Technical Discussion
Subject: ITCAP exam for "Beginning Java Programming"

Anyone taken this?

I am taking a week-long course that covers a 390-page course book and will
be taking the ITCAP certification exam on Friday afternoon. I don't know if
"Beginning Java Programming" is the actual name of the exam, but the course
is supposed to get us to where we can pass it. While the "certification" is
a nice acknowlegement, passing the exam is crucial since the cost will be
covered by the corporation's general "educational" funds, as opposed to my
department's disbursement code if I don't pass the exam.

The instructor indicated that the exam normally takes about 15 minutes to
complete. It occurs to me, then, that we are covering a whole lot more
material in 35 hours than we will be tested on.

My weak spots are in some fundamental stuff like casting, for example:
Person p1 = new Student();
and then using p1. I will be googling for this type of stuff tonight, but
most of what I've found thus far is "technical reference" and not any
"here's why you want to do this".

Advice appreciated!
- Dan

This thread ...


Follow On AppleNews
Return to Archive home page | Return to MIDRANGE.COM home page

This mailing list archive is Copyright 1997-2019 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].