1

I am making a website where Doctors and Patient both can login.

class Doctors {
    String firstName
    String lastName
    String email
    String password
    String hospitalName
    String NPINumber
}

And patients

class Patients{
    String firstName
    String lastName
    String email
    String password
}

As it is evident that there are lot of overlapping fields which are only used for authetication/login purpose, I was wondering if inheritance is a good idea or should I just create a flag in a single class.

So two options are:

OPTION-1

class Users{
    String firstName
    String lastName
    String email
    String password
}

class Doctors extends Users {
    String hospitalName
    String NPINumber
}


class Patients extends Users{
}

OPTION-2

class Users{
    String firstName
    String lastName
    String email
    String password
    boolean isDoctor
    String NPINumber
    String hospitalName
}

I am not sure which of these designs I should choose so that it is extendable in future!

Tiago Farias
  • 3,397
  • 1
  • 27
  • 30
Sap
  • 5,197
  • 8
  • 59
  • 101
  • 1
    Personally I prefer to extend the class User. Even if you add more data in the future, that aproach will do the work and will be centralized which is more readable, maintanable and a good practice. – reixa Aug 27 '12 at 08:57

1 Answers1

0

Option 1 is more OO for sure. However, I would still make a little bit of a change in order to make the design even better. Instead of extending User, why not have a User property which contains some of the common attributes. Extending the User class could make your design more complex. Maybe this topic can help you.

Also, as a suggestion, don't use the classes' names in the plural form. Imagine it's an entity for itself and not a collection of entities. Clearly your class Doctor, for example, represents one doctor specifically. Not more than one. So you should use Doctor, Patient and User. Hope it helps.

Community
  • 1
  • 1
Tiago Farias
  • 3,397
  • 1
  • 27
  • 30