OOPS, Classes and Objects, Inheritance and Polymorphism
OOPS, Classes and Objects, Inheritance and Polymorphism
Class
Objects
Methods
Constructors
CLASS
A class needs to be
instantiated if we want A class can be
to use the class instantiated by calling
attributes in another the class using the
class or method. class name.
OBJECTS
OBJECTS
The class is not visible to the outside world but object does
OBJECT CREATION using __init__ method
The __init__ method is similar to constructors in C++ and Java. It is run as soon as an object
of a class is instantiated. The method is useful to do any initialization you want to do with
your object.
CONSTRUCTOR
METHOD
OBJECT
ACCESSING METHOD
USING OBJECT
OOPS CONEPTS
Data
Inheritance Polymorphism
Encapsulation
Exception
Data Abstraction
Handling
INHERITANCE
INHERITANCE
• Inheritance allows us to define a class that inherits all the methods and properties from
another class.
• Parent class is the class being inherited from, also called base class.
• Child class is the class that inherits from another class, also called derived class.
Syntax of Inheritance
• class DerivedClassName(BaseClassName):
Statement-1
Statement-1
....
....
....
Statement-n
TYPES OF INHERITANCE
SINGLE INHERITANCE
When a child class inherits from one parent class, it is single inheritance.
MULTIPLE INHERITANCE
When a child class inherits from more than one parent class, it is multiple inheritance.
MULTI LEVEL INHERITANCE
When a child class inherits from other child class , it is multi-level inheritance.
HIERARCHICAL INHERITANCE
Hierarchical inheritance involves multiple inheritance from the same base or parent class.
HYBRID INHERITANCE
Hybrid inheritance involves multiple inheritance taking place in single program.
SUPER METHOD
POLYMORPHISM
POLYMORPHISM
• Polymorphism allows us to access these overridden methods and attributes that have
the same name as the parent class.
Problem statement 1
A class defines a blueprint for an object. We use the same syntax to declare objects of a class as we use
to declare variables of other basic types. For example:
Input Format
Most of the input is handled for you by the locked code in the editor.
In the void Student::input() function, you must read 5 scores from stdin and save them to your scores
instance variable.
Constraints
1 <= n <= 100
0<= examscore <= 50
Problem statement 1
Output Format
In the int Student::calculateTotalScore() function, you must return the student's total grade (the sum of
the values in scores ).
The locked code in the editor will determine how many scores are larger than Kristen's and print that
number to the console.
Problem statement 1
Sample Input
The first line contains , the number of students in Kristen's class. The n subsequent lines contain each
student’s 5 exam grades for this semester.
3
30 40 45 10 10
40 40 40 10 10
50 20 30 10 10
Sample Output: 1
Explanation
Kristen's grades are on the first line of grades. Only 1 student scored higher than her.
Problem statement 2
Write a Python class to find validity of a string of parentheses, '(', ')', '{', '}', '[' and ']. These brackets
must be closed in the correct order,
Example : here, "()" and "()[]{}" are valid but "[)", "({[)]" and "{{{" are invalid.