Ecco un esempio di quello che sto cercando di chiedereJava polimorfismo Come chiamare al metodo di classe eccellente per oggetto sottoclasse
superclasse Name.java
public class Name{
protected String first;
protected String last;
public Name(String firstName, String lastName){
this.first = firstName;
this.last = lastName;
}
public String initials(){
String theInitials =
first.substring(0, 1) + ". " +
last.substring(0, 1) + ".";
return theInitials;
}
e quindi la sottoclasse è ThreeNames.java
public class ThreeNames extends Name{
private String middle;
public ThreeNames(String aFirst, String aMiddle, String aLast){
super(aFirst, aLast);
this.middle = aMiddle;
}
public String initials(){
String theInitials =
super.first.substring(0, 1) + ". " +
middle.substring(0, 1) + ". " +
super.last.substring(0, 1) + ".";
return theInitials;
}
quindi se creo un oggetto Threename con ThreeNames example1 = new ThreeNames("Bobby", "Sue" "Smith")
e quindi chiamare System.out.println(example1.initials());
mi metterò B.S.S.
ottengo questo.
La mia domanda è c'è un modo per chiamare il metodo iniziali che è nella classe nome in modo che la mia uscita è solo B.S.
grazie fantastici per le risposte! –
possibile duplicato di [Accesso alla funzione di super classe usando l'oggetto sottoclasse] (http://stackoverflow.com/questions/2432249/accessing-super-class-function-using-subclass-object) –