Reputation: 7243
Imagine I have this:
public class Animal {
private String racaAnimal;
private String corAnimal;
public String getCorAnimal() {
return this.corAnimal;
}
public String getRacaAnimal() {
return this.racaAnimal;
}
public Animal getAnimaisCliente(int indice) {
return this.animaisCliente[indice];
}
}
public class Estimacao extends Animal{
private String nomeAnimal;
public String getNomeAnimal() {
return nomeAnimal;
}
}
public class Cliente{
private Animal[] animaisCliente;
}
Constructors aren't showing but they are working fine.
I have one arraylist that holds all Cliente
ArrayList<Cliente> clientes = new ArrayList<Cliente>();
And a animal is created like this
Estimacao animaisEstimacao = new Estimacao(nomeAnimal,racaAnimal,corAnimal);
and then its added to the array of Animal in Cliente
Now if I do this:
System.out.println(" Raça: " + clientes.get(0).getAnimaisCliente(0).getRacaAnimal());
It works. But how can i get nomeAnimal from class Estimacao?
If i put
System.out.println(" Nome: " + clientes.get(0).getAnimaisCliente(0).getNomeAnimal());
it do not works.
From a subclass we can get things from the super class but the other way arroud? is it possible?
Upvotes: 0
Views: 397
Reputation: 795
In addition to the answers provided above, you may want to just consider the general design of your classes. If the nomeAnimal
is something that the Animal
class should really be aware of then it might make sense to push it up (even the name of the variable suggests its Animal
-ness).
If not, then you may want to further consider leveraging polymorphism by adding a displaySpecificInfo() to the Animal
class and either making it abstract
, or adding an empty implementation in the Animal
class. That would allow you to call it from where your current - failing - print call is, and then have the current line in the derived class's implementation. This would save the need for the cast (not that there's anything wrong with casting), as well as create a more OO/Encapsulation-compliant implementation.
Edit:
Excellent note by @Guillaume and my apologies for potentially peeling back more layers of the OO onion then you are interested in! :)
Upvotes: 1
Reputation: 7234
No you can't do it unless you cast the Animal type object to Estimacao type. Only after the cast, the getNomeAnimal() will be available to you. But this is a bad thing to do. If your array contains a mix of Animal type and Estimacao type objects then it will fail with ClassCastException.
Upvotes: 0
Reputation: 2476
Think of it this way: an instance of a subclass is also an instance of the superclass. But an instance of the superclass is not necessarily an instance of the subclass. Thus the superclass methods are always available in both, but the subclass methods are only available in the subclass.
If you are sure you have an instance of the subclass, you can cast it explicitly and call the subclass-specific method on it, but you need to take care in doing so.
Upvotes: 3
Reputation: 1868
This is a misplaces usage in your animal class:
this.animaisCliente[indice] // animaisCliente is not an attribute of Animal
Upvotes: 0
Reputation: 797
It is not possible directly as you are asking. However, you could add getNomeAnimal()
in your base class (Animal
), that would return null
or an empty string - then the instances which are of Estimacao
class will return the correct values.
Upvotes: 0
Reputation: 32969
Cast the Animal
to a Estimacao
to access the methods defined in Estimacao
Upvotes: 0
Reputation: 22822
You need to cast your Animal
to Estimacao
. But obviously, if you don't want to risk an exception, make sure your Animal
is of the right class before:
Animal animal = clientes.get(0).getAnimaisCliente(0);
if (animal instanceof Estimacao) {
System.out.println(" Nome: " + ((Estimacao) animal).getNomeAnimal());
}
Upvotes: 5
Reputation: 3519
Try
System.out.println(" Nome: " + ((Estimacao) clientes.get(0).getAnimaisCliente(0)).getNomeAnimal());
Upvotes: 2