user5657755
user5657755

Reputation:

Class Diagrams relationships Interfaces

Say we have a class for exams and a class for course work, and both of these require the same attributes such as the courseword/exam id, name, deadline, weight etc.

Would it be wise to create an interface so both would implement the same methods such as what i've done below? [enter image description here][ trying to look for the best OO approach, just not sure whether i'm going a little overkill.

Thank you

Upvotes: 1

Views: 333

Answers (1)

Jessica
Jessica

Reputation: 119

I think the best approach is to create a class Class from whom ClassWork and ClassExam inherit.

The father class ClassF would declare those general attributes and the other 2 subclasses only will have to declare those specific attributes that make them unique 'cause the general ones are implied by inheritance.

I'll try to explain with an example:

       ClassF -> attributes: name, ID, weight, Date
              -> methods: getName() ....

             Exam -> attributes: marks 
                  -> methods: calculateMarks()...

             CourseWork -> attributes: typeOfCourse
                        -> methods: printInfoCourse()...

The subclasses Exam and CourseWork could implement and use for example, ID from ClassF because it's inherited from its father class.

Maybe it is a good idea to decide if you want to allow the instantiation of ClassF or not, because in that case it would be interesting to transform ClassF into an abstract class..


Just for the record in this concrete case the use of an interface to implement those classes is a wrong approach because that is not the function of an interface (https://docs.oracle.com/javase/tutorial/java/concepts/interface.html for more info)

Hope it helps!

Upvotes: 1

Related Questions