简体   繁体   中英

How to Call a Subclass Method From a SuperClass Object

lets suppose i have this Parent Class

public abstract class Parent
{
private String name;
private String surname;
public Parent(String name, String surname)
{
this.name=name;
this.surname=surname;
}

and lets suppose i have many child classes like that and everyone of them has it's own different attributes to add to their parent ones

public class Child extends Parent
{

private String favColor;

public Child(String name,String surname,String favColor)
{
super(name,surname);
this.favColor=favColor;
}

public getFavColor()
{
return this.favColor
}

now i'm in this situation

Parent parent = new Child(name,surname,favColor);

and what i want to do is calling the method getFavColor() on the object parent like this

parent.getFavColor();

is this working? i guess not, so how could i be able to call such method on such object? i thought of declaring abstract getters of childs attributes on the superclass but that doesn't sound very prone to the open/closed principle, because in a time in future when i will want to add more child-like classes i will have to declare every getters of the child attributes in the superclass which is not supposed to know about his childrens.

thank you very much :)

You would need an abstract method to do that. Your parent is already abstract so that's good. It would go something like this:

public abstract class Parent {
    private String name;
    private String surname;
    public Parent(String name, String surname) {
        this.name = name;
        this.surname = surname;
    }

    public void showFavColor() {
        system.print.ln(this.getFavColor());
    }

    abstract string getFavColor();
}

public class Child extends Parent {

    private String favColor;

    public Child(String name, String surname, String favColor) {
        super(name, surname);
        this.favColor = favColor;
    }

    @Override
    public String getFavColor() {
        return this.favColor
    }
}

Every child of the parent MUST extends the abstract function. Since the function is technically declared in the parent, it is accessible from it.

This means, you could do

Parent parent = new Child(name,surname,favColor);
parent.showFavColor();

In this case you can't call the getFavColor() method. The method is defined only in Child class and your reference is Parent. For this, is necessary the definition the getFavColor() method in Parent Class. You would create a abstract method fav() in Parent class:

public abstract class Parent
{
    private String name;
    private String surname;

    public Parent(String name, String surname)
    {
        this.name=name;
        this.surname=surname;
    }

    public abstract String fav();
}

So called:

    parent.fav();

Thus, you can implement the method in different ways on your children, such as:

public class Child extends Parent 
{

    private String favColor;

    public Child(String name,String surname,String favColor)
    {
        super(name,surname);
        this.favColor=favColor;
    }

    public String fav()
    {
        return this.favColor;
    }
}

And:

public class SecondChild extends Parent 
{

    private String favSport;

    public Child(String name,String surname,String favColor)
    {
        super(name,surname);
        this.favColor=favColor;
    }

    public String fav()
    {
        return this.favSport;
    }
}

Use this only if the signature of methods are equals in all children (in your case, if all children methods return a String).

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