jeudi 3 mars 2016

Java Reflection on Interfaces

mainly my question is about how I could create a "dynamic" method which I could Override for different objects.

So having this interface

public interface GenericInterface{

    public List<Object> getAll() throws Exception;

    public Object addObject(Object object) throws Exception; // This should use reflection

}

And now extending GenericInterface in this other Interface

public interface CarInterface extends GenericInterface{

     @Override
     List<Object> getAll() throws Exception;

     @Override
     public Object addObject(Object object) throws Exception;


     public Object newSpecificMethod() throws Exception;

}

And now having a public class

pulic Car implements CarInterface{
     //...

     @Override
     public Object addObject(Object object) throws 

          //.. here some logic

          return object;
     }

     //...
}

So how could I "Override" the method's name?

Instead of having

Car.addObject(object);

I would like to have

Car.addCar((Car) object);

I know that is related with Reflection but I don't know how I could do it. Any suggestion?





Aucun commentaire:

Enregistrer un commentaire