1. What is OOPs?
Object-Oriented Programming is a way of approaching, designing and developing software, so that the components of the software and the interactions between them resemble real-life objects and their interactions.
Proper usage of OOPs concepts helps us build well-organized systems that are easy to use and extend.
2. What are the advantages of OOPs?
The advantages of OOPs:
- Easier way to analyse and solve bugs
- Reusability of code through inheritance
- Effective problem solving
- Elimination of code redundancy
3. What are the principles of OOPs?
The principles of OOPs involve,
- Abstraction
- Encapsulation
- Inheritance
- Polymorphism
Abstraction in python is defined as a process of handling complexity by hiding unnecessary information from the user. This is one of the core concepts of object-oriented programming (OOP) languages.
Polymorphism is an OOPs concept. The word polymorphism means having many forms. It refers to the use of a single type entity (method, operator, or object) to represent different types in different scenarios
4. What are Classes?
A
Code
Output
5. What is __init__ / constructor in Python?
The
Code
In the above example, the
6. What is self in OOPs?
In Python, the
Code
Output
7. What are class methods?
Methods which need access to class attributes but not instance attributes are marked as class methods. For class methods, we send
Code
Output
8. What is Inheritance?
Inheritance is a mechanism by which a class inherits attributes and methods from another class.
The class whose attributes and methods are inherited is known as the Super/Base/Parent Class. And the class that inherits the attributes and methods from the parent class is the Sub/Derived/Child Class.
9. Write an example program to show Inheritance?
Code
Output
In the above example, the child class ElectronicItem inherits all the attributes and methods from the parent class Product.
10. What are the types of Inheritance?
There are 5 types of Inheritance in Python:
- Single Inheritance
- Multiple Inheritance
- Multilevel Inheritance
- Hierarchical Inheritance
- Hybrid Inheritance
Single Inheritance:
When a Child class inherits attributes and methods from a single Parent class, it is called Single Inheritance.
Multiple Inheritance:
When a Child class inherits attributes and methods from more than one Parent class, it is called Multiple Inheritance.
Multilevel Inheritance:
When a Child class inherits attributes and methods from another Child class, it is called Multilevel Inheritance.
Hierarchical Inheritance:
When more than one Child class inherits attributes and methods from a single Parent class, it is called Hierarchical Inheritance.
Hybrid Inheritance:
Any combination of more than one type of inheritance is called Hybrid Inheritance.
11. What is Encapsulation?
While modelling objects with object-oriented programming, we bundle related information together to clearly separate information of different objects.
The bundling of related attributes and methods together is called Encapsulation.
Classes can be used to bundle related attributes and methods.
12. What is Method Overriding?
Method Overriding is an OOPs concept related to Inheritance. When a child class method overrides the parent class method of the same name, parameters and return type, it is known as Method Overriding.
Method Overriding allows us to change the implementation of a function in the child class that is defined in the parent class.
Code
Output
In the above example, the