Skip to main content

Q and A

How to come up with object oriented design solutions

The typical OOD question used to test your object oriented programming skills with a real solution will give you a vague problem like:

•      Design a game of blackjack

•      Design an URL shortener service

•      Design an ATM

•      Design a two-player chess game

•      Design a restaurant

•      Design a parking lot

•      Design an online stock brokerage

The problem may also have some constraints to give you a better idea of the solution the interviewer expects. For example, you could limit the blackjack game to a dealer and one player. Pay attention to these constraints because they will keep you from overthinking your solution and spending a lot of time building a solution that doesn’t fit the problem. I’ve authored a top-rated object oriented design course covering the SOLID principles of object oriented design that can help with this.

Even with some practical constraints, you will still have a lot of work ahead of you. To make this process easier, you can use these steps to narrow down the functionality you need to create:

1.    Ask questions. If you don’t completely understand the requirements, ask some questions. I actually encourage you to ask questions to clarify both the interviewer’s and your own assumptions about the problem, so you and the interviewer agree. This will not only show that you are a responsible problem solver but also detail-oriented in your approach to thinking about the requirements. 

2.    Don’t jump right into writing code. Describe the use cases. Describe to the interviewer how people will use your system. Draw workflows and diagrams, especially if there is a whiteboard or notepad available in the room. Make a list of use cases and say them out loud. Describing and organizing your thought track is actually a very good interviewing technique and shows confidence in your ability to break down problems. This will all help you better determine the functionality your system will need at the outset instead of halfway through your solution when change is harder to pull off. 

3.    Identify the objects and their relationships with each other at a high-level. For example, in the blackjack game, you could create the following objects: deck, card, player, dealer, and game. These objects have relationships with each other, and you should draw them out at a high-level to clarify how they would interact. 

4.    Describe each object’s attributes and behaviors. Get into more detail about each object’s characteristics/attributes and behavior. Behavior typically relates to methods that would be needed on the object to perform some kind of work. 

Object Oriented Programming students also learn

5. Organize relationships and class hierarchies. Here is where you need to get specific about object relationships and how to organize class hierarchies. For example, you may find that the Dealer class should inherit from the parent class Player. But a dealer object would have the specific deal() method or a shuffle() method, whereas the parent class of Player would not. 

6.    Look for design patterns. Once you know how your objects interact, you can now look for places to apply object oriented design patterns that fit your application’s needs. Don’t be quick about forcing a specific object oriented design pattern to fit with the application. Only apply the pattern if it makes sense. Newcomers often try to force design patterns into their code, and that can make the application more complex than necessary if the pattern does not fit naturally. Watch out for this! 

Remember, there is no “right” answer to one of these types of interview questions. The interviewer is simply trying to determine:

•      Can the candidate ask the right questions to gather requirements about the problem?

•      Can the candidate identify objects and how they interact from the problem?

•      Is the candidate comfortable with OOD principles and design patterns?

•      How does the candidate balance the need to have software that they can code quickly and software that can adjust to the system’s changes?

Don’t overthink or get nervous, but do make sure you fully understand the problem, and you should do fine. Now let’s look at some general questions a tech interviewer may ask you.

Here are some questions that interviewers may ask to test your basic knowledge of object oriented design principles.

1.

What is aa...

