- OOP(object oriented programming) :
- Programming that is based around classes and instances of those classes.
- OOPs programming revolves around how these classes interact and directly affect one another.
- One of the most imp aspect of oops is INHERITENCE.
- INHERITENCE :
- It is when one class gains all the attributes of another class.
- In inheritance you ‘ve a parent class and then children classes.
That inherit features from the parent class. One of the great thing of
inheritance is that you can have as many subclasses as you want. You can
‘ve as many classes u want inherit from the same class.
Example- 1
As u can see we have 2 classes, date and time. Look closely at the
class we see object in the parenthesis and date in the argument of time.
What this means is that time inherits from date and date inherits from
class called object.
Object is the built in class provided by python. Now take a look
below, we created new date object(dt) and then we create a new time
object(tm) .
Now look we are also able to call get_date on the time object, it is
specifically because we place the date class name in the argument list
of the time class definition. So they were able to call a date method
ona time object. We can illustrate this by trying to run program without
having date as parameter in time
class.
Now time doesn’t inherit from date. We get a error.
Inheritance is simply another level of attribute lookup. Remember
when we ask a instance to give us the value of an attribute, we call a
method using object .attribute syntax, we initiating a lookup that
precedes from place to place in a particular order. As we know when we
ask a object for an attribute, it uses a lookup hierarchy. When we say
object .attribute, in this case let us say
that is attribute look up.
NOW there are 3 places where python looks for an attribute in the instance.
How to use INHERITENCE in PYTHON | Softcrayons IT Training Tutorial
No comments:
Post a Comment