I am stuck at a problem. My Problem goes like this. I have a superclass Animal and two subclasses Human And Bird. I have a fly method in my super class Animal which will provide an implementation for both the class Human and Bird based on the Flyable Interface.
My Animal class looks like this.
public class`Animal{
public Flyable flyable;
public void fly()
{
flyable.fly();
}
}
Human Class looks like this
class Human extends Animal {
Flyable flyable;
public Human()
{
flyable = new CantFly();
}
}
Bird class looks like this
class Bird extends Animal {
Flyable flyable;
public Bird()
{
flyable = new FlyHigh();
}
}
Interfaces are below
public interface Flyable {
public void fly();
}
public class CantFly implements Flyable{
@Override
public void fly()
{
Sysout("cant fly");
}
When I call
Animal me = new Human();
me.fly();
It gives me NullPointerException
What I m missing here ?.
I was assuming. Since I'm calling new Human() it initializes the Flyable interface in superclass Animal. Am I wrong?
I have solved this problem by changing the design of the method.
fly(Flyable flyable).So I don't want that design solution.
I remember facing this problem when I was implementing a search algorithm for an app which will provide a list of result but list item will have different
UI,
https call to different End URLs,
Parsing JSON,
Mapping to Different POJO classes.
Sadly I had to solve that problem with an alternate approach which I have mentioned.
Humaninherits fromAnimal, so all its fields are implicitly "copied" fromAnimal. Therefore, you don't need to redeclareflyableagain inHumanandBird.But you did. This causes the new
flyablefields in the subclasses to hide the original field declared inAnimal. As a consequence, when you do:in
Human, you are assigning a value to theflyableinHuman, not theflyableinAnimal.Then you did this:
flyinAnimaluses the fieldflyablethat is declared inAnimalclass, which has not been assigned yet (you only assigned theflyableinHuman)! An NPE occurs as a result.To fix this, simply remove all the
flyablefields in the subclasses ofAnimal. This way there is only oneflyablefield.Note
I think this design a little strange.
Humancan't fly, so it shouldn't really have aflyablefield. In fact, nothing should have aflyablefield. Among these 3 classes, onlyBirdshould implementFlyable.