0% found this document useful (0 votes)
2 views13 pages

Infosys Oops Master Guide Python

The document serves as a comprehensive guide to Object-Oriented Programming (OOP) concepts in Python, covering definitions, examples, and interview questions related to encapsulation, abstraction, inheritance, and polymorphism. It explains key differences between classes and objects, and includes practical Python code examples to illustrate each concept. Additionally, it provides a comparison between abstract classes and interfaces, as well as insights into compile-time and runtime polymorphism.

Uploaded by

likithprabhu3
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views13 pages

Infosys Oops Master Guide Python

The document serves as a comprehensive guide to Object-Oriented Programming (OOP) concepts in Python, covering definitions, examples, and interview questions related to encapsulation, abstraction, inheritance, and polymorphism. It explains key differences between classes and objects, and includes practical Python code examples to illustrate each concept. Additionally, it provides a comparison between abstract classes and interfaces, as well as insights into compile-time and runtime polymorphism.

Uploaded by

likithprabhu3
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Infosys Interview – OOPs Master Guide (Python-

Based)

What is OOP?
Object-Oriented Programming (OOP) is a programming approach where programs are designed using
classes and objects to improve modularity, reusability, scalability, and maintainability.

It helps organize software by modeling real-world entities and behaviors into structured programming
components.

Class and Object


Class
A class is a blueprint or template used to define properties and behaviors.

Example: Car

Object
An object is an actual instance created from a class.

Example: BMW, Audi, Tesla

Difference Between Class and Object


A class is a blueprint or template, while an object is a real instance created from that class.

Python Example

class Car:

def start(self):
print("Car started")

obj = Car()

1
[Link]()

4 Pillars of OOP
1. Encapsulation
2. Abstraction
3. Inheritance
4. Polymorphism

1. Encapsulation
Definition
Encapsulation is the process of combining data and methods together while restricting direct access to
sensitive data.

Core Idea
Data hiding + controlled access.

Why Encapsulation is Used


• Improve data security
• Restrict unauthorized access
• Provide controlled data modification
• Improve maintainability

Real-Life Example
ATM machine: Users can withdraw money or check balance, but internal banking data and transaction
processing remain hidden.

2
Python Example

class Bank:

def __init__(self):
self.__balance = 1000

def deposit(self, amount):


self.__balance += amount

def show_balance(self):
return self.__balance

obj = Bank()

[Link](500)

print(obj.show_balance())

Important Points
• __balance means private variable
• Double underscore (__) restricts direct access
• Methods provide controlled access to private data

Interview Questions
What is encapsulation?
Encapsulation is the process of combining data and methods together while restricting direct access to
sensitive data.

Why use encapsulation?


Encapsulation improves security and controlled access to sensitive information.

3
How is encapsulation achieved in Python?
Using private variables and methods with double underscore (__).

What does double underscore mean?


Double underscore is used to make variables private and restrict direct access.

Memory Lock
Encapsulation = Data Hiding + Controlled Access

2. Abstraction
Definition
Abstraction is the process of hiding implementation details and showing only essential functionalities to
the user.

Core Idea
Hide HOW. Show WHAT.

Why Abstraction is Used


• Reduce complexity
• Improve usability
• Hide unnecessary internal details

Real-Life Example
Driving a car: Users use steering, brakes, and accelerator without knowing internal engine mechanisms.

4
Python Example

from abc import ABC, abstractmethod

class Animal(ABC):

@abstractmethod
def sound(self):
pass

class Dog(Animal):

def sound(self):
print("Bark")

obj = Dog()

[Link]()

Important Points
• ABC is used for abstract classes
• @abstractmethod defines methods without implementation
• Child classes must provide implementation

Interview Questions
What is abstraction?
Abstraction is the process of hiding implementation details and showing only essential functionalities to
the user.

Why use abstraction?


To reduce complexity and improve usability.

5
How is abstraction achieved in Python?
Using abstract classes and abstract methods.

What is an abstract class?


A class containing abstract methods that cannot be instantiated directly.

