Hello... what method is the isInstance? is that the same of the comparator 
instanceof?

Rodrigo Ornellas

From: [email protected]
To: [email protected]; [email protected]
Subject: [java programming] Re: help to me about Inheritance in java
Date: Thu, 28 May 2009 20:36:18 +0000








Hello Miga,

About that example you gave:

Im not sure if i've understand well what you need, anyway...

You can recover the objects in the array and access'em as an instance of the 
class you specified in the Array Object Type.

When
you do it, you can only access the Object Type methods that you have
specified, because that's the variable type in the array.
To work with'em as the Class they really are, you have to Cast'em like that:
 
Its gonna be wrong if u do like that if you have an array of type Person:
   persons[i].managersMethodName();

With cast its gonna be ok:
(  (Manager) persons[i] ).managersMethodName();

If you want to build an array of persons, but when accessing its indexes you 
want to treat'em as its own Classes instance
you can use the "instanceof" comparator, that is able to get the real objects 
type, and the cast.
This way you can warranty that the instance you're casting is really an 
instance of that Classe you cast.

and example:
if you gonna acess diferents methods to know the persons incomming, thats a 
solution to do that:

Double incomming = null;

if(persons[i] instanceof Employee) {

    incomming = (  (Employee) persons[i] ).employeesInconmmingMethodName();

}else if(persons[i] instanceof Manager) {

    incomming = (  (Manager) persons[i] ).managersInconmmingMethodName();

}

So, that's the safe and elegant way to do that.

Never
forget that the if's must be if's and else's blocks in this case,
because the object may extend from some classe, that extends another
classe, and then the object will be an instance of classes those
classes too.

> Date: Thu, 28 May 2009 10:41:23 -0700
> Subject: [java programming] Re: help to me about Inheritance in java
> From: [email protected]
> To: [email protected]
> 
> 
> 
> 
> On May 28, 6:51 pm, [email protected] wrote:
> > I have three class as following
> >
> > - person class
> > - employee class
> > - manager class
> >
> > employee class and manager class extend the class person
> > I want to create a person array with 3 elements
> > element 1 is person
> > element 2 is employee
> > element 3 is manager
> You cannot do this directly in Java as an Array in Java is of a
> defined type, unless you declare an Array of Object or of Person,
> which is relatively vague.
> You may use the same technik as in the homework of Generics lesson.
> >
> > how to create 3 elements ?
> </html




_________________________________________________________________
Descubra todas as novidades do novo Internet Explorer 8
http://brasil.microsoft.com.br/IE8/mergulhe/?utm_source=MSN%3BHotmail&utm_medium=Tagline&utm_campaign=IE8
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to