Home > Mobile >  Java Inheritance, instanceof, casting - how to accept a children object throught parent object?
Java Inheritance, instanceof, casting - how to accept a children object throught parent object?

Time:12-10

I have three classes: (Parent) Animal, (Child) Bird & Monkey

Monkey has weight & two pet Animals, which can be another Monkey, or bird

Bird has weight & a name (weight, name)

Together, they form a TREE, where leaf nodes are Bird, and non-leaf nodes are Monkeys (SEE VISUAL)

//                                          Monkey, weight=40
//                                       /              \   
//                                      /                \                         
//                                     /                  \
//                                  Bird(5,"Big Bird")  Monkey,weight=25
//                                                        /           \
//                                                       /             \
//                                                      /               \
//                                                     /                 \
//                                       Bird(weight=7, name="BirdMan")  Bird(w=11, n="Stinky") 

While recursively traversing this tree to find a bird with a certain name, i need to check if the curent node is a Bird or Monkey

// psuedo-is code
String recursive(Animal root, String target){

if (root instanceof Bird && root.name == target) return root.name;

// else, its not a Bird, its a Monkey
else 
    Animal left = root.left;
    Animal right = root.right;

    if (recursive(left) == target) return target;
    if (recursive(right) == target) return target;

    return "not found";

}

When I try to do this, it says that

error: cannot find symbol [in Main.java]
        Animal left = root.left;

I want to use parent-child inheritance in this problem, but its not allowing me to access the properties of the child object, since im using the parent object declaration in the variable.

How can I solve this problem? I want to use inheritance, but I just can;t figure it out. Please help. I also have some smaller questions in the code below. If anyone can help clarify those, that would be very helpful.

// animal parent class
class Animal {
   int weight;
   public Animal (int weight) { 
       this.weight = weight;
   }
}

// child class Bird, has weight & name
class Bird extends Animal{
   int name;
   public Bird (int weight, String name) {
       // * Question 1*
       // btw, is this line super(w) necessary? 
       //is it because the constructor of bird & animal have different args? 
       // do i have to say this.weight = weight;? or is that implied from super(w)? whats the most efficient way of declaring the inheritance i'm trying to establish?
       super(w);
       this.weight = weight;
       this.name = name;
   }
}

// child class Monkey, has weight & two pets (can be Monkey, or Bird)
class Monkey extends Animal{
   // *Question 2* Since animal can be both Monkey or Bird, I used parent class to do this.
   // is there a better way to do this?
   // I tried
   Animal left;
   Animal right;
   public Monnkey(int weight, Animal left, Animal right) {
       super(w);
       this.weight = weight;
       this.left = left;
       this.right = right;
   }
}

CodePudding user response:

If you want to avoid casting, you can implement the search polymorphically on the Animal types:

class Animal {
    abstract Animal find(String name);
}

class Bird extends Animal {
    String name;

    @Override Animal find(String name) {
        if (this.name.equals(name)) return this;
        return null;
    }
}

class Monkey extends Animal {
    Animal left, right;

    @Override Animal find(String name) {
        Animal result = left.find(name);
        if (result == null) result = right.find(name);
        return result;
    }
}
  • Related