Skip to content

Instantly share code, notes, and snippets.

@kinisoftware
Created April 29, 2013 15:12
Show Gist options
  • Save kinisoftware/5482235 to your computer and use it in GitHub Desktop.
Save kinisoftware/5482235 to your computer and use it in GitHub Desktop.
Herencia/Jerarquia con array [] en Java
public class Prueba {
public class Padre {
}
public class Hija extends Padre {
@Override
public String toString() {
return "soy una hija";
}
}
@Test
public void test() {
List<Padre> hijas = new ArrayList<Padre>();
hijas.add(new Hija());
System.out.println(hijas.size());
Padre[] hijas2 = new Padre[1];
hijas2[0] = new Hija();
System.out.println(hijas2.length);
// hola(hijas2); --> Pego un pete de casting ^_^
hola2(hijas2);
}
private void hola(Padre[] hijas) {
Hija[] hijasCopia = (Hija[]) hijas;
System.out.println(hijasCopia[0]);
}
private void hola2(Padre[] hijas) {
Hija hijaCopia = (Hija) hijas[0];
System.out.println(hijaCopia);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment