You can not access Employee's and Manager's methods because, even you have an
Object of that type, you are using a Person Classe's variable,
so the one methods and attibutes that are warranted that you can acess using
that variable is the Person Classe's methods and attributes.
The one thing that you can access from the child classe's using its parents
variables are parents methods and attributes that you have overwrited in the
child.
The one way of access the objects real methods and attributes that were'nt
overwrited are casting them. You may use that structure i've sent before.
---
The other question... If you want to put:
1 array of persons;
1 array of employees;
1 array of managers;
If you want to do that using arrays, so you have to do something like that:
Object[] persons = {new Person[10], new Employee[10], new Manager[10]};
This way you have it, and you can recover that using the instanceof and the
cast.
If you want use the java.util.Collection implementing classes:
Collection<Person[]> ppl = new ArrayList<Person[]>();
Person[] pessoas = new Person[10];
Employee[] empregado = new Employee[10];
Manager[] gerente = new Manager[10];
ppl.add(pessoas);
ppl.add(empregado);
ppl.add(gerente);
This way you have it, and you can recover that using the instanceof and the
cast too.
I think you have a better control using Collection's implementations... its
easier to manipulate and to mantain.
If you wanna have an array of arrays and you dont want the subarray to
repeated, so, by steps:
- You shall create a classe (yeah, I think you have to use a collection class)
that extends ArrayList(if you dont need that the itens inside of it be aways
diferent) or HashSet (if you want the elements to be diferents and you have the
hashcode and equals implemented in that object you gonna put there)
- implement its hashcode and equals methods expliciting what make one
collection equal to the other
an example: when the arrays elements aren't of the same object's instance.
- put this collection in another HashSet. HashSets warranties through the
hashcode that the element will not repeat.
Hope it was clear... still training Java... and my english too :)
> Date: Thu, 28 May 2009 18:52:09 -0700
> Subject: [java programming] Re: help to me about Inheritance in java
> From: [email protected]
> To: [email protected]
>
>
> Hi all
> I tried create following:
> Person[] myPerson=new Person[3];
> myPerson[0]=new Person();
> myPerson[1]=new Employee();
> myPerson[2]=new Manager();
>
> but myPerson[1] have not methods and attributes of the Employee class
> myPerson[2] have not also methods and attributes of the Manager class
>
> >
_________________________________________________________________
ConheƧa os novos produtos Windows Live! Clique aqui.
http://www.windowslive.com.br
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---