This blog post lists some critical questions and answers about Object-oriented programming. We also have another blog post (A summary of modern C++ features) that contains related information to this topic. To avoid content duplication, we will not cover in this blog post the ideas that have already been in the other one. Thus, we recommend readers consult both the blog posts for a complete view of the current topic.
Briefly describe OOP
OOP (Object-Oriented Programming) is the programming paradigm to represent things using objects. Objects are instances of classes. Objects have their own characteristics (i.e. data, or attributes) and behaviors (i.e. functions, or methods).
Name the most popular programming paradigms
They are:
- Imperative programming: in which we focus on how the program achieves the desired result. This is in contrast with Declarative programming where we specify what the result is and let the computer handle the rest. Python and C++ are examples of imperative languages, while SQL and Prolog are declarative.
- Object-oriented programming: as described above, is revolved around objects.
- Procedural programming: in which there is no classes or objects, but only procedures (i.e. functions) and statements to be executed.
- Functional programming: focus on writing mathematical functions instead of statements.
Describe the main features of OOP
The main features of OOP are Abstraction, Encapsulation, Inheritance, and Polymorphism.
Abstraction: the end-users don’t need to know all the details about how data is stored and things are executed to achieve the result. They only need to know how to interact with the devices or objects to get the desired result. Abstraction is achieved through Encapsulation.
Encapsulation: is a method to bind data and functions together. Everything needed to do the job is encapsulated into a box and that box is presented to the users. Encapsulation embraces data hiding (detailed data is hidden from users) and data binding, resulting in Abstraction.
Inheritance: A class can inherit attributes and functions from its parent classes. For example, class Humans is a sub-class of Animal, which means Humans have every trait of an Animal. Inheritance makes the program simpler and facilitates code reuse.
Polymorphism: literally means many forms. This means some pieces of code can behave differently in different contexts. There are 2 types of polymorphism: Compile-time polymorphism (overloading) and Run-time polymorphism (overriding of virtual functions in C++).
Do classes take up memory?
Classes are blueprints to create objects, they are not objects so they do not occupy variable memory. However, at least the code of the classes needs to be stored in the compiler space so that it can be executed when the users call.
What are the limitations of Inheritance?
While Inheritance has advantages, it also comes along with limitations:
- The base and derived classes are tightly coupled. Thus, changes to one class (especially the parent class) may also undesirably affect another class.
- Multiple Inheritance (one child class is derived from multiple parent classes) and Hierarchical Inheritance (many child classes from one parent class) can make the code flow complicated.
- The code is a bit slower as the compiler needs to navigate through the hierarchy of classes.
- For small projects, OOP is redundant and might lead to longer and more complex code than needed.
Is it always necessary to instantiate objects from classes?
No, not always. Some examples are:
- Virtual base classes, which are just base classes so that other classes can inherit from them. It is strictly forbidden to create objects from virtual classes.
- MixIn classes are also for others to inherit.
- Static methods of a class can be called without any objects.
Which function cannot be overloaded?
Static member functions.
List the types of Inheritance
- Single Inheritance.
- Multiple Inheritance.
- Hierarchical Inheritance.
- Multilevel Inheritance.
- Hybrid Inheritance.
Is it possible to override with a different return type?
Yes, but the overriding function’s return type needs to be narrower than the original type. In particular:
- If the original return type is void, the overriding return type is void.
- If the original return type is a primitive type, the overriding return type is the same as the original one.
- Otherwise, the return type is either the same or a subclass of the original return type.
Compare Exceptions and Errors
Errors are problems that should not happens to the program. If an error happens, we should modify the code to fix it.
Exceptions are problems that are to be caught by the program. Even if an exception is raised, we might not want to modify the code to fix anything.
What is a constructor?
A constructor is a function to initialize the state of objects. A constructor is invoked when an object is created.
There are different types of constructors:
- The default constructor has no parameters.
- Parameterized constructors are the ones that have parameters.
- The copy constructor has one parameter which is of type of the current class. If the class does not require dynamic allocation of memory then there is no need for the programmer to define the copy constructor explicitly.
Constructors are not inheritable by default, but when a child class’s object is created, the constructor of the parent class is also invoked, even before the constructor of the child class.
What is a destructor?
The destructor’s role is to free up all the memory that an object occupies. It is called when the object is garbage collected.
Usually, we should explicitly define the destructor only when our class requires dynamic allocation of memory. On the other case, the default destructor (implicitly defined by programming language) is sufficient.
The destructor is not inheritable neither, however, the destructor of the parent class is invoked after the destructor of the child class when a child class’s object is collected.
References:
- OOPs Interview Questions, interviewbit.
- Top 50 Important OOPs Interview Questions and Answers, edureka.
Thank you author for your post. Keep it up.