Created
April 29, 2013 15:12
-
-
Save kinisoftware/5482235 to your computer and use it in GitHub Desktop.
Herencia/Jerarquia con array [] en Java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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