Win a copy of Spring Boot in Practice this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

Designing with Interface or Abstract class

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, i am preparing for SCJP 5. I need advice regarding using interface and abstract in a particular scnario. Suppose there are three entities teacher, student, and admin. An teacher CAN be admin (giving out usernames and password) OR an admin CAN BE student (taking some course). How can we design this using interface and abstract classes:

1) I can have an abstract class Person and both student and teacher extending it. But then how do i bring in admin role. If i make admin an interface and make teacher and student classes implement this interface, both student and teacher classes will have to provide implementation.

2) If i have interface admin as a parent and abstract class person implementing the interface that does not much sense because it is not necessary that both student and teacher will be admin.

3) The last option i can think of is simply using composition and make a separate class admin whose reference will be in student and teacher class. It can be set to null if they dont have this role. But i am not sure if this is a good design? In this example we do not need to store information regarding admin so i dont think we need a concrete class for admin.


Please let me know of any other solution. Thanks
 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Nomi,

If you are sure, you would not need to store any information related to Admin, you can simply add a admin flag in the Person class

A boolean variable with getters and setters should be sufficient



The access modifiers can be changed to suit the requirements of the application
 
reply
    Bookmark Topic Watch Topic
  • New Topic