0% found this document useful (0 votes)
12 views

Oop in

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Oop in

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

OOPs Interview Questions

Object-oriented programming (OOPs) is a programming paradigm that is based on the concept of


objects rather than just functions and procedures.

An object is a real word entity that contains data and code. It allows binding data and code
together.

The aim of OOP is to implement real-world entities like inheritance, hiding, polymorphism in
programming. The main purpose of OOP is to bind together the data and the functions that
operate on them so that no other part of the code can access this data except that function

The OOP has the following four features:

Inheritance
Encapsulation
Polymorphism
Data Abstraction

OOPS ,helps in writing a complex piece of code easily, but it also allows users to handle
and maintain them easily as well. Not only that, the main pillar of OOPs - Data Abstraction,
Encapsulation, Inheritance, and Polymorphism, makes it easy for programmers to solve complex
scenarios. As a result of these, OOPs is so popular.

Advantages of OOP

It follows a bottom-up approach.


It models the real word well.
It allows us the reusability of code..
Programmer are able to reach their goals faster.
Minimizes the complexity.
Easy redesign and extension of code that does not affect the other functionality.

Disadvantages of OOP

Proper planning is required.


Program design is tricky.
Programmer should be well skilled.
Classes tend to be overly generalized.

What are the limitations of OOPs?

Requires intensive testing processes.


Solving problems takes more time as compared to Procedure Oriented Programming.
In certain scenarios, these programs can consume a large amount of memory.
Not suitable for small problems.
Takes more time to solve problems.

The programming language is called pure object-oriented language that treats everything
inside the program as an object. The primitive types are not supported by the pure OOPs
language. There are some other features that must satisfy by a pure object-oriented
language:
Encapsulation
Inheritance
Polymorphism
Abstraction
All predefined types are objects
All user-defined types are objects
All operations performed on objects must be only through methods exposed to the objects.

Class: A class is a blueprint of an object. It is a user-defined data type. Inside a class,


we define variables, constants, member functions, and other functionality. It does not
consume memory at run time. It is a logical entity. It is the best example of data binding.

Object: An object is a real-world entity that has attributes, behavior, and properties. It
is referred to as an instance of the class. It contains member functions, variables that we
have defined in the class. It occupies space in the memory. Different objects have different
states or attributes, and behaviors.

The method is a function that is associated with an object. In Python, a method is not
unique to class instances. Any object type can have methods.

Inheritance
Inheritance is the capability of one class to derive or inherit the properties from another
class. The class that derives properties is called the derived class or child class and the
class from which the properties are being derived is called the base class or parent class

The benefits of inheritance are:

It represents real-world relationships well.


It provides the reusability of a code. We don’t have to write the same code again and again.
Also, it allows us to add more features to a class without modifying it.
It is transitive in nature, which means that if class B inherits from another class A, then
all the subclasses of B would automatically inherit from class A.

Types of Inheritance –
Single Inheritance:
Single-level inheritance enables a derived class to inherit characteristics from a
single-parent class.

Multilevel Inheritance:
Multi-level inheritance enables a derived class to inherit properties from an immediate
parent class which in turn inherits properties from his parent class.

Hierarchical Inheritance:
Hierarchical level inheritance enables more than one derived class to inherit properties
from a parent class.

Multiple Inheritance:
Multiple level inheritance enables one derived class to inherit properties from more than
one base class.

Hybrid inheritance: This form combines more than one form of inheritance. Basically, it is a
blend of more than one type of inheritance.

Subclassing (Calling constructor of parent class)


A child class needs to identify which class is its parent class. This can be done by
mentioning the parent class name in the definition of the child class.

There are 2 built-in functions in Python that are related to inheritance. They are:

1. isinstance(): It checks the type of an object. Its syntax is:


isinstance(object_name, class_name)
It would return True if the class of object_name is class_name else False.

2. issubclass(): It checks whether a specific class is the child class of another class or
not. Its syntax is:
issubclass(childclass_name, parentclass_name)
It would return True if the entered child class is actually derived from the entered parent
class else, it returns False.

Overriding a method means redefining a method in the subclass when it has already been
defined in some other class.
A method in the subclass would be called as overridden only when there exists another method
with the same name and same set of parameters in the superclass.

A superclass or base class is a class that acts as a parent to some other class or classes.

A class that inherits from another class is called the subclass

Encapsulation

Encapsulation refers to binding the data and the code that works on that together in a
single unit.

Access specifiers or access modifiers are keywords that determine the accessibility of
methods, classes, etc in OOPs. These access specifiers allow the implementation of
encapsulation. The most common access specifiers are public, private and protected.

Polymorphism

The word polymorphism means having many forms. In programming, polymorphism means the same
function name (but different signatures) being used for different types.
By polymorphism, we understand that one task can be performed in different ways.

Static polymorphism (static binding) is a kind of polymorphism that occurs at compile time.
An example of compile-time polymorphism is method overloading.

Runtime polymorphism or dynamic polymorphism (dynamic binding) is a type of polymorphism


