Skip to content

Commit 08ff1d5

Browse files
Files Added
OOP basics, Inheritance, Polymorphism, Super Keyword, Methods Type in OOP
1 parent bc96109 commit 08ff1d5

File tree

5 files changed

+419
-0
lines changed

5 files changed

+419
-0
lines changed

Advance_Inhertance.py

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
"""
2+
Inheritance: An Important Concept. It's like a Parent Child Relationship. The parent Class can be used by the Child
3+
class, which means attributes as well as methods can be used by child class.
4+
5+
6+
Note: Credit Goes to Geeks for Geeks
7+
8+
Note: Learn Multilevel Inheritance and also Multiclass Inheritance.
9+
"""
10+
11+
12+
def inheritance_basics():
13+
class Person:
14+
def __init__(self, name, age):
15+
self.name = name
16+
self.age = age
17+
self.country = 'Pakistan'
18+
19+
def __str__(self):
20+
return 'Hello to Person Class'
21+
22+
class Employee(Person):
23+
# Need to assign Values otherwise through Error.
24+
def __init__(self):
25+
Person.name = 'Hassan'
26+
Person.age = '21'
27+
28+
@staticmethod
29+
def show():
30+
print('Its Employee Class')
31+
32+
# Objects / Instance
33+
emp1 = Employee()
34+
print(emp1)
35+
emp1.show()
36+
print(emp1.name)
37+
print(emp1.country) # Through error. Not recognize.
38+
39+
40+
# ====== Inheritance Super Keyword ======= #
41+
def super_keyword():
42+
class Person:
43+
def __init__(self, name, age):
44+
self.name = name # gives Hassan
45+
self.age = age
46+
47+
def __str__(self):
48+
return f'{self.name} {self.age} {self.role} {self.company} '
49+
50+
# Check if a Person is Employee or Not.
51+
def is_employee(self):
52+
return False
53+
54+
class Employee(Person):
55+
def __init__(self, name, age, role):
56+
self. company = 'Purelogics'
57+
self.Name = name # gives Ali
58+
self. role = role
59+
super().__init__('Hassan', age)
60+
61+
62+
def show(self):
63+
return f'{self.Name} {self.age} {self.role} {self.company} '
64+
65+
def is_employee(self):
66+
return True
67+
68+
# Objects / Instance
69+
emp1 = Employee('Ali', 22, 'Real Estate Agent')
70+
print(emp1.show())
71+
print(emp1)
72+
print(emp1.is_employee())
73+
74+
75+
if __name__ == '__main__':
76+
# inheritance_basics()
77+
super_keyword()

Inhertiance.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
"""
2+
Object-Oriented Programming Concepts
3+
4+
Total 5 Concepts in terms of OOP
5+
Class
6+
Objects
7+
Polymorphism
8+
Encapsulation
9+
Inheritance
10+
Data Abstraction
11+
12+
Note: Very important programming Concepts give max time to work on it.
13+
14+
15+
Inheritance: An Important Concept. It's like a Parent Child Relationship. The parent Class can be used by the Child
16+
class, which means attributes as well as methods can be used by child class.
17+
18+
"""
19+
20+
21+
def inheritance():
22+
# Parent Class
23+
class Person:
24+
def __init__(self, name, age):
25+
self.name = name
26+
self.age = age
27+
28+
# Instance Method
29+
def display(self):
30+
print(f'{self.name} {self.age}')
31+
32+
# Child Class
33+
class Student(Person):
34+
def __init__(self, name, age, roll_no, degree):
35+
super().__init__(name, age)
36+
self.roll_no = roll_no
37+
self.degree = degree
38+
39+
# Class attribute
40+
University = 'COMSATS University'
41+
42+
# Just return in __Str__ method, then print by Object.
43+
def __str__(self):
44+
return f'{self.name} {self.age} {self.roll_no} {self.degree} {self.University}'
45+
46+
std1 = Student('Hassan', 21, '114', 'BCS')
47+
48+
std1.display()
49+
50+
print(std1)
51+
52+
53+
if __name__ == '__main__':
54+
inheritance()

