简体   繁体   中英

Instantiate a variable in a child class

is possible to instantiate a variable in a child class that was declared in the parent class? What would be the advantage? Ex:

public class Animal{ Food foodType;}
public class Dog extends Animal{ 
   public Dog(){
       foodType=new Food();
   }
}

There is a lot of advantage of doing it. Actually depends on your design.

I created an example, maybe the worse example that I could, but I think it will make you clarify your mind. I just tried to follow your code.

In this example we use Strategy design pattern and Inversion of Control . You can see that Animal doesn't know nothing about Food implementation? Think about it, Animal.eat() can run multiple implementations without changing the eat() method. It is a little bit what the OOP can do.

public class Main {
    public static void main(String ... args){
        Animal paul = new Dog("Paul");
        Animal elsa = new Cat("Elsa");

        paul.eat();
        elsa.eat();
    }

    public static abstract class Animal {
        private String name;
        protected Food foodType;

        public Animal(String name){
            this.name = name;
        }

        public void eat() {
            System.out.println("The " + name + " has eaten " + foodType.getAmount() + " " + foodType.foodName());
        }
    }

    public static class Dog extends Animal {
        public Dog(String name) {
            super(name);
            foodType = new DogFood();
        }
    }

    public static class Cat extends Animal {
        public Cat(String name) {
            super(name);
            foodType = new CatFood();
        }
    }

    public interface Food {
        int getAmount();
        String foodName();
    }

    public static class DogFood implements Food{
        @Override
        public int getAmount() {
            return 2;
        }

        @Override
        public String foodName() {
            return "beef";
        }
    }

    public static class CatFood implements Food{
        @Override
        public int getAmount() {
            return 5;
        }

        @Override
        public String foodName() {
            return "fish";
        }
    }
}

advantage:

For ex.
If there is DogFood class extends Food, Dog can instantiate it (if Dog knows about DogFood) and Animal does not need to know about DogFood.

This simplifies codes.

As food type is an Animal property, and always the same for a specific child class, make it final in Animal, and pass it in via the Animal constructor.

public class Animal {
    public final Food foodType;

    public Animal(Food foodType) {
        this.foodType = foodType;
    }
}

public class Dog extends Animal{ 
    public Dog() {
       super(new DogFood());
    }
}

Now every Animal (child) has a foodType, and one does not have to fill the field of a parent class. The responsibilities are where they should be.

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