What is OOP in Python and why we need oops?
OOP stands for Object-Oriented Programming. In the future, all the code you write with the help of OOP will utilize its advanced features, making it easier to create well-structured and organized programs.
OOP provides many features that help you write code in a clear and efficient way, such as:
- Classes and Objects
- Inheritance
- Abstraction
- Encapsulation
- Polymorphism
1) Classes and Objects:
In OOP, all the code is organized into classes and objects.
Class :
Class is like a blueprint (structure) for an object. Inside the class we define the properties, methods and behaviors for an object.
We create a class by using class keyword.
For Example:
class classname:
# inner structure
What we write within the class?
Inside the class we define our methods, properties and attributes. Within a single class there are n numbers of methods we create according to our need.
What are the methods in oops?
methods are very similar to the Python simple functions. You only keep one thing in your mind. If you are create a function within the class then that function is called method.
remember a method that we create in the class, always holds a parameter by default thats called self.
For Example:
class classname:
def method1(self):
#method 1 codee
def method2(self):
# method 2 codee
What is self in Python oops and why we need self?
self is a way for a class to refer to the specific object (or instance) that’s using it.
Think of self as meaning “this object right here.” It allows each object to keep track of its own data and use its own methods.
For example, if you have a Dog class, each dog you create (like "Buddy" or "Max") is its own object. self helps each dog object keep track of its own name, age, and actions without mixing up with other dogs.
Example of class:
class vehicle:
def car(self):
print("This is a car.")
def bus(self):
print("This is a bus.")
def train(self):
print("This is a train.")
What is object in Python oops?
Object is the instances (part) of the class. We are not able to use any functionality of the class, for while we are not creating an object of the class.
How to create an object in oops?
It is very simple to create an object for a class. Create a variable or that variable holds the class name with parentheses ().
That that variable is called the object of that class.
For Example:
class vehicle:
def car(self):
print("This is a car.")
def bus(self):
print("This is a bus.")
def train(self):
print("This is a train.")
obj = vehicle() #now obj is the object of vehicle class.
Now you are able to call anything from the class by using this obj object.
For Example:
class vehicle:
def car(self):
print("This is a car.")
def bus(self):
print("This is a bus.")
def train(self):
print("This is a train.")
obj = vehicle()
obj.train()
Output:
This is a train.
You also call multiple things by using object.
For Example:
class vehicle:
def car(self):
print("This is a car.")
def bus(self):
print("This is a bus.")
def train(self):
print("This is a train.")
obj = vehicle()
obj.train()
obj.car()
obj.bus()
Output:
This is a train.
This is a car.
This is a bus.
You are also able to make multiple objects for a single class.
13+ simple interview questions covering OOP intro, classes, objects, self
, and methods in Python
Question 1. What is Object-Oriented Programming (OOP) in Python? Why do we need OOP?
Answer:Object-Oriented Programming (OOP) is a programming style that helps organize code by grouping related properties and behaviors into classes and objects. OOP makes code more structured, modular, and easier to understand by modeling real-world concepts directly in code.
With OOP, you can:
- Easily reuse code.
- Create organized and maintainable code.
- Implement complex ideas in a manageable way.
Question 2. What are Classes and Objects in Python OOP?
Answer:
Class: A class is like a blueprint that defines the structure and behavior of objects.
Object: An object is an instance of a class. It is created from the class and contains specific data.
Example:
class Animal:
def sound(self):
print("Animals make sounds")
# Creating an object of the class
dog = Animal()
dog.sound()
Output:
Animals make sounds
Question 3. How do you create a class in Python?
Answer: To create a class in Python, use the class keyword followed by the class name and a colon.
Example:
class Person:
# This is a class definition with no attributes or methods
pass
Question 4. What is a method in Python OOP?
Answer: A method is a function defined within a class. It describes behaviors that an object created from the class can perform.
Example:
class Vehicle:
def start_engine(self):
print("The engine has started.")
# Creating an object of Vehicle class
car = Vehicle()
car.start_engine()
Output:
The engine has started.
Question 5. What is self in Python OOP, and why do we need it?
Answer: self refers to the specific instance (object) of the class that’s being used. It allows each object to access its own attributes and methods, keeping each object's data separate.
Example:
class Dog:
def bark(self):
print("Woof!")
# 'self' allows each object (e.g., dog1, dog2) to call its own methods
dog1 = Dog()
dog1.bark()
Output:
Woof!
Question 6. How do you create an object in Python OOP?
Answer: To create an object of a class, assign the class name with parentheses to a variable.
Example:
class Bird:
def fly(self):
print("Birds can fly.")
# Creating an object of the Bird class
sparrow = Bird()
sparrow.fly()
Output:
Birds can fly.
Question 7. What is the difference between a class and an object?
Answer:
Class: A template or blueprint that defines properties and behaviors.
Object: An instance of a class that holds specific data and can use the methods defined in the class.
Example:
class Fruit:
def taste(self):
print("Fruits taste good.")
apple = Fruit()
banana = Fruit()
apple.taste()
banana.taste()
Output:
Fruits taste good.
Fruits taste good.
Question 8. How do you define attributes in a class?
Answer: Attributes are defined in the __init__ method of the class, allowing each object to store its own data.
Example:
class Car:
def __init__(self, color):
self.color = color
car1 = Car("red")
car2 = Car("blue")
print(car1.color)
print(car2.color)
Output:
red
blue
Question 9. How do you call a method of an object?
Answer: You call a method by using the object name, a dot, and the method name.
Example:
class Book:
def read(self):
print("Reading a book.")
# Creating an object
my_book = Book()
my_book.read()
Output:
Reading a book.
Question 10. What happens if you forget to include self in a method?
Answer: If you forget to include self in a method, Python will raise an error because it won’t know which instance the method should refer to.
Example:
class Animal:
def speak():
print("Animals speak.")
obj = Animal()
obj.speak()
Output:
TypeError: Animal.speak() takes 0 positional arguments but 1 was given
Question 11. Can you create multiple objects of the same class?
Answer: Yes, you can create multiple objects from the same class, each with its own data.
Example:
class Flower:
def __init__(self, color):
self.color = color
rose = Flower("red")
lily = Flower("white")
print(rose.color)
print(lily.color)
Output:
red
white
Question 12. What is an instance variable in Python OOP?
Answer: An instance variable is a variable defined in the __init__ method, specific to each object of the class.
Example:
class Laptop:
def __init__(self, brand):
self.brand = brand
laptop1 = Laptop("Dell")
laptop2 = Laptop("HP")
print(laptop1.brand)
print(laptop2.brand)
Output:
Dell
HP
Question 13. Can methods in a class access both class and instance variables?
Answer: Yes, methods can access instance variables using self.
Example:
class Computer:
type = "Electronics" # Class variable
def __init__(self, brand):
self.brand = brand # Instance variable
def display(self):
print(f"{self.brand} is a type of {Computer.type}")
pc = Computer("Apple")
pc.display()
Output:
Apple is a type of Electronics
Question 14. Can you call a method from within another method in the same class?
Answer: Yes, you can call another method within the same class using self.
Example:
class Phone:
def turn_on(self):
print("Phone is on.")
def make_call(self):
self.turn_on() # Calling another method
print("Making a call.")
my_phone = Phone()
my_phone.make_call()
Output:
Phone is on.
Making a call.
Tags:
Leave a comment
You must be logged in to post a comment.