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 a class?

A class is a fundamental building block in object oriented programming. Considered the blueprint for defining an object in your software, a class contains the properties and methods that make your objects function. One advantage of OOP is the ability of one class to inherit from another.

2. What are properties and methods of a class?

Properties are the “nouns” of your classes. They let the programmer define the attributes of the object. For instance, if you have a class that defines a house, the class properties would describe the house’s color, size, and materials.

Methods are the “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 the actions the objects of that class must carry out.

3. What is inheritance?

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

Let’s say you have an Animal class that contains attributes like weight and size. It can contain methods like eat() or sleep(). A Bird class would naturally inherit from the parent Animal class because a bird is an Animal. Because birds are a kind of Animal, they would naturally 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. 

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 you to build a hierarchy of classes to simulate the way objects in the real world relate to each other through an IS A relationship. This is an important point that newcomers miss. The goal of inheritance is not to just inherit methods. It’s abiding by the IS A relationship rule! For example, both Vehicle and Animal classes could have the move() method defined, but a bird should never inherit the Vehicle class just to use the move() method. That’s poor design. IS A relationship is key for forming class hierarchies. A well-designed application allows for good code reuse where it makes sense.

4. What is the difference between private and public methods or properties in classes?

Public and 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 enclosed within the class containing 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 members cannot be inherited by child classes! Public methods and properties are available to other classes to use.

5. What is a class constructor?

A constructor is the method used when you instantiate an instance of a class. In most object oriented languages, the class constructor will have the same name as the class. In many languages, you can have more than one constructor per class. You can have several overloaded constructors or a constructor with no parameters. Constructors are used to initialize specific properties in your class.

6. What are overloaded methods?

Overloaded methods use the same method name but a different set of parameters. 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. The order of the parameters data types does matter. If two methods with the same 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. 

7. What is an abstract class?

An abstract class cannot be instantiated, but it can be inherited. This 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 the abstract methods defined in the parent abstract class. As a follow-up, you will probably be asked about abstract methods. An abstract method is a method that is declared without an implementation (without braces and followed by a semicolon).  If a class includes abstract methods, then the class itself must be declared abstract. 

8. What is instantiation?

Instantiation is what happens when an instance of a class is created. An instance is another term for an object of that class. Calling the class constructor method instantiates the object. You cannot use a class before you instantiate it. When instantiated, the object has the properties and methods defined in its class’ blueprint.

9. What is the difference between passing parameters by value and passing them by reference?

When parameters pass to a method by value, the method only has access to the parameter’s value and not the variable used as the parameter. If this value is changed when the method executes, it does not affect the original variable.

When parameters pass by reference, a pointer 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 have its own implementation for the move() method in its own class rather than inheriting the parent class Animal’s generic move behavior. 

11. What is exception handling?

Exception handling is a process used to trap the errors that can occur when your application is running. It allows users to continue using the software and gracefully handle issues rather than letting the application crash. For example, your browser application doesn’t crash if you lose connectivity to the internet. It just says “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.

12. What is the “this” object?

The “this” reference refers to the current instance of the object. You typically use “this” to reference an internal property or method of the class. You will see “this” in many programming languages. Python uses “self” instead of “this,” but 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 a pointer, you change the value in memory.

14. What are static methods?

The static keyword defines methods that will exist independently of any instances created from the class. Static methods do not have access to any of the instance variables of the class. Static functions are beneficial when you need quick execution of functions that don’t need other parts of the containing class.