Aggiunta di diverse entità ereditate da una classe astratta - Java Spring
2 users upvote it!
2 answers

In Java Spring, different entities can inherit from an abstract class by using class inheritance. Abstract classes in Java Spring are used to define common properties and methods for a group of child classes that inherit from them.
To add entities that inherit from an abstract class in Java Spring, follow these steps:
1. Create an abstract class: First, create an abstract class with methods and properties that will be inherited by the child class. java public abstract class EncjaAbstrakcyjna { private int id; private String nazwa; public abstract void metodaAbstrakcyjna(); // inne metody i właściwości }
2. Create an inheriting class: Next, create a class that inherits from the abstract class. This class should extend the abstract class and implement all its abstract methods. java public class Encja extends EncjaAbstrakcyjna { @Override public void metodaAbstrakcyjna() { // implementacja metody abstrakcyjnej } // inne metody i właściwości }
3. Use of the entity: The finished entity can now be used to store data in the database or to perform any other operations. java Encja encja = new Encja(); encja.setId(1); encja.setNazwa("Nazwa encji"); encja.metodaAbstrakcyjna(); // wykorzystanie encji
By inheriting from an abstract class in Java Spring, you can avoid code repetition and create a hierarchy of classes that share common properties and methods. This is at the same time more flexible and easier to maintain.
In Java Spring, different entities can inherit from an abstract class by using class inheritance. Abstract classes in Java Spring are used to define common properties and methods for a group of child classes that inherit from them.
To add entities that inherit from an abstract class in Java Spring, follow these steps:
1. Create an abstract class: First, create an abstract class with methods and properties that will be inherited by the child class. java public abstract class EncjaAbstrakcyjna { private int id; private String nazwa; public abstract void metodaAbstrakcyjna(); // inne metody i właściwości }
2. Create an inheriting class: Next, create a class that inherits from the abstract class. This class should extend the abstract class and implement all its abstract methods. java public class Encja extends EncjaAbstrakcyjna { @Override public void metodaAbstrakcyjna() { // implementacja metody abstrakcyjnej } // inne metody i właściwości }
3. Use of the entity: The finished entity can now be used to store data in the database or to perform any other operations. java Encja encja = new Encja(); encja.setId(1); encja.setNazwa("Nazwa encji"); encja.metodaAbstrakcyjna(); // wykorzystanie encji
By inheriting from an abstract class in Java Spring, you can avoid code repetition and create a hierarchy of classes that share common properties and methods. This is at the same time more flexible and easier to maintain.
Machine translated
Polymorphism can be used to create the PersonService and PersonController classes so that they behave flexibly for each class that inherits from the abstract Person class. A generic mechanism can be used for this purpose.
Here is an example of implementing the PersonService class in Java:
javaCopy code
public class PersonService { private List persons = new ArrayList(); public void addPerson(T person) { persons.add(person); } public List getPersons() { return persons; } }
The above implementation uses a generic type T that inherits from the Person class. The class defines a list of objects of type T and two methods that allow you to add an object of type T to the list and retrieve a list of all objects of type T .
Next, you can create a PersonController class that will use the PersonService class to handle HTTP requests. Here is an example of implementing the PersonController class:
javaCopy code
@RestController public class PersonController { private PersonService personService; @Autowired public PersonController(PersonService personService) { this.personService = personService; } @PostMapping("/persons") public ResponseEntity addPerson(@RequestBody Person person) { personService.addPerson(person); return ResponseEntity.ok().build(); } @GetMapping("/persons") public ResponseEntity getPersons() { List persons = personService.getPersons(); return ResponseEntity.ok(persons); } }
The PersonController class uses the @RestController annotation, which means that the class will handle HTTP requests. The class constructor takes a PersonService** object, which allows you to pass any type that inherits from the **Person class .
Two endpoints are defined in the class - one for adding new Person objects and the other for getting a list of all Person objects. In the case of adding a new Person object, the @PostMapping annotation and the addPerson method were used, which adds the new object to the list in the PersonService class. In the case of retrieving a list of objects, the @GetMapping annotation and the getPersons method were used, which retrieves a list of all objects from the PersonService class.
Thanks to the generic mechanism, the PersonService and PersonController classes will work flexibly for any class that inherits from the Person class, without the need to add new methods or endpoints for each of these classes.
Polymorphism can be used to create the PersonService and PersonController classes so that they behave flexibly for each class that inherits from the abstract Person class. A generic mechanism can be used for this purpose.
Here is an example of implementing the PersonService class in Java:
javaCopy code
public class PersonService { private List persons = new ArrayList(); public void addPerson(T person) { persons.add(person); } public List getPersons() { return persons; } }
The above implementation uses a generic type T that inherits from the Person class. The class defines a list of objects of type T and two methods that allow you to add an object of type T to the list and retrieve a list of all objects of type T .
Next, you can create a PersonController class that will use the PersonService class to handle HTTP requests. Here is an example of implementing the PersonController class:
javaCopy code
@RestController public class PersonController { private PersonService personService; @Autowired public PersonController(PersonService personService) { this.personService = personService; } @PostMapping("/persons") public ResponseEntity addPerson(@RequestBody Person person) { personService.addPerson(person); return ResponseEntity.ok().build(); } @GetMapping("/persons") public ResponseEntity getPersons() { List persons = personService.getPersons(); return ResponseEntity.ok(persons); } }
The PersonController class uses the @RestController annotation, which means that the class will handle HTTP requests. The class constructor takes a PersonService** object, which allows you to pass any type that inherits from the **Person class .
Two endpoints are defined in the class - one for adding new Person objects and the other for getting a list of all Person objects. In the case of adding a new Person object, the @PostMapping annotation and the addPerson method were used, which adds the new object to the list in the PersonService class. In the case of retrieving a list of objects, the @GetMapping annotation and the getPersons method were used, which retrieves a list of all objects from the PersonService class.
Thanks to the generic mechanism, the PersonService and PersonController classes will work flexibly for any class that inherits from the Person class, without the need to add new methods or endpoints for each of these classes.
Machine translated