![]() It so happens that this abstract class also implements a well known PSR-3 interface. Here is an example from PayPal repository where a class extends an abstract class:Ĭlass PayPalLogger extends AbstractLogger ![]() By "North" I mean responding to property/method calls from classes who (in your case) did not define method addArtikel. The job of an abstract class is to always point "North". That's a constraint and constraints help programmers think by narrowing down the choices they can make. They nod their head and go to next question. First you need to extend it and it's a job of a regular class. When I'm asked in job interview what's an abstract class and how it differs from regular class I reply: You can't create an object from an abstract class. Typical design patterns for this have classes called DAO (Data Access Object) or Repository. They should be part of a class that models what the items are added to or removed from. However, the real design problem here is that addArtikel() and removeArtikel() should not be in the Artikel class at all because they have nothing to do with the data model itself. How best to avoid the code duplication is hard to say without seeing that code, but part of it could be to make addArtikel() and removeArtikel() in the superclass not abstract, but have them deal with the common properties, so that the overriding implementations in the subclasses can reuse this logic by calling super.addArtikel() and removeArtikel(). And the implementation probably have a lot of duplicated code for the common properties like name and publisher. I'm guessing that you're talking about the addArtikel() and removeArtikel(), and it's not clear what exactly those are doing, but I'm guessing it has something to do with a database or, for a learning project, just a list or hash table in memory. I noticed that in all subclasses i basically needed to do the exact same thing, with a few atributes changes. It's not completely clear what you mean with this: ![]() Your use of an abstract base class in this case is perfectly correct. I think your problem has nothing to do with abstract classes or interfaces. with method bodies) for each method that you are now copy/pasting between all of its subclasses. Keep Artikel abstract, but give it non-abstract methods (i.e. Is it a bad design choice if I keep Artikel as abstract? the Book class to rely on the methods found in SeparateClass? If this SeparateClass was created as another (4th) subclass from Artikel, how would you expect e.g. And the same for all other derived classes.ĭon't take this the wrong way, but your attempts at solving this suggest you don't really master OOP. Then you will be required to implement this method separately in every class: public class Book : IArtikel If you were to use an interface: public interface IArtikel Interfaces prevent the ability to create a common method body. I thought of making Artikel not abstract and put an interface between Artikel and the other classes Its body has been declared in the base class and can be used by all of the derived classes. When you derive Artikel into subclasses, you do not need to repeat the method body of the SayHello method. An abstract method is a method in an abstract class which must be implemented in the derived class.īut an abstract class can have non-abstract methods: public abstract class Artikel This is not the case.Īn abstract class is a class that cannot be instantiated directly (only its derivations can be instantiated). Now when i started programming I noticed that in all subclasses i basically needed to do the exact same thingīased on this, it seems like you think abstract classes are only allowed to declare abstract methods. Note: please forgive the C# syntax, but the principle of the answer is the same for Java and C#. How would you guys design something like this? Is it a bad design choice if I keep Artikel as abstract? It's also possible to make an seperate class which inherits from Artikel where I can put all the methods, but then there the methods would still be needed to made three times, one for each subclass right? I thought of making Artikel not abstract and put an interface between Artikel and the other classes, but then i would still be copying a lot of work. Now when i started programming I noticed that in all subclasses i basically needed to do the exact same thing, with a few atributes changes. Then I have for examaple my book class which extends from Artikel. I have an Artikel class which is abstract. Started making an class diagram to get me started and when that was finished started programming. I want to build an application where i can store my collection of books, records, boardgames and such. ![]() At school I am currently learning to program with Java. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |