I'm planning to implement a system with a super class named User and 2 subclasses of the class User, named Seller and Buyer. They are specific types of Users.
So, I have 3 files with 3 classes:
- User.php (class User)
- Seller.php (class Seller extends User)
- Buyer.php (class Buyer extends User)

When there is a session indicating that the user is logged I want to create the corresponding object user type (Seller or Buyer).
My first instinct was to instantiate the User class with the given session ID and then, inside, instantiate the corresponding subtype. But I believe this is extremely incorrect cause I'm trying to instantiate a subclass from the inside of a superclass.

What's the best approach to implement such a system?

4 Answers 4


Create a common interface when the user is logged in create an instance of Seller or Buyer and set the object.


interface IUser {
   name: string;
   id: number;
abstract class User extends IUser{
  // common functionality
class Seller implements User extends IUser{}
class Buyer implements User extends IUser{}

// login code
let user: IUser;
if (loggedinuser is seller) {
  this.user = new Seller({...data});
} else if (loggedinuser is buyer) {
  this.user = new Buyer({...data});

Prefer composition to inheritance.

Have a generic User class that has all the common attributes, like the login name, email, etc.

Have separate classes that describe roles of the user, e.g. Seller and Buyer (and maybe also Admin, etc).

Make a User object refer to one (or more) role object to describe the user's role.

This way, you can easily assign and revoke roles from users, add new roles, and have a generally finer-grained control over what a user is allowed (or not) to do. The code will also be cleaner, and you'll never have to check to which subclass a user belongs and whether a particular method is available.


Use a UserFactory class or a factory method inside the User class. That factory/method creates a Seller or a Buyer based on the information inside the session.

  • Wouldn't it be incorrect since I would be creating objects of the subclass type inside the superclass?
    – random425
    Commented Apr 7, 2017 at 13:07
  • 1
    It's explicitely described as an option in the GoF FactoryMethod pattern: "Parameterized factory methods. Another variation on the pattern lets the factory method create multiple kinds of products. The factory method takes a parameter that identifies the kind of object to create. All objects the factory method creates will share the Product interface." Gamma, Erich. Design Patterns: Elements of Reusable Object-Oriented Software (Adobe Reader) (Kindle Locations 2047-2049). Pearson Education. Kindle Edition.
    – Philippe
    Commented Apr 7, 2017 at 13:23

If you created Seller or Buyer inside the User class, then they would be different, unrelated objects and that is not usually what you want to do. You really want to create an instance of the subclass Seller or Buyer with the Id being defined as an element of the User class and accessible, if needed, by the subclasses. Let me know if you need further clarification.

  • Seller and Buyer are outside the User class. They are classes that extend the User class. I've updated the question.
    – random425
    Commented Apr 7, 2017 at 13:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.