There is an abstract class:

public abstract class SomeClass { String description; public String getDescription() { return description; } } 

It has many subclasses, for example:

 public class SubClass extends SomeClass { public SubClass() { description = "description"; } } 

Each subclass must have a description field that will be initialized when the object is created.
Did I implement the logic correctly? The idea is that in an abstract class, the field must be declared with the private modifier in order not to break encapsulation.

UPD: As an option, you can make such an implementation, it looks more correct, but for some reason confuses me.

 public abstract class SomeClass { private String description; public void setDescription(String description) { this.description - description; } public String getDescription() { return description; } } public class SubClass extends SomeClass { public SubClass() { setDescription("description"); } } 
  • one
    it turns out that your every subclass will not have a description field, change it to protected, this also does not break encapsulation - jashka

2 answers 2

Why not implement through a constructor in an abstract class, all heirs will be required to call this constructor.

 public abstract class SomeClass { private String description; public SomeClass(String description){ this.description = description; } public String getDescription() { return description; } } public class SubClass extends SomeClass { public SubClass(String description){ super(description); } } public class SubClass2 extends SomeClass { public SubClass2(){ super("значение по умолчанию для данного класса"); } } 
  • If, for example, you need to set some default value in a subclass? - jisecayeyo
  • You can create a parameterless constructor that passes a literal constant to the parent constructor. - Mikhailov Valentine

Just create a field in an abstract class with the protected modifier. In general, a strange question, think about what you want to get in the end. For such cases, there are excellent examples from the standard library. For example, in the AbstractList class there is a protected field modCount , which is used to track changes to data in derived classes so that iterators work correctly in them. Why go far for examples, the standard library is full of excellent examples.

  • I agree with you, but the author of the question indicated that he must fulfill the following condition: "Each subclass must have a description field that will be initialized when the object is created." - Mikhailov Valentine
  • @MikhailovValentine, yes, I saw and perhaps did not quite flesh out my answer. It seemed to me that this question is not so much a specific task as an attempt to understand what abstract classes are and how to use them. I think such qualitative examples as in the standard library are the easiest to do. I can be mistaken, but it makes no sense to use abstract classes because the author of the question tried to use them, except in some very “exotic” cases. - diofloyk 2:58 pm