简体   繁体   中英

Learning about Polymorphism

Why can't I access the method inside of the Daily class from the main located in the Test class. Specifically the occursOn() method is the one that I would like to access form the main. If I create an object originally of type Daily then it allows me to access the occursOn() method but if I originally declare it as a general Appointment then it doesn't.

I'm trying to teach myself polymorphism btw.

public class Appointment{

private String description;
private String date;

public Appointment(){
    this.description = "";
    this.date = "";
}
public Appointment(String de, String da){
    this.description = de;
    this.date = da;
}

public void setDescription(String d){
    this.description = d;
}
public String getDescription(){
    return this.description;
}
public void setDate(String d){
    this.date = d;
}
public String getDate(){
    return this.date;
}

public String toString() {
    return "Appointment [description=" + description + ", date=" + date + "]";
}
}

public class Daily extends Appointment{

public Daily(){
    super();
}
public Daily(String de, String da){
    super(de, da);
}

public boolean occursOn(int year, int month, int day){
    return true;
}
public String toString(){
    String str = "Daily " + super.toString();
    return str;
}
}

public class Test {

public static void main(String [] args){

    Appointment appt1;

    appt1 = new Onetime("See doctor", "01/01/2015");

}
}

This is because child classes inherit the attributes of parent classes, not the other way around. Although you can generalize an instance of Daily to an Appointment, you lose the ability to call Daily-specific methods on the instance without a cast.

If you want to access occursOn() in both Daily and Appointment, you need to declare the method in the Appointment class so that it is inherited by its subclasses.

Appointment doesn't have an occursOn() .

If you have maleDuck extends duck and you give me either one, but only tell me it's a duck, I can make it quack() , but i don't know if it layseggs() .

Duck ladyDuck = new FemaleDuck();
ladyDuck.layseggs();  //WRONG, not a ducks lay eggs

FemaleDuck ladyDuck = new FemaleDuck();
ladyDuck.layseggs();  //CORRECT, it's clear we're dealing with female ducks

What's interesting about polymorphism is when we overload the quack() (pseudo-code:)...

Duck[] ducks = { new MaleDuck(), new FemaleDuck() };
foreach (duck in ducks) { duck.quack() };
Assert("1 manish quack and 1 ladyish quack just happened");

...we can treat them identically and access their unique behavior. But we can't just go calling layegg() on the members of that array, because the MaleDucks would struggle.

Check out some tutorials on Polymorphism.

I don't know why I didn't put this in terms of appointments. But I feel it's just unwritten law that all polymorphism examples must be explained in terms of either cars or animals. :D

Your Appointment class needs a method occursOn, it doesn't need to be filled. then you can override it in classes that override appointment.

If you declare a Daily as an Appointment , you must downcast the variable to its proper type in order to access Daily 's occursOn method:

 Appointment appt1;
 appt1 = new Daily("See Doctor", "01/01/2015");
 ((Daily) appt).occursOn();

If a type is declared as an Appointment , then the compiler is only aware of Appointment methods, even if the underlying type is Daily . 'Downcasting' is the process by which you refine a reference's type to one of it's derived classes. Note that you can only downcast to a derived type when the underlying class is actually of the derived type, for instance, if you try this:

 Appointment appt1;
 appt1 = new Appointment("See Doctor");
 ((Daily) appt).occursOn(); //Compiler throws an error!!

You will end up in a world of hurt.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM