-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathinheritance-1.py
41 lines (30 loc) · 937 Bytes
/
inheritance-1.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# inheritance-1.py
# The code below shows how a class can inherit from another class.
# We have two classes, `Date` and `Time`. Here `Time` inherits from
# `Date`.
# Any class inheriting from another class (also called a Parent class)
# inherits the methods and attributes from the Parent class.
# Hence, any instances created from the class `Time` can access
# the methods defined in the parent class `Date`.
class Date(object):
def get_date(self):
print("2016-05-14")
class Time(Date):
def get_time(self):
print("07:00:00")
# Creating an instance from `Date`
dt = Date()
dt.get_date() # Accesing the `get_date()` method of `Date`
print("--------")
# Creating an instance from `Time`.
tm = Time()
tm.get_time() # Accessing the `get_time()` method from `Time`.
# Accessing the `get_date() which is defined in the parent class `Date`.
tm.get_date()
'''
O/P-
2016-05-14
--------
07:00:00
2016-05-14
'''