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.
Human
inherits from Animal
, so all its fields are implicitly "copied" from Animal
. Therefore, you don't need to redeclare flyable
again in Human
and Bird
.
But you did. This causes the new flyable
fields in the subclasses to hide the original field declared in Animal
. As a consequence, when you do:
flyable = new CantFly();
in Human
, you are assigning a value to the flyable
in Human
, not the flyable
in Animal
.
Then you did this:
Animal me = new Human();
me.fly();
fly
in Animal
uses the field flyable
that is declared in Animal
class, which has not been assigned yet (you only assigned the flyable
in Human
)! An NPE occurs as a result.
To fix this, simply remove all the flyable
fields in the subclasses of Animal
. This way there is only one flyable
field.
Note
I think this design a little strange. Human
can't fly, so it shouldn't really have a flyable
field. In fact, nothing should have a flyable
field. Among these 3 classes, only Bird
should implement Flyable
.