Inheritance in Python | Python Tutorials for Beginners #lec89
Summary
TLDRThis video introduces the concept of inheritance in Python, explaining its importance in object-oriented programming. The instructor discusses how inheritance allows a class (child class) to acquire attributes and methods from another class (parent class), similar to how humans inherit traits from their parents. The video highlights the efficiency inheritance brings, reducing redundant code and increasing maintainability. Key points include class syntax, method overriding, and accessing parent class methods using the `super()` function. The session concludes by emphasizing how inheritance promotes code reusability, clarity, and customization in Python programming.
Takeaways
- 🧬 Inheritance in Python is analogous to inheriting traits from parents in real life, where properties, appearances, and behaviors are passed down.
- 🏗️ In object-oriented programming, inheritance allows a class (child/derived) to inherit attributes and methods from another class (parent/base/super), promoting code reusability.
- 🔑 The base class is the class being inherited from, while the derived class is the new class that inherits properties and behaviors.
- 📝 The syntax for inheritance in Python involves using the `class` keyword, followed by the derived class name and the base class name in parentheses.
- 🛠️ Inheritance reduces redundancy by allowing methods and attributes from an existing class to be used in a new class without rewriting them.
- 🔄 Derived classes can have their own unique attributes and methods, in addition to those inherited from the base class.
- 🔧 Method overriding allows a derived class to provide a specific implementation of a method that is already defined in the base class.
- 🔄 The `super()` function is used in Python to access methods and attributes of the base class, including calling the base class's constructor.
- 🔑 If a derived class defines its own constructor, it must call the base class's constructor using `super().__init__()` to ensure proper initialization.
- 📈 Inheritance enhances code maintainability and readability by allowing for a clear hierarchy and structure in the codebase, making it easier to manage and understand.
Q & A
What is inheritance in Python?
-Inheritance in Python is the ability of a class to inherit properties, methods, and attributes from another class. The class from which properties are inherited is called the base or parent class, while the class that inherits is called the derived or child class.
Why do we use inheritance in Python?
-Inheritance is used to increase the reusability of code. Instead of writing duplicate code in different classes, a derived class can inherit methods and attributes from a base class, making the code more efficient, readable, and maintainable.
How does inheritance relate to real life?
-In real life, inheritance refers to inheriting traits or properties from parents, like physical appearance or behavior. In programming, inheritance allows one class to inherit properties and behaviors (methods and attributes) from another class, similar to how a child inherits traits from parents.
What is the basic syntax for inheritance in Python?
-The syntax for inheritance in Python involves defining a derived class and passing the base class as a parameter within parentheses. For example: `class DerivedClass(BaseClass):` followed by the class definition.
What are the benefits of using inheritance in large-scale projects?
-In large-scale projects, inheritance helps save time by reducing redundancy, lowering the number of lines of code, improving readability, and enhancing maintainability. It also promotes code reuse, which is vital for managing large projects with multiple classes.
What is method overriding in Python?
-Method overriding occurs when a derived class defines its own version of a method that is already defined in its base class. This allows the derived class to provide specific behavior for the method while keeping the same method name.
How can you call the base class method from a derived class in Python?
-You can call the base class method from a derived class by using the `super()` function. This function gives access to the methods and attributes of the base class, allowing the derived class to extend or modify the behavior of the base class method.
What is the purpose of the 'super()' function in inheritance?
-The `super()` function in Python is used to call methods or access attributes from the base class. It allows the derived class to inherit functionality from the base class without redefining it and can be used to extend or add new behavior to existing methods.
Can a derived class have its own attributes and methods in Python?
-Yes, a derived class can have its own attributes and methods in addition to the ones it inherits from the base class. This allows the derived class to add specific functionality or properties while still using the inherited features.
What happens if a derived class defines an '__init__' method but also wants to use the base class's '__init__' method?
-If a derived class defines its own `__init__` method but also wants to use the base class's `__init__`, it needs to call the base class's `__init__` method using `super().__init__()`. This ensures that the base class’s initialization is also performed.
Outlines
📘 Introduction to Python Inheritance
The speaker introduces the concept of inheritance in Python by relating it to real-life inheritance from parents, such as inheriting properties, physical traits, and behaviors. The speaker explains how this real-life concept translates into object-oriented programming, where classes inherit attributes and methods from other classes. The base class (or parent class) passes down its features to the derived class (or child class), and this forms the foundation of inheritance in Python.
❓ Why Use Inheritance?
This section focuses on why inheritance is important in programming. The speaker emphasizes the need to understand the purpose of inheritance before diving into its implementation. By using inheritance, code can be reused, which saves time and effort, especially in large projects. Inheritance reduces redundancy by allowing a derived class to inherit attributes and methods from a base class instead of rewriting them from scratch. This increases code readability and maintainability.
🛠 Practical Example of Inheritance Syntax
The speaker explains the syntax of inheritance with a practical example. A 'Human' class is created with methods like `eat` and `work`. A second class, 'Male', is shown to inherit these methods from the 'Human' class. The speaker highlights that instead of duplicating code in the derived class, we can reuse the methods from the base class through inheritance. This makes the code shorter and more efficient.
🎯 Inheritance in Action: Accessing Base Class Methods
In this paragraph, the speaker demonstrates how the 'Male' class can access methods from the 'Human' class using inheritance. The importance of reusability is stressed, as it reduces lines of code and increases efficiency. The speaker also explains that the derived class can have its own unique methods in addition to those inherited from the base class, showcasing flexibility in class design.
🔄 Overriding Methods in Derived Class
The speaker introduces the concept of method overriding, where the derived class can redefine a method that exists in the base class. In the example, the `work` method is overridden in the 'Male' class to represent a specific behavior (`I can code`), different from the general behavior defined in the 'Human' class (`I can work`). This demonstrates how derived classes can customize or specialize inherited methods.
🚀 Using the Super() Function for Enhanced Inheritance
Here, the speaker introduces the `super()` function, which allows a derived class to access methods and attributes of the base class while also adding additional functionality. An example is provided where the `work` method from the base class is extended in the derived class to include both the base class’s behavior and the new behavior. This approach is useful for enhancing inherited methods without completely overriding them.
Mindmap
Keywords
💡Inheritance
💡Base Class
💡Derived Class
💡Reusability
💡Overriding
💡Super Function
💡Attributes
💡Methods
💡Class
💡Object
Highlights
Introduction to object-oriented programming (OOP) concepts in Python, focusing on classes and objects.
Introduction to inheritance: why it is needed and its benefits in OOP.
Inheritance in real life: explaining how children inherit properties and behaviors from their parents as an analogy for inheritance in programming.
Basic explanation of inheritance in Python: inheriting attributes and methods from an already existing class to a newly created class.
Clear distinction between base (parent) class and derived (child) class in inheritance.
Importance of reusability: inheritance reduces code repetition by reusing methods and attributes from parent classes.
Demonstrating the syntax of inheritance with an example: using the 'class' keyword, followed by the derived class name and the base class in parentheses.
Practical example: creating a 'Human' class with methods 'eat' and 'work', and inheriting them in a 'Male' class.
How inheritance reduces code redundancy: saving time and improving readability by avoiding repeated method definitions.
Overriding methods: derived classes can have their own specific implementation of methods inherited from the base class.
Using the 'super' function to call methods from the base class in addition to defining new behavior in the derived class.
Attributes in inheritance: derived classes can access attributes of the base class using the 'super' function.
Explaining the purpose of the 'init' method in classes and how it initializes object attributes in both base and derived classes.
Handling errors when missing required attributes in the constructor method and how to pass parameters using the 'super' function.
Summary of key advantages of inheritance: code reusability, readability, maintainability, and extending functionality with minimal effort.
Transcripts
hey everyone I hope you all are safe and
doing good so in the series of learning
Python programming language we are
discussing object or programming Concept
in Python so till now we have discussed
classes and object concept right now the
second thing is the second important
concept of Hope is inheritance so that
thing we'll see in this video what is
inheritance and the important
thing is why inheritance why you need
inheritance the need the benefits of
using this concept because whenever you
go to A New Concept you must first clear
this answer why you need that concept
why you are studying that concept the
need of that thing so the need of
inheritance why obviously there are some
benefits of using this concept that is
why we are using this conception but
what are those what are those benefits
this thing we'll see practically with
the help of programs right first we will
see basically what is inheritance right
now
don't go too into that programming kind
of thing in just real life we inherit
properties from our parents right means
if our parents are having some
Properties or some business means
automatically we are means that property
or business belong to
us right not just this we inherit like
now uh that appearance also like my eyes
are exactly same as my mother's eyes
so we inherited the appearance the
features like maybe your nose is same as
your father's nose
like this right maybe your height
whether you are tall or you are you know
sure it depends on your parents height
like this
right
so each other you can say it's in our
genes
not only the these properties not only
the appearance we also inherit the
behavior of our parents
like sometimes maybe you uh you have
faced this kind of situation you just
you know
you behave in a weird way and maybe your
mother say like father like son
means
or maybe your father or your parents are
having anger issues they are short
tempered and so you are your also are
having anger issues you are also short
temper
you also react same as your parents or
your father or your mother behave you
know in a particular situation you also
you know react or you also behave in
that situation exactly same means we are
inheriting the behavior also from our
parents or maybe from our grandparents
right this is I think you know and also
you have experienced this thing and
obviously you can uh like right now you
can feel this kind of thing maybe right
now you can feel this kind of thing
right
so this is what inheritance in real life
fine now this concept we can implement
in programming
right with the help of object only
programming there we have a concept of
inheritance right means we can say like
uh in programming we have classes till
now we have discussed classes and
objects
so classes are just blueprints those are
having some attributes and methods
attributes mean means what what an
object has are attributes what an object
does the methods those are methods
so in a class
we are having these attributes and
methods
right now inheritance is the ability
to inherit
the attributes or the method so you can
see the properties of the class or the
features of the class already created
class
to a newly created class
we can inherit all these attributes and
methods from this class
into another class
so this class is
this is basically
from here we are inheriting these
attributes and methods so this is known
as base class or parent class
or super class
and the second class which inherit
see which inherit these properties
attributes and methods from this class
this class is known as Drive class
or child class or subclass
right
so this is simple you can say
inheritance
so we can say inheritance is the ability
to inherit the features or the
properties or the attributes and methods
from an already existing classes or
already created classes into newer
classes that we make right rather than
creating that class from scratch we can
inherit these features
okay
now this is what inheritance but why why
we are inheriting these attribute and
methods we can also write down our
attributes and methods in this class so
why inheritance the answer of this
question is important so if you got the
answer of this question why inheritance
and you understand this thing
then means you are done almost you got
everything about inheritance yeah we are
studying many things like types of
inheritance and many other things but
the answer of this question is important
one before proceeding further why why
inheritance so this thing now I'll see
you with the help of program right okay
let me just first tell you the syntax
before
you just write down a name this keyword
class then
this derived class name
right and in bracket we write down
Base Class name
and the colon and here the class
definition
whatever you want to Define in this
class that definition you can write
right
so now here you're gonna like like class
definition or statements
so this is simple syntax of inheritance
now let's see practically
okay so let's create a new file here
inheritance intro dot py right
I am simply
Define the class and the class name I'm
taking like human
okay now I'm defining some functions
like eat
and in it simply I am printing like I
can
eat
and one more function suppose I am
defining like work
right
and simply I am going to print here I
can work
so these two methods sorry not function
these are two methods eat and
not word its work I can work
right now
let's define see I'm having only right
now methods no attributes that was we'll
see later about attributes now I am
having a class
and suppose I am having a class mail
right
now obviously Paul male and obviously
females are human human being
so male can also eat we know male can
also work right
now one way is either in mail also you
can Define these two functions for male
and like this Ctrl C and
they eat deaf work I can work I can eat
right and simply if I like if I create
an object of male male underscore one
any name you can take and I'm creating
an object of this male class
right and with the help of this now male
object male dot see eat and work these
two methods are from this male and male
class as you can see right so you can
access these methods like if I access it
and if you run this then obviously it
will print
I can eat see right but the thing is
this is what just a waste of time
right and just to increase the number of
lines in our code unnecessarily because
we are just repeating these lines these
two things
if you are a human obviously male or
human so they can eat and they can work
so rather than writing again
I can just use these
methods these are these are already
defined these are already in this
already created class so rather than
creating this class from scratch what I
can do
I can just inherit these methods so this
is what inheritance and this will
definitely save a lot of time because
here we are I'm only two classes and
only two methods small example but on
industry level there are multiple
classes and very large project on very
last project we are working right so
obviously this is going to save a lot of
time and less number of lines in our
code so that is why if less number of
lines then readability of our code will
automatically increase maintainability
as well as readability right so these
are I guess you are getting now the
benefits of using this concept so this
is same the answer of why we need
inheritance so now so the syntax I have
told you derived class name and in
bracket you just have to pass simply the
base class
from this class I am going to inherit
these two features so these two methods
so just write down the name of that
class that's it now if you don't want to
Define anything in main class obviously
male class if we cannot leave this like
this so just have to pass
just a past statement means we we don't
know what to write down in this mail
class right now but later on we will
Define this class also right now in mail
now see with the help of this male
object
mail one
can you access eat and walk see
here also see the difference now method
eat and what you can access but here see
the name of the class is human human and
at previous case the name of the here
the class was male and male so from
Human class we are accessing it and self
so this is inheritance so now also I can
print I can access it and yeah I can eat
so the main purpose of inheritance is to
increase the reusability of the code we
are either using this code again rather
than writing this again we are just
using it
right
now
this thing this is the simplest concept
of inheritance the second thing is this
derived class
can Implement can have its own
attributes and its own behavior
right like we
we can have our own behavior it's not
like that everything the all the
properties all the appearance properties
and all the like our Behavior we inherit
from our parents know
we are having our own identity maybe
their their you know uh
uh their way of doing some work is
different our way of doing our work is
different so we can have different
Behavior so like this the derived class
can also have its own attribute and
methods so right now just I'm not taking
the attributes I'm taking just methods
so I'm just defining a method one more
method like maybe flood
no offense
it's just an example don't take it
seriously
so I can
see
these now see using this mail how many
thing you can access male dot
eat and work from Human class and flirt
from male class all the three methods I
can access right so if I access this
slot so it will print yeah I can flirt
right and both if I want to access like
male 1 Dot and work
this is also fine see I can plot I can
work
right so this work
this may class is accessing from human
and this is its own method so our own
attribute and methods we can Define this
is second thing
third Point important is
this derived class can have its own
implementation
of the method that has that are already
defined in human class
now what does that mean let's see
practically if you're not getting my
words see
I am
taking the same method work
from Human class work right but my way
of work is different from Human class so
I can redefine this method here
so my way of work is
like maybe I'm specific I can call
so I have just I am my I'm having my own
implementation of this work method and
this women class is having
implementation of this work method I can
work but I am having I can code
so this particular thing is called
overridden this is what
override concept method over it
overriding
means you have overruled this method
work method previously this but you have
overrun this method right now if you
want to access using this object mail
one dot C flirt walk from male eat from
Human
see this work is from male you are
accessing not from Human because I am
having my own implementation
of this method so if you print
work then it will print not I can walk
it will print I can code because you
have overridden this method I can plot I
can code
right
this is overriding concept
okay fine now next point is
if you want if you want to have this
this definition also this Implement
implementation also as well as you want
to add something extra
in that definition of that method
see means I want to print I can work as
well as I can code
right I am not
ah implementing I am not defining this
method from the scratch I want the
previous definition from the base class
and I want to add some extra
right
so that what how you can do that you can
access with the help of we have a
function
super
just call this function dot what method
see with the help of super it indicates
that
from Human class from Super means
superclass we know the base class so
Base Class is what class you have passed
here human so this is human is what the
base class so from Base Class you can
access all the attributes and methods of
that class
right with the help of with the super
Super Dome now work see human and eat so
work
sorry
and I want to access this thing super
dot work right
now see let's print mail one dot work
see I can plot and I can work I can code
now this work definition is
because we have accessed this
implementation from Super so it will
print I can work as well as now I have
added my extra feature means one more
statement I can code so both think it
will print and now if you see here
this work is from mail only not from
human male only
right
so I hope you got this thing right I can
figure out I can work I can code
so these kind of thing you can do now
this is about methods if suppose I am
having attribute 2.
so so this super class the super
function is basically give you the
access to all the methods and attributes
of parent class right so let's define
some attribute also I am having a init
function and there
I'm having like two attribute all the
human are having like maybe two eyes
maybe not not maybe yeah definitely they
are having two eyes so numb underscore
ions equal to I'm setting two right and
nose
number of no is equal to one only two
attribute I am taking number five is the
number of nodes so obviously male is
also having the same thing number of
eyes and knows two eyes and one knows
right
now see
with the help of
this male object
now you can access and number of nose
from Human class and number of eyes from
Human class right so if you want to
print Nam underscore ions
I want to print this thing so let's
how many number of eyes uh this male
object are having
so let's run this and it will show you
see I can flirt I can work I can code
random number of eyes are two
number five is two
we haven't defined any attribute in mail
but it is accessing from parent class
like this
and if
rather than eyes and nose I want to
specify one more attribute specifically
for mail so that also you can do with
the help of depth I want to
initialize and like
I want to pass one parameter name name
of the mail
with this class right so while calling
this now creating object you have to
pass name
so I'm writing like self thought name is
equal to
name
right now it will give error while
calling this object because see
in line number 17 while you are creating
this object mail type error mail in it
missing one required positional argument
name because in defining in this
Constructor you have to pass
this parameter name so let's pass this
name
maybe Akash
let's send this
mail object has no attribute Namaz
see
no attribute
num eyes
why so because see last time
in line number 20 because you are
printing numbers and mail object is not
having attribute namise in mail object
now I am having only name attribute
but see now at this point of time
let's see let me just show you this
thing
if you want to print mail One Note
then name
Namaz are from human number of nose from
human and maybe you think you can access
let's access number of nose
maybe you think it should print let's
comment out this these lines work and
all
I'll just print number of nodes
see it is not printing anything male
object has no attribute num nose why you
are getting this attribute because this
is dried glass we can access all the
attribute from here
because now you have defined your own
init function here
right
now if you want to access these
attributes
if you want to access these attributes
then you have to use super function
function so yeah we can use these
attributes as well but for that in this
init function in this derived class init
function you have to call
this super function
Dot
in it
init function of super
right
and now that's fine
now let me just run this see if again
mail one don't
name number of nodes number of eyes from
human and work plot and these are the
methods eat as well as from human right
so number of nose or number of eyes I
want to print
let's run this and it will print number
of eyes are two
okay
if I'll print the name
mail one dot
name so it will print
C Akash
right so this is what the beauty of this
super function it will give you the
access
of all the attributes and methods of
superclass
right
because here you have if you don't
Define your own init function it's okay
without any super function you can use
this thing but if you if you have
defined
your own init function in that case you
have to call Super function to access
the attribute of
parent class
see the difference here right in case
here also I want to pass one argument
like maybe
self Dot
heart equal to
foreign
because this time this initialization is
also having one parameter
if you run this like this
see
in line number 19 because you are
calling this Akash and in line number 12
super init this type error women don't
init missing one required positional
argument num heart
in line number 12 in line number
12.
so you have to pass one argument num
hard any other name you can take it's
not like that you have to use this
number same name so I am calling suppose
heart only heart
right
okay
still it will give an error see now
maybe you think we have passed one
parameter name as well now why it is
giving an error let's run this here
heart is not defined as well as see this
line line number 19
Akash
here also we got an error see you are
you are accessing all the attributes
from this human class right means one
parameter is nam hard so rather than
name one more you have to pass here
one more attribute for number of heart
to the seed number of R so hard right
and whatever you write down the
parameter name you have to pass the same
parameter name in super init
it's not compulsive that these should be
same as this parameter this can be
different right now it will work fine
now it will give error because Akash as
well as okay let's run this whatever you
will get see
while calling this mail one while
creating this object this is missing one
required positional argument r so
obviously you have to pass your name as
well as R so Akash comma one heart
and if you print here
using male 1 Dot
see you can access now
this attribute of human class
foreign or if suppose I want to define a
function here
one more function like display and
simply I want to print here
using F string
like High
m
name of this object
on which you are calling so I am
the name obviously we can
Fetch with the help of we have a name
attribute here name so I am self dot
name
and suppose I want profit I have
number of Parts how many number of part
so this much
hard
from where you can fetch the value of
this heart
from
self dot C
um hard from Human
and other than this just call this
display
mail one dot
display let's run this
hi I'm Akash and I have one heart
although the smell is not having any
attribute numb heart but still we are
using self-load numb heart because we
are accessing this from the parent class
the same attribute num underscore heart
not simply hard or hard just a parameter
name
the attribute name is nam underscore
Heart Like This
so now you got the main purpose or need
or use or you can say advantage of
inheritance concept
what main purpose of inheritance is
reusability of the code because we can
create
a new class from already existing class
right we can inherit all the features
and attributes the methods from already
created class rather than creating our
class derived class from scratch right
and that will definitely save a lot of
time
right so it enhanced the reusability of
the code and your code would be more
maintainable more readable
right and as well as a child class can
provide its own specific implementation
of the methods that
inherit that the class derived class
inherit from base class right or you can
add in child class also you can add your
own attribute and methods right
so now I hope you have a basic idea of
inheritance and why we need inheritance
some use of inheritance or you can say
advantage of using this concept right
now in the lecture we will see types of
inheritance so now it's in the next
video till then bye bye take care
تصفح المزيد من مقاطع الفيديو ذات الصلة
5.0 / 5 (0 votes)