0

I want all the Service classes in my backend to have CRUD methods.

For that purpose, I thought of creating an interface:

public interface ServiceCRUD {

    public Object save(Object object);
...
}

And then on my service, implement it:

@Service
public class SampleService implements ServiceCRUD {
    @Autowired
    private SampleRepository repository;

    @Override
    public Sample save(Sample sample) {
        return repository.save(sample);
    }

...
}

I haven't touched Java in a while, but if I recall correctly, every object extend Object, so why is it that I can't use Object to have the service accept all the entities I might have?

Best regards

Luis Fernandez
  • 539
  • 1
  • 4
  • 24
  • Every class extends Object. That doesn't mean List extends List – Michael Mar 25 '21 at 12:55
  • I have the same problem on the method without the list – Luis Fernandez Mar 25 '21 at 12:57
  • 1
    Your interface says "I am a thing which can accept any Object", your specific implementation says "I am a thing which accepts only Samples". So your implementation does not adhere to the interface – Michael Mar 25 '21 at 12:58
  • Suppose I have `ServiceCRUD service = new SampleService(...);` I should be able to call `.save("hello world")`. That's what the interface says. The actual implementation does not support that – Michael Mar 25 '21 at 12:59
  • Ok, I see the problem. So, how can I solve it? – Luis Fernandez Mar 25 '21 at 13:00
  • This is what generics are for. `interface ServiceCRUD`, `T save(T object);` and `SampleService implements ServiceCRUD` – Michael Mar 25 '21 at 13:06
  • Are you thinking about generics @LuisFernandez – Shubham Dixit Mar 25 '21 at 13:06
  • But also, Spring already declares the [`Repository` interface (and `CrudInterface` etc.)](https://docs.spring.io/spring-data/data-commons/docs/1.6.1.RELEASE/reference/html/repositories.html) which is basically for an identical purpose. I would start by making sure that you don't want to use that. – Michael Mar 25 '21 at 13:08

2 Answers2

0

You can achieve such scenario using Generics

interface ServiceCRUD {

    public < E > void save(E object);
}


class Sample {
    private String name = "Joe";

   
   @Override
    public String toString() {
        return "hello"+name;
    }
}

class SampleService implements ServiceCRUD {


    @Override
    public < Sample > void save(Sample sample) {
        System.out.print(sample.toString());
    }


}



public class Main {
    public static void main(String[] args) {
        new SampleService().save(new Sample());

    }
}

This is just an example ,you can extend it as per your use case. See working here

Shubham Dixit
  • 9,242
  • 4
  • 27
  • 46
0

Your interface declares that one is possible to save Object, i. e. any object. But your implementation declares that it cat get only Sample, that's why you get compilation error. You should go with genetic and let each implementation to declare what kind of object it can deal with. I strongly suggest to have a look at spring data project. It will save you a lot if time

Alexander.Furer
  • 1,817
  • 1
  • 16
  • 24