19 Object Oriented Design Interview Questions a Developer Should Know
If you plan to work in software development and engineering, expect to answer some tough questions related to the field. Adequate preparation is the best way to make tech interviews seem less nerve-wracking.
Many of the questions will deal with topics that you rarely experience on the job or in your college courses. There’s also a chance that you’ll have to answer those questions in front of multiple people who will judge your answers and your personality. You may have to show your work on a whiteboard.
The good news is that you can prepare for these interview questions. Although you can’t prepare for all the questions an interviewer can ask, studying before your interview will help you with some common questions.
Last Updated November 2021
Don’t just learn Java! Learn to write practical industry standard object oriented software and super charge your career! | By Imtiaz AhmadExplore Course
Refreshing your memory on a few object oriented programming (OOP) topics with some example questions before your interview will help you answer the basic interview questions. Knowing what the interviewers are looking for and using that to form a general approach to solutions will help when they ask you to create a custom solution to an interview question.
The importance of object oriented design and the interview process
Object oriented programming is the most powerful and effective way to write software! That’s not a small statement. Let’s break it down a bit so you can understand the gravity of this hugely important topic.
What is code? In simple terms, lines of code written by a programmer are instructions to the computer on what to do and how to do it. What an underwhelming idea, right? Wrong! It’s actually incredibly complicated and needs tremendous respect.
In a world without object oriented programming, there would be no chance for hundreds of developers to collaborate and build or extend applications or work on large, open-source projects together. Without object oriented programming, code written once would probably never be approachable again even by the same developer who wrote it unless they were willing to dedicate almost the same time relearning the application as it took to write it.
Before object oriented programming was invented, all code would go in large files containing thousands of lines of instructions with no organization, just a huge list of commands, and a mountain of complexity all intertwined like a bundle of yarn. Good luck untangling that!
Then came object oriented programming with languages like Simula, C++, Python, and Java, and now the most popular programming languages in the world are all object oriented. Thankfully, we now live in a much more enlightened and organized world!
Object oriented programming is a way to organize your code. It’s all about code organization, extensibility, reusability, and design. Most interviewers want to know whether a software developer applying for the job can take a complex, real-life problem and translate it into well-organized code using an object oriented approach to design.
We will get to the list of questions that have simple answers, but now that we have a better idea of what interviewers are looking for, let’s create an approach to solve the questions that involve designing a real object oriented solution.
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.
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.
19 popular object oriented interview design questions
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.
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.
15. What is encapsulation?
Encapsulation is an OOP concept that refers to bundling data with the methods that operate on that data in one package. Most object oriented languages use encapsulation in the form of classes.
Encapsulation can also refer to hiding the state of the object from the outside, or information hiding. By setting an attribute in an object private, the object can control access to that attribute with methods that have to be called to set or get that attribute.
16. What is polymorphism?
Polymorphism is an object oriented concept that refers to a class’ ability to take on different forms. Inheritance and overriding allow for polymorphism. If we create a class called Ferrari and a class called Yugo that inherit from a Car class, each will have a drive method that essentially does the same thing, though the results will be different.
17. What is composition?
Composition is an important concept in object oriented programming. It describes a class that references one or more other classes in its instance variables. An example of this would be a car class that uses an engine class and tire class. Composition allows for reuse. You can also use the engine class used by a car class in a generator class.
18. What are design patterns?
A design pattern refers to a repeatable solution that you can use to solve a commonly occurring software problem. It’s basically a blueprint that a developer can customize for their own needs. An example is the Model-View-Controller pattern that some use as a pattern for desktop and web applications, or the singleton pattern which ensures only a single object of a class can exist while the application is running.
19. What are the SOLID principles of object oriented programming?
SOLID is an acronym for five object oriented design principles that make software understandable, flexible, and maintainable. SOLID stands for:
• Single Responsibility: A class should have a single responsibility.
• Open/Closed: Software should be open for extension and closed for modification.
• Liskov Substitution: Objects in a program should be replaceable with instances of their subtypes without altering that program’s correctness.
• Interface Segregation: Many client-specific interfaces are better than one general-purpose interface.
• Dependency Inversion: A developer should depend on abstractions, not concretions.
I cannot overstate the importance of the SOLID principles listed above. I created an entire course dedicated to learning and practicing these principles. It’s one of my most popular and top-rated courses. I also have an excellent YouTube playlist for you to watch that explains the object oriented programming basics.
Reviewing the concepts of object oriented programming is an excellent start toward preparing yourself for a programming interview. If you are rusty, the questions above will refresh your memory, help you face the interview panel, and answer some of the basic questions they may ask.
Interviewers will likely ask you to find solutions to real-life problems by designing a custom application. When creating a solution, the important thing is not to panic or overthink. Realize the interviewers just want you to show that you know the answers to their questions and understand them enough to put them to use.
Top courses in Object Oriented Programming
Object Oriented Programming students also learn
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.