What is an abstract method?


A method declared without implementation that child classes must define.

Difference Between Encapsulation and


Abstraction
Encapsulation focuses on protecting data and controlling access.

Abstraction focuses on hiding implementation details and reducing complexity.

Memory Lock
Abstraction = Implementation Hiding

3. Inheritance
Definition
Inheritance is the process where one class acquires properties and behaviors from another class.

Core Idea
Reuse parent class features.

6
Why Inheritance is Used
• Improve code reusability
• Avoid rewriting code
• Maintain hierarchical structure

Real-Life Example
Animal → Dog

Dog automatically inherits behaviors from Animal.

Python Example

class Animal:

def eat(self):
print("Animal eats")

class Dog(Animal):

def bark(self):
print("Dog barks")

obj = Dog()

[Link]()
[Link]()

Important Points
• Parent class provides properties/methods
• Child class inherits them
• Code reuse happens automatically

7
Types of Inheritance
Single Inheritance
One parent → one child

Example: Animal → Dog

Multilevel Inheritance
Grandparent → parent → child

Example: Animal → Mammal → Dog

Hierarchical Inheritance
One parent → many child classes

Example: Animal → Dog Animal → Cat

Multiple Inheritance
One child inherits from multiple parents.

Mostly achieved using interfaces in Java.

Interview Questions
What is inheritance?
Inheritance is the process where one class acquires properties and behaviors from another class.

Why use inheritance?


To improve code reusability and avoid rewriting code.

What is parent class?


The class whose properties and methods are inherited.

8
What is child class?
The class that inherits properties and methods from the parent class.

Memory Lock
Inheritance = Code Reusability

4. Polymorphism
Definition
Polymorphism is the ability of a method or object to behave differently in different situations.

Core Idea
Same method → different behavior.

Why Polymorphism is Used


• Improve flexibility
• Improve code reusability
• Allow dynamic behavior

Real-Life Example
A person behaves differently with friends, teachers, and parents.

Python Example

class Dog:

def sound(self):
print("Bark")

9
class Cat:

def sound(self):
print("Meow")

Types of Polymorphism
Compile-Time Polymorphism (Method Overloading)
Same method name with different parameters.

Example

class Math:

def add(self, a, b=0, c=0):


return a+b+c

Runtime Polymorphism (Method Overriding)


Child class changes parent class method implementation.

Example

class Animal:

def sound(self):
print("Animal sound")

class Dog(Animal):

def sound(self):
print("Bark")

10
Interview Questions
What is polymorphism?
Polymorphism is the ability of a method or object to behave differently in different situations.

Types of polymorphism?
• Method Overloading
• Method Overriding

What is method overloading?


Same method with different parameters.

What is method overriding?


Child class redefining parent class method.

Difference Between Overloading and Overriding


Overloading
• Same method name
• Different parameters
• Same class
• Compile-time polymorphism

Overriding
• Same method name
• Same parameters
• Parent-child relationship
• Runtime polymorphism

Memory Lock
Polymorphism = Same Method → Different Behavior

11
Interface vs Abstract Class
Abstract Class
• Partial blueprint
• Can contain abstract and normal methods
• Used for partial abstraction

Interface
• Full contract/rulebook
• Defines method declarations
• Used for pure abstraction

Difference
Abstract class allows partial implementation.

Interface mainly defines rules that classes must implement.

Important Quick Concepts


Compile-Time Polymorphism
Method decision made during compilation.

Example: Method Overloading

Runtime Polymorphism
Method decision made during execution.

Example: Method Overriding

Final OOP Interview Revision Points


Must revise:

• Class vs Object

12
• Encapsulation
• Abstraction
• Inheritance
• Polymorphism
• Overloading vs Overriding
• Abstract Class vs Interface
• Compile-time vs Runtime polymorphism
• Real-life examples
• Basic Python code examples

Final Memory Locks


Encapsulation = Data Hiding

Abstraction = Implementation Hiding

Inheritance = Code Reusability

Polymorphism = Same Method Different Behavior

13

You might also like