![]() |
VOOZH | about |
Object-Oriented Programming (OOP) is a core paradigm in software development that models real-world entities into objects combining data and behavior. The following are topic wise interview questions on Object Oriented Programming.
Object Oriented Programming is a programming paradigm where the complete software operates as a bunch of objects talking to each other. An object is a collection of data and the methods which operate on that data.
The main advantage of OOP is better manageable code that covers the following:
The programming paradigm is referred to the technique or approach of writing a program. The programming paradigms can be classified into the following types:
1. Imperative Programming Paradigm
It is a programming paradigm that works by changing the program state through assignment statements. The main focus in this paradigm is on how to achieve the goal. The following programming paradigms come under this category:
2. Declarative Programming Paradigm
Declarative programming focuses on what is to be executed rather than how it should be executed. In this paradigm, we express the logic of a computation without considering its control flow. The declarative paradigm can be further classified into:
Structured Programming is a technique that is considered a precursor to OOP and usually consists of well-structured and separated modules. It is a subset of procedural programming. The difference between OOPs and Structured Programming is as follows:
Object-Oriented Programming | Structural Programming |
|---|---|
| Programming that is object-oriented is built on objects having a state and behavior. | A program's logical structure is provided by structural programming, which divides programs into their corresponding functions. |
| It follows a bottom-to-top approach. | It follows a Top-to-Down approach. |
| Restricts the open flow of data to authorized parts only providing better data security. | No restriction to the flow of data. Anyone can access the data. |
| Enhanced code reusability due to the concepts of polymorphism and inheritance. | Code reusability is achieved by using functions and loops. |
| Methods work dynamically, making calls based on object behavior and the need of the code at runtime. | Functions are called sequentially, and code lines are processed step by step. |
| Modifying and updating the code is easier. | Modifying the code is difficult as compared to OOPs. |
| Data is given more importance in OOPs. | Code is given more importance. |
OOPs paradigm is one of the most popular programming paradigms. It is widely used in many popular programming languages such as:
Advantages of OOPs | Disadvantages of OOPs |
|---|---|
| OOPs provides enhanced code reusability. | The programmer should be well-skilled and should have excellent thinking in terms of objects as everything is treated as an object in OOPs. |
| The code is easier to maintain and update. | Proper planning is required because OOPs is a little bit tricky. |
| It provides better data security by restricting data access and avoiding unnecessary exposure. | OOPs concept is not suitable for all kinds of problems. |
| Fast to implement and easy to redesign resulting in minimizing the complexity of an overall program. | The length of the programs is much larger in comparison to the procedural approach. |
A class is a building block of Object-Oriented Programs. It is a user-defined data type that contains the data members and member functions that operate on the data members. It is like a blueprint or template of objects having common properties and methods.
An object is an instance of a class. Data members and methods of a class cannot be used directly. We need to create an object (or instance) of the class to use them. In simple terms, they are the actual world entities that have a state and behaviour.
student1.name: Rahul
The main feature of the OOPs, also known as 4 pillars or basic principles of OOPs are as follows:
Encapsulation is the binding of data and methods that manipulate them into a single unit such that the sensitive data is hidden from the users
It is implemented as the processes mentioned below:
Abstraction is similar to data encapsulation and is very important in OOP. It means showing only the necessary information and hiding the other irrelevant information from the user. Abstraction is implemented using classes and interfaces.
π abstraction in OOPsThe idea of inheritance is simple, a class is derived from another class and uses data and implementation of that other class. The class which is derived is called child or derived or subclass and the class from which the child class is derived is called parent or base or superclass.
The main purpose of Inheritance is to increase code reusability. It is also used to achieve Runtime Polymorphism.
The word "Polymorphism" means having many forms. It is the property of some code to behave differently for different contexts. For example, in C++ language, we can define multiple functions having the same name but different working depending on the context.
Polymorphism can be classified into two types based on the time when the call to the object or function is resolved. They are as follows:
A) Compile-Time Polymorphism
Compile time polymorphism, also known as static polymorphism or early binding is the type of polymorphism where the binding of the call to its code is done at the compile time. Method overloading or operator overloading are examples of compile-time polymorphism.
B) Runtime Polymorphism
Also known as dynamic polymorphism or late binding, runtime polymorphism is the type of polymorphism where the actual implementation of the function is determined during the runtime or execution. Function overriding is an example of this method.
Access specifiers are special types of keywords that are used to specify or control the accessibility of entities like classes, methods, and so on. Private, Public, and Protected are examples of access specifiers or access modifiers.
The key components of OOPs, encapsulation and data hiding, are largely achieved because of these access specifiers.
A compile-time polymorphism feature called overloading allows an entity to have numerous implementations of the same name. Method overloading and operator overloading are two examples.
Overriding is a form of runtime polymorphism where an entity with the same name but a different implementation is executed. It is implemented with the help of virtual functions.
Yes, there are more challenges when you have more authority. Although inheritance is a very strong OOPs feature, it also has significant drawbacks.
Inheritance can be classified into 5 types which are as follows:
Note: Type of inheritance supported is dependent on the language. For example, Java does not support multiple inheritance.
A unique class type known as an interface contains methods but not their definitions. Inside an interface, only method declaration is permitted. You cannot make objects using an interface. Instead, you must put that interface into use and specify the procedures for doing so.
Both abstract classes and interfaces are special types of classes that just include the declaration of the methods, not their implementation. An abstract class is completely distinct from an interface, though. Following are some major differences between an abstract class and an interface.
Abstract Class | Interface |
|---|---|
| A class that is abstract can have both abstract and non-abstract methods. | An interface can only have abstract methods. |
| An abstract class can have final, non-final, static and non-static variables. | The interface has only static and final variables. |
| Abstract class doesn't support multiple inheritance | An interface supports multiple inheritance. |
Classes do not use memory. They merely serve as a template from which items are made. Now, objects actually initialize the class members and methods when they are created, using memory in the process.
No. If the base class includes non-static methods, an object must be constructed. But no objects need to be generated if the class includes static methods. In this instance, you can use the class name to directly call those static methods.
The structure is also a user-defined datatype in C++ similar to the class with the following differences:
A constructor is a block of code that initializes the newly created object. A constructor resembles an instance method but itβs not a method as it doesnβt have a return type. It generally is the method having the same name as the class but in some languages, it might differ. For example:
In python, a constructor is named __init__.
In C++ and Java, the constructor is named the same as the class name.
Example:
The most common classification of constructors includes:
The default constructor is a constructor that doesn't take any arguments. It is a non-parameterized constructor that is automatically defined by the compiler when no explicit constructor definition is provided.
It initializes the data members to their default values.
It is a user-defined constructor having no arguments or parameters.
Example:
The constructors that take some arguments are known as parameterized constructors.
Example:
A copy constructor is a member function that initializes an object using another object of the same class.
Example:
In Python, we do not have built-in copy constructors like Java and C++ but we can make a workaround using different methods.
A destructor is a method that is automatically called when the object goes out of scope or destroyed.
In C++, the destructor name is also the same as the class name but with the (~) tilde symbol as the prefix.
In Python, the destructor is named __del__.
Example:
In Java, the garbage collector automatically deletes the useless objects so there is no concept of destructor in Java. We could have used finalize() method as a workaround for the java destructor, but it is also deprecated since Java 9.
Yes We can overload the constructor in a class in Java. Constructor Overloading is done when we want constructor with different constructor with different parameter (Number and Type).
No, a destructor cannot be overloaded in a class. There can only be one destructor present in a class.
Friend Functions: A friend function is a special function that is allowed to access private and protected data of a class, even though it's not a member of the class.
Friend Class: A friend class is a class that can access the private and protected members of another class. It's like allowing a trusted friend or a group of friends to see and change your personal information, which others cannot.
A virtual function is a function that is used to override a method of the parent class in the derived class. It is used to provide abstraction in a class.
Example:
A pure virtual function, also known as an abstract function, is a member function that doesn't contain any statements. This function is defined in the derived class if needed.
Example:
In Python, we achieve this using @abstractmethod from the ABC (Abstract Base Class) module.
In general terms, an abstract class is a class that is intended to be used for inheritance. It cannot be instantiated. An abstract class can consist of both abstract and non-abstract methods.
Example:
In Python, we use ABC (Abstract Base Class) module to create an abstract class.
1. Core Concepts: Classes & Objects, Encapsulation, Polymorphism, Abstraction, Access Modifiers, Inheritance
2. Advanced Topics: Creational Design Patterns, Structural Design Patterns, Behavioral Design Patterns, SOLID Principles, Refactoring Techniques.