You are on page 1of 10

1) Explain the importance of class with an example program?

Definition
Class is a collection of attributes (variables and functions) together in a single unit.
In oop, variables are also called data members, functions are also called as methods.
The collection of class members (data and methods) together called as class entity.

Syntax
class class_name:
member1
member2
………

Importance of class
 Class provides encapsulation and data hiding mechanism.
 Accessing class data is restricted from the outside world using access modifiers (public
private).
 Once the class (template) is ready, by using which we can create multiple copies of same
type of objects (we can consider class is like a factory for making objects).
 Static methods can be accessed with the class name without creating an object to the
class.

#Example
Write a program to demonstrate class and show how to access class members
class shape:
@staticmethods
def circle():
return("circle")
def triangle():
return("triangle")
def rectangle():
return("rectangle")

#calling class methods using class name


print (shape.circle())
print(shape.triangle())
print(shape.rectangle())

2) Explain the importance of object in python with an example program?

Definition
Instance of a class is called object.
Importance of object:
 Object is needed to access class data.
 Without Object we cannot access instance variables and instance methods.
 Object invokes constructor method to load class data into main memory during program
execution.
 Accessing Class methods using object must have the first argument named as self.
 The self argument refers to the object itself. Python provides current class object
reference as an argument value to the self automatically.
Syntax
object_name = class_name()

#Example
Create a template called shape and define triangle, rectangle, circle methods in it and return its
name as a result.
class shape:
def circle(self):
return("circle")
def triangle(self):
return("triangle")
def rectangle(self):
return("rectangle")

#calling class methods using class name


obj = shape()
print(obj.circle())
print(obj.triangle())
print(obj.rectangle())

3) Define inheritance? Explain different types of inheritance with an example program?


Answer
The technique of creating a new class from existing class is called inheritance. It means defining
a new class with little or no modification to an existing class.
The new class is called derived (or child) class and the one from which it inherits is called
the base (or parent) class.
Advantages
 Inheritance is a powerful feature in object oriented programming.
 It helps programmer to re-using existing classes.
 Re-using an existing piece of source code saves effort and cost required to build a
software product.
 It eliminates from the steps re-write, re-debug, and re-test the code which is already
been used in existing software.
 Inheritance follows top-down approach (generalized classes are designed first and then
specialized classes are derived by inheriting / extending the generalized classes).
Types of Inheritance

1) Single inheritance
2) Multi Level inheritance
3) Multiple inheritance
4) Multipath or hybrid inheritance

Single Inheritance

A class derived from one


base class is called Single
inheritance. 

Multi Level Inheritance


Derived class derived
from an already derived
class is called multi level
inheritance

Multiple Inheritance

Derived class inherits


properties from more
than one base class.

Multi Path/ Hybrid Inheritance


Deriving a class from other derived classes that are in turn derived from the same base class is called
mutlpath.

4) Explain method overriding with an example program?


Answer
 Modifying/providing the base class definition in derived class is called method
overriding.
(Or)
Overriding is a feature that allows a subclass/derived class to provide a definition to
base class methods which is already existed in super-classes or parent classes.
 Overriding concept can be easily recognized, when super class and derived class having
a method with the same name, then the method in the subclass is said to override the
method in the super-class.
 Run-time polymorphism can be achieved by using method overriding concept.
Run-time polymorphism means
The versions of a method defined in base class/derived class with the same name is
executed by using object. If an object of a parent class is used to invoke the method,
then the version in the parent class will be executed, but if an object of the subclass is
used to invoke the method, then the version in the child class will be executed.
 Super is a built in function, that is used to call overridden methods of base class.
 Derived class object hides the base class overridden methods, in-order to access
overridden methods we use super function from anywhere in derived class methods.

Example (Program)

5) Write short note on class members defined in a class?


Answer
 Class is a collection of data and methods.
 The mechanism of binding data and methods together in a single unit is called
encapsulation.
 Encapsulation provides data abstraction and hiding through classes.
 Data Abstraction/Hiding means essential details are provided to the outside world and
the implementation details are hidden.
Example
 Any entity outside the world does not know about the implementation details of the class
methods (see above diagram).

Data/Variable/class There are two types of attributes defined in class


attributes  Class variable
 Instance Variable
Class Methods A class can be defined with any one of these methods
 Constructor method (init )
 Destructor method(del)
 Static method (with/without any default parameter) (self/cls)
 Class method (cls as an argument type)
 Instance/object method (self as an argument)
 Normal/User defined methods
 Builtin methods such as str,repr,cmp,getitem,setitem

6) Define constructor and destructor methods with an example program?

Constructor
 Constructor allocates the resources (memory) to class members.
 It is a special method defined in a class either by user (explicitly) or by the python
interpreter (implicitly), if user not provided.
 It is automatically (implicitly) invoked when we create an object to the class.
 It is identified with the name __init__()
 Constructor method needs self as an argument as it is invoked with an object.
 It is mainly used to initialize class data (instance variables or class variable).
 Super ().__init__() method is used to invoke constructors explicitly in the case of
inheritance.

Destructor
 It is used to de allocate the resources occupied by an object (constructor)
 It is a special method written automatically by the python interpreter (implicitly), if
programmer not defined explicitly.
 It is invoked automatically (implicitly) when object is going out of scope.
 It is identified with the name __del__()

Example
Write a program to initialize class variable and instance variable with an example program
using constructor.
7) Explain the concept of operator overloading with an example program?
Operator Overloading
 The predefined operators (+,-,*…) existed in any programming language performs
operation between operands if they are valid types.
 These operators cannot works with user defined types like objects.
 To perform operation between user defined types, python allows the programmer to
extend the features of existing operators such as +,-,*… by using concept called operator
overloading.
Example:

Operato Operation Explanation


r
+ 4+5 It performs operation perfectly by returning 9, because they
are valid predefined types

‘REC’+’RIT’ Works perfectly by concatenating two strings

4 + ‘A’ It doesn’t works, because they are invalid types and it raises an
exception TypeError
NOTE:
 If you observe clearly the same operator (+) between integer and string is
performed without any restriction or by raising an error.
 This type of concept is called polymorphism.
 The same operator is changing its behavior based on its input type (i.e.,
between integers and strings)

Requirement

 We need operator overloading concept in order to perform operation between


( int and str )
 By extending this feature to existing + operator, generally we called as operator
overloading.
 Refer below example program, how to extend this new feature to +.

You might also like