class?

  1. Class:

    • A class is a fundamental building block in object object-oriented programming.
    • Considered
    • It theserves as a blueprint for defining an objectobjects in your software, a class contains thecontaining properties and methodsmethods.
    • that make your objects function. One advantage of
    • OOP isallows the ability offor one class to inherit from another.

      another,

      2.facilitating Whatcode arereuse.

    • properties
  2. Properties and methodsMethods:

    of
      a class?

    • Properties are the “nouns”attributes (nouns) of your classes. They let the programmer define the attributes of the object. For instance, if you have a classclass, thatdefining definescharacteristics a house, the class properties would describe the house’slike color, size, andetc.
    • materials.

    • Methods are the “verbs”behaviors (verbs) of your class. Another way to think about it is that methods are behaviors your object carries out. For example, if you have a Bird class, fly() would be a method. This will allow bird objects to fly. Methods are named blocks of code that contain instructions that are made part of the class’ blueprint that define thedefining actions thelike objectsfly(), ofeat(), thatetc.
    • class
    must
  3. carry
  4. out.

    Inheritance:

    3.
      What is inheritance?

  5. Inheritance allows onea child class to use the same methods and properties of another class. A child class “inherits” its methods and properties from a parent class.

  6. Let’s

  7. Child sayclasses youinherit havebehavior anfrom Animalparent classes, facilitating code reuse and promoting a hierarchical structure.
  8. For example, a Bird class thatmay contains attributes like weight and size. It can containinherit methods like eat() orand sleep(). A Bird class would naturally inherit from thea parent Animal classclass, because a bird is an Animal. Becauseas birds are a kindtype of Animal,animal. they
      would
    • naturally

      Hierarchy inherit the behavior of eating and sleeping as defined in the parent Animal class. This is one example of code reuse. You should not need to redefine eat and sleep methods in the child class Bird if bird is an Animal and the Animal class already contains these methods. Building:

      Empower
        your team. Lead the industry.

       

      Get a subscription to a library of online courses and digital learning tools for your organization with Udemy Business.

       

    • Inheritance allows youfor tothe buildcreation of a hierarchy of classesclasses, tomirroring simulatereal-world therelationships.
    • way
    • It simulates how objects in the real world relate to each other through an IS A relationship.
    • This
    is
  9. an
  10. important

    Understanding pointIS thatA newcomersRelationship:

    miss.
    • The goal of inheritance is not just to just inherit methods.methods It’sbut abidingto byadhere to the IS A relationship rule!rule.
    • It ensures that subclasses represent specialized versions of their superclass and are related in a meaningful way.
  11. Avoiding Poor Design:

    • Inheritance should not be used indiscriminately for method reuse.
    • For example, both Vehicle and Animal classes could have the move() method defined, but a birdBird class should nevernot inherit thefrom a Vehicle class justsolely to use the move() method. That’sThis violates the IS A relationship principle and leads to poor design.
  12. Key Principle:

    • The IS A relationship is keycrucial for forming class hierarchies.hierarchies Aand well-designedensuring application allows for goodproper code reuse where it makes sense.

    • 4.
    • Well-designed Whatapplications isleverage theinheritance differenceto betweenpromote privatecode reuse in a meaningful and publiclogical manner.
  1. Public vs. Private Methods/Properties:

    • Public methods orand properties inare classes?accessible

      Publicfrom andexternal classes, while private define the accessibility of a class’ properties or methods.

      Private methods and properties are only accessed by that enclosing class itself. No external classes have access to private members of a class. If there is an inner nested class, that’s enclosedaccessible within the class containingitself.

    • private members. Then that inner nested class has access to those private members because it is still enclosed within that same class definition. Remember that private
    • Private members cannot be inherited by child classes!classes.
    • Public
    • Inner methodsnested andclasses propertieswithin arethe availablesame class definition have access to otherprivate classesmembers.
    • to
    use.
  2. Class Constructor:

    5.
      What is a class constructor?

  3. A constructor is thea method used when youto instantiate an instance of a class.
  4. In
  5. It mosttypically object oriented languages, the class constructor will havehas the same name as the class.class Inand manyinitializes languages,specific youproperties.
  6. can
  7. Overloaded haveconstructors moreallow thanfor onemultiple constructor per class. You can have several overloaded constructors or a constructordefinitions with nodifferent parameters.
  8. Constructors are used
  9. to

    Overloaded initialize specific properties in your class.Methods:

    6.
      What are overloaded methods?

  10. Overloaded methods usehave the same method name but a different set of parameters.
  11. The methods can return different data because the return type can be different. What’s important to know is that the data types of the parameter list must not repeat; otherwise, there will be a compilation error. You may get asked whether order matters in the parameter list and the answer is YES.
  12. The order of theparameters matters, and parameters must have different data types doesto matter.avoid Ifcompilation twoerrors.
  13. methods with the
  14. same

    Abstract name have the same parameter list data types, but the parameter order is different, that is legal code, and it will compile just fine in Java. Class:

    7.
      What is an abstract class?

  15. An abstract class cannot be instantiated,instantiated but it can be inherited.
  16. This
  17. It means that your inherited class can inherit methods from the parent abstract class, but it cannot directly call the abstract class. Abstract classes are excellent for defining global definitions for your inherited classes, so you only need to create these definitions once. The child class is expected to implement thecontains abstract methods definedthat in the parent abstract class. As a follow-up, you will probablymust be askedimplemented aboutby abstractits methods.child Anclasses.
  18. abstract
  19. Abstract methodmethods is a method that isare declared without an implementation (without braces and followed by a semicolon).  If a class includes abstract methods, then the class itself must be declaredimplemented abstract.by subclasses.
  20. Instantiation:

    8.
      What is instantiation?

  21. Instantiation isrefers whatto happensthe whencreation of an instance (object) of a classclass.
  22. is
  23. It created.occurs An instance is another term for an object of that class. Callingwhen the class constructor method instantiatesis thecalled, object. You cannot use a class before you instantiate it. When instantiated,providing the object has thewith properties and methods defined in itsthe class’class blueprint.
  24. Passing Parameters:

    9.
      What is the difference between passing
    • Passing parameters by value andallows passing them by reference?

    When parameters passmethods to aaccess method by value,only the method only has access to the parameter’parameter's value andvalue, not the variable useditself.

  25. as
  26. Passing parameters by reference passes a pointer to the parameter.variable, Ifallowing thismethods valueto is changed when the method executes, it does not affectmodify the original variable.
  27. Method Overriding:

    When

      parameters
    • Method passoverriding byoccurs reference,when a pointersubclass actually passes into the method. The pointer references the variable used as the parameter. If the method then modifies this parameter, it will alter the original variable as well.

      10. What does it mean to override a method?

      When you create a class that inherits from another class, you can override the parent class’ methods. For example, if the parent Animal class contains the method move(), a child class such as Bird can either inherit the move() functionality from the parent or override it to something more specific to birds. If overriding is done, then the bird will haveprovides its own implementation for the move()a method ininherited itsfrom owna classsuperclass.

    • rather
    • It thanallows inheritingfor customizing the parentbehavior classof Animal’sinherited genericmethods movein behavior.child classes.
  28. Exception Handling:

    11.
      What is exception handling?

  29. Exception handling is a process used to trap thehandle errors that can occur whenduring yourprogram applicationexecution.
  30. is running.
  31. It allows usersfor tograceful continueerror usinghandling, the software and gracefully handle issues rather than lettingpreventing the application crash.from Forcrashing example,and yourproviding browserusers applicationwith doesn’tfeedback crashon if you lose connectivityhow to theproceed.
  32. internet. It just
  33. says

    "this" “retry connecting” or something similar. This is an example of exception handling done correctly in the software. Exception handling is done to capture error scenarios that are outside of the developer’s control.Object:

    12.
      What is the “this” object?

  34. The “this”"this" reference refers to the current instance of the object.object Youwithin typicallya useclass.
  35. “this”
  36. It is used to referenceaccess aninstance internalvariables propertyand ormethods method ofwithin the class.
  37. You will see
  38. “this”

    Pointer:

    in
      many
    • Pointers programming languages. Python uses “self” instead of “this,” butreference the concept is the same.

      13. What is a pointer?

      Pointers are typically used by name in C++, but they also apply in other programming languages. Pointers “point” to the actual memory location of a value. Because a pointer references the actual value, when you change a value in aprogramming pointer,languages youlike changeC++.

    • They allow for direct manipulation of memory values, affecting the valueoriginal indata.
    • memory.
  39. Static Methods:

    14.
      What are static methods?

    The static keyword defines

  40. Static methods that will exist independently of anyclass instances created from the class. Static methodsand do not have access to any of the instance variablesvariables.
  41. of the class. Static functions
  42. They are beneficialuseful whenfor youdefining need quick execution ofutility functions that don’tdo neednot otherrequire partsaccess ofto theinstance-specific containingdata.
  43. class.