which is resolved during runtime. An example of runtime polymorphism is method overriding.

Method overloading is a feature of OOPs which makes it possible to give the same name to
more than one methods within a class if the arguments passed differ.

Method overriding is a feature of OOPs by which the child class or the subclass can redefine
methods present in the base class or parent class. Here, the method that is overridden has
the same name as well as the signature meaning the arguments passed and the return type.

Operator overloading refers to implementing operators using user-defined types based on the
arguments passed along with it.

Data abstraction
Abstraction is used to hide internal details and show only functionalities.

Data abstraction can be achieved through:

Abstract class
Abstract method

An abstract class is a class that consists of abstract methods. These methods are basically
declared but not defined. If these methods are to be used in some subclass, they need to be
exclusively defined in the subclass.

Advantages Of Abstraction

Only you can make changes to your data or function and no one else can.
Makes the application secure by not allowing anyone else to see the background details.
Increases reusability of the code.
Avoids duplication of your code.

What are the characteristics of an abstract class?


An abstract class is a class that is declared as abstract. It cannot be instantiated and is
always used as a base class. The characteristics of an abstract class are as follows:

Instantiation of an abstract class is not allowed. It must be inherited.


An abstract class can have both abstract and non-abstract methods.
An abstract class must have at least one abstract method.
You must declare at least one abstract method in the abstract class.
It is always public.
It is declared using the abstract

There are 2 types of abstraction,

Data Abstraction
Hiding the details about the data is called data abstraction.
Control Abstraction
Hiding the details about the implementation is called control abstraction.

A constructor is a special type of method that has the same name as the class and is used to
initialize objects of that class.

A destructor is a method that is automatically invoked when an object is destroyed. The


destructor also recovers the heap space that was allocated to the destroyed object, closes
the files and database connections of the object, etc.

In Python, there are two different types of constructors.

Non-parameterized Constructor: The constructors in Python which have no parametres present


is known as a non parameterized constructor.

Parameterized Constructor: A constructor that has a parametre pre defined is known as a


parameterized constructor.
IN BUILT CLASS FUNCTIONS

getattr(obj,name,default): This in built function in Python is used to gain access to the


attributes of a class.

delattr(obj, name): If you need to delete a specific attribute in a class, then make use of
this inbuilt function

setattr(obj, name,value): In a certain situation, if you decide to set a particular value to


a specific attribute, then make use of this function which comes inbuilt in Python.

hasattr(obj, name): Last but not least, if you need to see if a particular object contains
an attribute, then make use of this function. Upon execution, this will return true if an
attribute is present in a function.

Inbuilt Class Attributes

__dict__: By using this you can view the dictionary that contains information regarding the
class namespace.

__name__: Use this attribute, if you need to view the name of the current class.

__doc__: This attribute contains a string, which has the documentation for the current
class.

__module__: If you need to access the module in which the class is defined make use of this
inbuilt attribute.

__bases__: If you need to view the tuple which includes all the base classes, then use this
function.

An exception is a kind of notification that interrupts the normal execution of a program.


Exceptions provide a pattern to the error and transfer the error to the exception handler to
resolve it. The state of the program is saved as soon as an exception is raised.

Exception handling in Object-Oriented Programming is a very important concept that is used


to manage errors. An exception handler allows errors to be thrown and caught and implements
a centralized mechanism to resolve them.

The self-parameter refers to the current instance of the class and accesses the class
variables. We can use anything instead of self, but it must be the first parameter of any
function which belongs to the class.

Manipulators are helping functions. It is used to manipulate or modify the input or output
stream. The modification is possible by using the insertion (<<) and extraction (>>)
operators.

What are the rules for creating a constructor?


It cannot have a return type.
It must have the same name as the Class name.
It cannot be marked as static.
It cannot be marked as abstract.
It cannot be overridden.
It cannot be final

Composition is one of the vital concepts in OOP. It describes a class that references one or
more objects of other classes in instance variables. It allows us to model a has-a
association between objects.

The main benefits of composition are:

Reuse existing code


Design clean APIs
Change the implementation of a class used in a composition without adapting any external
clients.

Instance Variable: It is an object-level variable. It should be declared inside a class but


must be outside a method, block, and constructor. It is created when an object is created by
using the new keyword. It can be accessed directly by calling the variable name inside the
class.

Static Variable: It is a class-level variable. It is declared with keyword static inside a


class but must be outside of the method, block, and constructor. It stores in static memory.
Its visibility is the same as the instance variable. The default value of a static variable
is the same as the instance variable. It can be accessed by calling the
class_name.variable_name.

Local Variable: It is a method-level variable. It can be declared in method, constructor, or


block. Note that the use of an access modifier is not allowed with local variables. It is
visible only to the method, block, and constructor in which it is declared. Internally, it
is implemented at the stack level. It must be declared and initialized before use.

Reference Variable: It is a variable that points to an object of the class. It points to the
location of the object that is stored in the memory.

You might also like