2

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 :)

FLM995
  • 41
  • 5
  • Unless the parent class declares that method, you can't. You'd have to cast it to be the subtype, which is something you shouldn't be doing in the parent class (if you are it's indicative that you've got your design wrong somewhere) – JonK Jan 16 '20 at 17:25
  • In your opinion should i declare all the getters of the subclass in the superclass or that would be a violation of the openclosed principle and a bad use of inheritance? – FLM995 Jan 16 '20 at 17:27
  • 1
    If every child class is supposed to have the `getFavColor()`method you could declare it in the parent as an abstract method. This is completly normal programming and not bad use at all. – OH GOD SPIDERS Jan 16 '20 at 17:27
  • not every child has favColor as an attribute, eitherwise they could have all different attributes and different getter, is that bad to declare all the getters in the superclass? – FLM995 Jan 16 '20 at 17:37

2 Answers2

2

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();
Nicolas
  • 8,077
  • 4
  • 21
  • 51
  • not every child has that attribute (favColor) lets suppose i have another subclass ChildB which has an attribute called favSport but not the attribute favColor – FLM995 Jan 16 '20 at 17:46
  • 2
    @FLM995 Then your relationship probably isn't an "is a" relationship, it's a "has a" which means you want interfaces/implements and not subclass/extends. Should I write up an answer about interfaces? – Bill K Jan 16 '20 at 20:58
  • 1
    In your exact case, a child is not a parent so it should not extend. parent and child are both human though so you could add a human class and have them both extend it--at that point either all humans have a favorite color, or only some do. Perhaps all children have a favorite color, if not it's an interface. In general, avoid extends anyway, try to do it as an interface unless it's absolutely obvious it is a perfect case for extends. The fact that all parents don't have a fav color should show that extends isn't good here. – Bill K Jan 16 '20 at 21:29
  • is it allowed something like this: im getting this object from a method Parent parent = new Child(name,surname,favColor); now i wanna work on this object like a Child Object so im doing this ((Child) parent).getFavColor; and im really sure during my code that this one is for sure a Child – FLM995 Jan 17 '20 at 14:51
2

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).

MatheusEdnei
  • 104
  • 1
  • 1
  • 9