1

I have classes which are dependent on an interface which defines methods CREATE , READER , UPDATE and DELETE

However some of my implementation do not have option for CREATE

I believe it is not best practice to force those classes to implement CREATE method .

How can I best implement my interface so that it is not mandatory to implement CREATE in some classes .

yantrakaar
  • 374
  • 3
  • 15
  • 6
    Maybe you can create several interfaces `C` `R` `U` and `D`. Then CRUD would extend those 4 interfaces. It is also acceptable to implement the method and throw a `UnsupportedOperationException`. – Arnaud Denoyelle Feb 02 '15 at 14:19
  • I used UnsupportedOperationException as only one of the implementation did not have create method . Thanks . – yantrakaar Feb 04 '15 at 17:12

1 Answers1

2

the only possible way I think is having another class (ClassA) which have CREATE method but not implements the interface now the target class (TClass) which implements the interface(CURDInterface) can have not the CREATE method if extends from ClassA:

interface CRUDInterface{     
  public void Create();
  public SomeType Read();
  public void Update();
  public void Delete();
} 

class ClassA{
  public void Create() {
    //do something;
  }
}

class TClass extends ClassA implements CRUDInterface{
  public SomeType Read() {
    //do something;
  }
  public void Update() {
    //do something;
  }
  public void Delete() {
    //do something;
  }
}
void
  • 7,760
  • 3
  • 25
  • 43