samedi 12 août 2017

Java Interface Design Pattern Situation

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.

Aucun commentaire:

Enregistrer un commentaire