OOP Interview Questions
Constructor:
- A constructor is a special member function of a class that is automatically called when an object is created.
Destructor:
- A destructor is a special member function of a class that is automatically called when an object of the class goes out of scope or is deleted.
- Overloading deals with multiple methods in the same class with the same name but different signatures.
- Overriding deals with two methods, one in a parent class & one in a child class, that have the same signatures.
Polymorphism is composed of two words - “poly”, which means “many”, and “morph”, which means “shapes”.
Therefore, Polymorphism refers to something that has many shapes.
In OOP, Polymorphism refers to the process by which some code, data, method, or object behaves differently under different circumstances or contexts.
For Example:
(Shape) has a (Circle, Triangle, Square), This is called polymorphism from one thing.
There are two types of polymorphism:
1- Compile-time polymorphism (also known as static polymorphism) is achieved through function or operator overloading.
2- Runtime polymorphism (also known as dynamic polymorphism) is achieved through function overriding.
1- Extends:
- Keyword used to make inherit from the parent class.
2- Final:
- Keyword used to stop inheritance from this class.
In general terms, an abstract class is a class that is intended to be used for inheritance.
An abstract class can consist of both abstract and non-abstract methods.
- It cannot be instantiated. (Cannot create object from)
- Made for other classes to inherit prop & methods from.
- can have methods & properties.
- can have abstract methods & non-abstract methods.
- abstract methods no body code. (only parameters or arguments)
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.
The base class is the root class- the most generalized class. At the same time,
The superclass is the immediate parent class from which the other class inherits.
- Class is a blueprint that you can create an object from.
- Object is a member in the main application.
For Example:
(Car) is a class.
(BMW, Ford) are objects.
Encapsulation is the process of binding data members and methods of a program together to do a specific job, without revealing unnecessary details.
Encapsulation can also be defined in two different ways:
1) Data hiding: Encapsulation is the process of hiding unwanted information, such as restricting access to any member of an object.
2) Data binding: Encapsulation is the process of binding the data members and the methods together as a whole, as a class.
1- OOPs is very helpful in solving very complex level of problems.
2- Highly complex programs can be created, handled, and maintained easily using object-oriented programming.
3- OOPs, promote code reuse, thereby reducing redundancy.
4- OOPs also helps to hide the unnecessary details with the help of Data Abstraction.
5- OOPs, are based on a bottom-up approach, unlike the Structural programming paradigm, which uses a top-down approach.
6- Polymorphism offers a lot of flexibility in OOPs.
1- It requires intensive testing
2- Not apt for minor problems
3- It requires good planning
4- It takes more time to solve problems
5- Problems need to be thought in term of objects
Yes. The limitations of inheritance are:
1- Increased execution effort and time.
2- Tight coupling of parent and child class.
3- Requires correct implementation.
4- Requires jumping between different classes.
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, also known as visibility markers.
Inheritance is relationship between two or more classes where derived class inherits properties of pre-existing (class).
The 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.
If one class shares the behavior and structure defined in another multiple class, it is called multiple inheritance.
An interface refers to a special type of class, which contains methods, but not their definition. Only the declaration of methods is allowed inside an interface. To use an interface, you cannot create objects. Instead, you need to implement that interface and define the methods for their implementation.
The various types of inheritance include:
1- Single inheritance
2- Multiple inheritances
3- Multi-level inheritance
4- Hierarchical inheritance
5- Hybrid inheritance
Superclass is also a part of Inheritance. The superclass is an entity, which allows subclasses or child classes to inherit from itself.
It is also known as the parent class or base class.
The subclass is a part of Inheritance. The subclass is an entity, which inherits from another class. It is also known as the child class or delivered class.
An object refers to the instance of the class, which contains the instance of the members and behaviors defined in the class template. In the real world, an object is an actual entity to which a user interacts, whereas class is just the blueprint for that object. So the objects consume space and have some characteristic behavior.
For example, a specific car (bmw, ford, etc...).
A class can be understood as a template or a blueprint, which contains some values, known as member data or member, and some set of rules, known as behaviors or functions. So when an object is created, it automatically takes the data and functions that are defined in the class.
Therefore the class is basically a template or blueprint for objects. Also one can create as many objects as they want based on a class.
For example, first, a car’s template is created. Then multiple units of cars are created based on that template.
The main feature of the OOPs, also known as 4 pillars or basic principles of OOPs are as follows:
1- Encapsulation
2- Data Abstraction
3- Polymorphism
4- Inheritance
The programming languages that use and follow the Object-Oriented Programming paradigm or OOPs, are known as Object-Oriented Programming languages. Some of the major Object-Oriented Programming languages include:
- C++
- C#
- Java
- Javascript
- Python
- PHP
And many more.
1. Imperative Programming Paradigm:
- Procedural Programming Paradigm
- Object-Oriented Programming
- Parallel Programming
2- Declarative Programming Paradigm:
- Logical Programming Paradigm
- Functional Programming Paradigm
- Database Programming Paradigm
The main advantage of OOP is better-managed code that covers the following:
1- The overall understanding of the software is increased as the distance between the language spoken by developers and that spoken by users.
2- Object orientation eases maintenance by the use of encapsulation. One can easily change the underlying representation by keeping the methods the same.
3- The OOPs paradigm is mainly useful for relatively big software.
4- OOPs helps users to understand the software easily, although they don’t know the actual implementation.
5- With OOPs, the readability, understandability, and maintainability of the code increase multifold.
6- Even very big software can be easily written and managed easily using OOPs.
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.