-3

Why is this Student class (a class which creates new student objects and assigns them unique ID's) better suited to be an abstract class rather than a concrete class? Each student object created is assigned it's own unique ID right, so why not just have the implementation for the graduate() method inside the class itself rather than implemented by an inherited class?

public abstract class Student {
    protected int id;
    private static int lastID = 0;
    protected String firstName;
    protected String familyName;

    public Student(String firstName, String familyName) {
        id = Student.nextID();
        this.firstName = firstName;
        this.familyName = familyName;
    }

    private static int nextID() {
        return ++lastID;
    }

    public String toString() {
        return firstName + " " + familyName;
    }

    // Generate string containing graduation information
    public abstract String graduate();
}
Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95
J.Phillips
  • 11
  • 2
  • are there any sub classes to `Student` what are they? how did they impenitent the abstract method? – Sharon Ben Asher May 22 '19 at 14:39
  • 4
    You can't judge this without a more broad view of the rest of the design. Are there various kind of students that still share part of the actual code? – Federico klez Culloca May 22 '19 at 14:39
  • Why are you assuming it is? This would depend on how it was supposed to be used. – lijat May 22 '19 at 14:39
  • I am not sure if I understand your question correctly, maybe provide some more information, why you ask this question. It depends on context. If you intend to create subclasses of Student which could also graduate but require a different implementation, then the abstract implementation is good. If performance is no issue and you are not sure if you need subclasses, making the function abstract and providing a default implementation may be the most versatile solution. – jwsc May 22 '19 at 14:41
  • Just wondering: is your question answered (maybe consider accepting then), or do you need further information? – GhostCat May 23 '19 at 07:23

2 Answers2

1

The point would be: if you have various sub classes of Student, each one coming with a distinct differentiating attribute, say "major class".

Thus: this model would only allow students with a distinct "major class", therefore it would be meaningful to prevent creating "raw" Student objects that lack the corresponding "attribute".

But you have to understand: the reasons that determine how exactly you build your object model (which classes are abstract, and which ones are not) are coming out of what you intend to model.

In other words: the person writing this abstract Student class deemed that doing it exactly like this would be the best fit to the underlying requirements. In a different context, Student might very will be not abstract.

GhostCat
  • 137,827
  • 25
  • 176
  • 248
0

Generally a student class is written as abstract because of the types of students that you will have. i.e, high school, college, etc.

Some students have information that others don't. For instance, a college student may have a dorm number where a high school student wouldn't. The abstract class allows you to have more flexibility and not lock in a singular definition for student.

rabowlen
  • 186
  • 1
  • 13