Method_Types_OOP.py

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
"""
2+
Object-Oriented Programming Concepts
3+
4+
Total 5 Concepts in terms of OOP
5+
Class
6+
Objects
7+
Polymorphism
8+
Encapsulation
9+
Inheritance
10+
Data Abstraction
11+
12+
Note: Very important programming Concepts give max time to work on it.
13+
14+
Types of Methods in OOP:
15+
16+
1- Instance Methods:
17+
Through which perform getter|setter operations and also can do computation in it.
18+
19+
2- Class Methods:
20+
Used for the class variables to return them. Also, we need to declare a decorator for this type of method.
21+
@classmethod
22+
23+
3- Static Methods:
24+
Methods which return nothing's, just used to print somthing like message.
25+
Note: it doesn't use self keyword as it not take attributes/properties.
26+
@staticmethod
27+
28+
4- Special Methods:
29+
Methods like __str__(), which have some specific purpose are special methods.
30+
Constructor -> __init__(self) is also a special method
31+
32+
"""
33+
34+
35+
def class_methods():
36+
class Student:
37+
def __init__(self, name, roll_no):
38+
self.name = name
39+
self.roll_no = roll_no
40+
self.gpa = dict()
41+
42+
# class variable
43+
University = 'COMSATS University'
44+
45+
# Class method
46+
@classmethod
47+
def university(cls):
48+
return cls.University
49+
50+
# Instance variable
51+
def compute_gpa(self, subject, gpa):
52+
update_dict = {f'{subject}': gpa}
53+
gpa = self.gpa.update(update_dict)
54+
55+
return gpa
56+
57+
# static Method
58+
@staticmethod
59+
def message():
60+
print('This Class represents a individual Student')
61+
62+
# Instance Method
63+
def display(self):
64+
print(f'{self.name}, {self.roll_no}, {self.University}, {self.gpa}')
65+
66+
# __str__() Method (Preferred Way of Display)
67+
def __str__(self):
68+
return f'{self.name}, {self.roll_no}, {self.University}, {self.gpa}'
69+
70+
# Declaring Objects Here
71+
std1 = Student('Hassan', 'SP20-BCS-114')
72+
std1.compute_gpa('DSA', '3')
73+
std1.compute_gpa('DIP', '3.3')
74+
75+
print(std1.university())
76+
print(std1.__class__.University)
77+
78+
# std1.display()
79+
print(std1)
80+
81+
82+
if __name__ == '__main__':
83+
class_methods()

Oop_Basic.py

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
"""
2+
Object-Oriented Programming Concepts
3+
4+
There are two-way of writing code
5+
1- Functional Programming
6+
2- OOP
7+
8+
Note: Python Build-in Functionalities are develop using OOP.
9+
10+
Total 6 Concepts in terms of OOP
11+
Class
12+
Objects
13+
Polymorphism
14+
Encapsulation
15+
Inheritance
16+
Data Abstraction
17+
18+
Note: Very important programming Concepts, give max time to work on it.
19+
20+
Important Points:
21+
1- One class have many instances called Objects.
22+
2- Attributes|Property, Behavior, Methods.
23+
3- Attributes define by variables and behavior by methods.
24+
4- Self is used to represent an object in a method.
25+
5- __Init__ is a method for defining Attribute for objects, it's like constructor.
26+
27+
28+
Note: A class is a collection of objects.
29+
"""
30+
31+
32+
def python_build_in():
33+
a = 10
34+
print(type(a)) # Shows variable 'a' of class <int>
35+
36+
37+
def basic_class_concept():
38+
class Student:
39+
# Class Attribute
40+
university = 'COMSATS, Lahore Campus'
41+
42+
# Creating an Object
43+
hassan = Student()
44+
print(hassan.university) # Not prefer way.
45+
print(hassan.__class__.university) # clarify more (prefer).
46+
47+
48+
def class_concept_instance_attr():
49+
class Student:
50+
# declaring instance Attr using Constructor
51+
def __init__(self, reg_no):
52+
self.reg_no = reg_no
53+
54+
# Instance Method
55+
def display(self):
56+
return self.reg_no
57+
58+
# Class Attribute
59+
university = 'COMSATS, Lahore Campus'
60+
61+
# Creating an Object
62+
# pass instance attribute value.
63+
hassan = Student('SP20-BCS-114')
64+
65+
print(hassan.__class__.university)
66+
# Instance Attr
67+
print(hassan.reg_no)
68+
69+
print(hassan.display())
70+
print(Student.display(hassan)) # Work Same.
71+
print(type(hassan))
72+
73+
74+
def class_advance_concept():
75+
class Person:
76+
def __init__(self, name, age):
77+
self.name = name
78+
self.age = age
79+
80+
def compare(self, other):
81+
return True if self.age == other.age else False
82+
83+
def update(self):
84+
self.age = 22
85+
86+
p1 = Person('Hassan', 21)
87+
p2 = Person('Hamid', 20)
88+
89+
print('Both are same') if p1.compare(p2) else print('Not same')
90+
91+
p2.update()
92+
print(p1.name + "\n" + p2.name)
93+
print(f"{p2.age}")
94+
95+
96+
# Variables Type in OOP (instance or (static or class)
97+
def variables_oop():
98+
class Car:
99+
def __init__(self):
100+
self.Company = 'Toyota'
101+
self.model = 'Corolla'
102+
103+
# Class variable.
104+
seater = 5
105+
106+
# Method of display
107+
def display(self):
108+
print(self.Company + '\n' + self.model + '\n' + self.seater + ' ')
109+
return ''
110+
111+
# you can change variables inside class using object.
112+
car1 = Car()
113+
car1.Company = 'Honda'
114+
car1.model = 'BRV'
115+
car1.seater = '7'
116+
117+
print(car1.display())
118+
119+
120+
if __name__ == '__main__':
121+
variables_oop()

0 commit comments

Comments
 (0)