Inheritance in Python | Python Tutorials for Beginners #lec89

Jenny's Lectures CS IT
24 Jul 202327:20

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

00:00

📘 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.

05:02

❓ 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.

10:04

🛠 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.

15:04

🎯 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.

20:06

🔄 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.

25:06

🚀 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

Inheritance is a key concept in object-oriented programming (OOP), allowing a class (child class) to inherit properties and methods from another class (parent class). In the video, the speaker uses a real-world analogy of inheriting features like appearance and behavior from parents to explain how a class can inherit attributes and methods from another. This concept saves time by reusing code rather than rewriting it.

💡Base Class

A base class, also known as a parent class or super class, is the class from which attributes and methods are inherited. In the video, the 'Human' class is used as a base class, with its methods and attributes like 'eat' and 'work' being inherited by a derived class 'Male'.

💡Derived Class

A derived class, also known as a child class or subclass, is the class that inherits attributes and methods from a base class. In the example from the video, the 'Male' class is the derived class, inheriting features from the 'Human' base class. This demonstrates how inheritance creates a hierarchy between classes.

💡Reusability

Reusability refers to the ability to use existing code again, particularly through inheritance. The video emphasizes that inheritance improves code reusability by allowing child classes to reuse the code of parent classes, avoiding redundancy and saving time. This also makes the code more maintainable.

💡Overriding

Overriding is when a derived class provides its own implementation of a method that is already defined in its base class. In the video, the 'Male' class overrides the 'work' method of the 'Human' class. Instead of 'I can work', the 'Male' class prints 'I can code', showing how a child class can customize inherited methods.

💡Super Function

The super function allows access to the methods and attributes of the base class from within the derived class. In the video, it is used to access the 'work' method from the 'Human' class while still allowing the 'Male' class to add extra functionality like printing 'I can code'. It plays a vital role in method overriding and extending inherited functionality.

💡Attributes

Attributes refer to the variables or properties that describe an object. In the video, the 'Human' class has attributes like 'num_eyes' and 'num_nose', which represent characteristics shared by humans. These attributes are inherited by the 'Male' class, showing how attributes can be passed down through inheritance.

💡Methods

Methods are functions defined within a class that describe the behaviors of an object. In the video, the 'Human' class has methods like 'eat' and 'work', which describe actions that all humans can perform. These methods are inherited by the 'Male' class, illustrating how methods are reused in inheritance.

💡Class

A class is a blueprint for creating objects in object-oriented programming. In the video, the speaker explains how classes, such as 'Human' and 'Male', are used to define attributes and methods. The class structure is central to understanding inheritance, as it allows the reuse of code across multiple classes.

💡Object

An object is an instance of a class, containing specific data and behaviors defined by the class. In the video, 'male_1' is an object of the 'Male' class, created to demonstrate how methods and attributes are accessed and used. Objects are essential in inheritance as they represent the real-world entities that are described by classes.

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

play00:00

hey everyone I hope you all are safe and

play00:02

doing good so in the series of learning

play00:03

Python programming language we are

play00:05

discussing object or programming Concept

play00:07

in Python so till now we have discussed

play00:09

classes and object concept right now the

play00:13

second thing is the second important

play00:15

concept of Hope is inheritance so that

play00:17

thing we'll see in this video what is

play00:19

inheritance and the important

play00:22

thing is why inheritance why you need

play00:25

inheritance the need the benefits of

play00:27

using this concept because whenever you

play00:30

go to A New Concept you must first clear

play00:33

this answer why you need that concept

play00:35

why you are studying that concept the

play00:37

need of that thing so the need of

play00:38

inheritance why obviously there are some

play00:40

benefits of using this concept that is

play00:42

why we are using this conception but

play00:44

what are those what are those benefits

play00:46

this thing we'll see practically with

play00:48

the help of programs right first we will

play00:51

see basically what is inheritance right

play00:53

now

play00:55

don't go too into that programming kind

play00:57

of thing in just real life we inherit

play00:59

properties from our parents right means

play01:02

if our parents are having some

play01:04

Properties or some business means

play01:05

automatically we are means that property

play01:08

or business belong to

play01:10

us right not just this we inherit like

play01:15

now uh that appearance also like my eyes

play01:19

are exactly same as my mother's eyes

play01:22

so we inherited the appearance the

play01:24

features like maybe your nose is same as

play01:26

your father's nose

play01:28

like this right maybe your height

play01:31

whether you are tall or you are you know

play01:33

sure it depends on your parents height

play01:36

like this

play01:37

right

play01:38

so each other you can say it's in our

play01:40

genes

play01:42

not only the these properties not only

play01:45

the appearance we also inherit the

play01:48

behavior of our parents

play01:50

like sometimes maybe you uh you have

play01:53

faced this kind of situation you just

play01:55

you know

play01:56

you behave in a weird way and maybe your

play01:59

mother say like father like son

play02:03

means

play02:04

or maybe your father or your parents are

play02:07

having anger issues they are short

play02:09

tempered and so you are your also are

play02:11

having anger issues you are also short

play02:13

temper

play02:14

you also react same as your parents or

play02:18

your father or your mother behave you

play02:20

know in a particular situation you also

play02:22

you know react or you also behave in

play02:24

that situation exactly same means we are

play02:27

inheriting the behavior also from our

play02:30

parents or maybe from our grandparents

play02:32

right this is I think you know and also

play02:37

you have experienced this thing and

play02:38

obviously you can uh like right now you

play02:42

can feel this kind of thing maybe right

play02:43

now you can feel this kind of thing

play02:45

right

play02:46

so this is what inheritance in real life

play02:49

fine now this concept we can implement

play02:54

in programming

play02:56

right with the help of object only

play02:58

programming there we have a concept of

play03:00

inheritance right means we can say like

play03:04

uh in programming we have classes till

play03:07

now we have discussed classes and

play03:08

objects

play03:10

so classes are just blueprints those are

play03:12

having some attributes and methods

play03:14

attributes mean means what what an

play03:16

object has are attributes what an object

play03:20

does the methods those are methods

play03:23

so in a class

play03:25

we are having these attributes and

play03:28

methods

play03:32

right now inheritance is the ability

play03:38

to inherit

play03:39

the attributes or the method so you can

play03:42

see the properties of the class or the

play03:45

features of the class already created

play03:48

class

play03:49

to a newly created class

play03:53

we can inherit all these attributes and

play03:55

methods from this class

play03:57

into another class

play04:01

so this class is

play04:03

this is basically

play04:05

from here we are inheriting these

play04:07

attributes and methods so this is known

play04:09

as base class or parent class

play04:13

or super class

play04:16

and the second class which inherit

play04:19

see which inherit these properties

play04:22

attributes and methods from this class

play04:24

this class is known as Drive class

play04:27

or child class or subclass

play04:31

right

play04:32

so this is simple you can say

play04:35

inheritance

play04:36

so we can say inheritance is the ability

play04:39

to inherit the features or the

play04:41

properties or the attributes and methods

play04:44

from an already existing classes or

play04:47

already created classes into newer

play04:50

classes that we make right rather than

play04:53

creating that class from scratch we can

play04:55

inherit these features

play04:58

okay

play04:59

now this is what inheritance but why why

play05:02

we are inheriting these attribute and

play05:04

methods we can also write down our

play05:06

attributes and methods in this class so

play05:09

why inheritance the answer of this

play05:11

question is important so if you got the

play05:13

answer of this question why inheritance

play05:15

and you understand this thing

play05:17

then means you are done almost you got

play05:20

everything about inheritance yeah we are

play05:23

studying many things like types of

play05:24

inheritance and many other things but

play05:27

the answer of this question is important

play05:28

one before proceeding further why why

play05:31

inheritance so this thing now I'll see

play05:34

you with the help of program right okay

play05:36

let me just first tell you the syntax

play05:39

before

play05:40

you just write down a name this keyword

play05:43

class then

play05:45

this derived class name

play05:49

right and in bracket we write down

play05:52

Base Class name

play05:56

and the colon and here the class

play05:58

definition

play06:00

whatever you want to Define in this

play06:02

class that definition you can write

play06:04

right

play06:06

so now here you're gonna like like class

play06:09

definition or statements

play06:12

so this is simple syntax of inheritance

play06:14

now let's see practically

play06:17

okay so let's create a new file here

play06:19

inheritance intro dot py right

play06:23

I am simply

play06:25

Define the class and the class name I'm

play06:28

taking like human

play06:31

okay now I'm defining some functions

play06:33

like eat

play06:35

and in it simply I am printing like I

play06:38

can

play06:39

eat

play06:41

and one more function suppose I am

play06:43

defining like work

play06:46

right

play06:48

and simply I am going to print here I

play06:51

can work

play06:54

so these two methods sorry not function

play06:56

these are two methods eat and

play06:59

not word its work I can work

play07:03

right now

play07:05

let's define see I'm having only right

play07:07

now methods no attributes that was we'll

play07:10

see later about attributes now I am

play07:12

having a class

play07:14

and suppose I am having a class mail

play07:18

right

play07:19

now obviously Paul male and obviously

play07:22

females are human human being

play07:24

so male can also eat we know male can

play07:28

also work right

play07:30

now one way is either in mail also you

play07:33

can Define these two functions for male

play07:35

and like this Ctrl C and

play07:39

they eat deaf work I can work I can eat

play07:42

right and simply if I like if I create

play07:46

an object of male male underscore one

play07:48

any name you can take and I'm creating

play07:51

an object of this male class

play07:54

right and with the help of this now male

play07:58

object male dot see eat and work these

play08:03

two methods are from this male and male

play08:06

class as you can see right so you can

play08:08

access these methods like if I access it

play08:10

and if you run this then obviously it

play08:13

will print

play08:14

I can eat see right but the thing is

play08:19

this is what just a waste of time

play08:23

right and just to increase the number of

play08:27

lines in our code unnecessarily because

play08:29

we are just repeating these lines these

play08:32

two things

play08:34

if you are a human obviously male or

play08:36

human so they can eat and they can work

play08:38

so rather than writing again

play08:41

I can just use these

play08:45

methods these are these are already

play08:47

defined these are already in this

play08:49

already created class so rather than

play08:50

creating this class from scratch what I

play08:53

can do

play08:54

I can just inherit these methods so this

play08:58

is what inheritance and this will

play09:00

definitely save a lot of time because

play09:03

here we are I'm only two classes and

play09:05

only two methods small example but on

play09:07

industry level there are multiple

play09:10

classes and very large project on very

play09:12

last project we are working right so

play09:15

obviously this is going to save a lot of

play09:17

time and less number of lines in our

play09:20

code so that is why if less number of

play09:21

lines then readability of our code will

play09:23

automatically increase maintainability

play09:25

as well as readability right so these

play09:28

are I guess you are getting now the

play09:30

benefits of using this concept so this

play09:32

is same the answer of why we need

play09:35

inheritance so now so the syntax I have

play09:37

told you derived class name and in

play09:39

bracket you just have to pass simply the

play09:42

base class

play09:43

from this class I am going to inherit

play09:46

these two features so these two methods

play09:48

so just write down the name of that

play09:50

class that's it now if you don't want to

play09:53

Define anything in main class obviously

play09:54

male class if we cannot leave this like

play09:56

this so just have to pass

play09:59

just a past statement means we we don't

play10:02

know what to write down in this mail

play10:03

class right now but later on we will

play10:05

Define this class also right now in mail

play10:08

now see with the help of this male

play10:11

object

play10:12

mail one

play10:13

can you access eat and walk see

play10:18

here also see the difference now method

play10:20

eat and what you can access but here see

play10:23

the name of the class is human human and

play10:27

at previous case the name of the here

play10:29

the class was male and male so from

play10:31

Human class we are accessing it and self

play10:33

so this is inheritance so now also I can

play10:36

print I can access it and yeah I can eat

play10:41

so the main purpose of inheritance is to

play10:45

increase the reusability of the code we

play10:47

are either using this code again rather

play10:49

than writing this again we are just

play10:52

using it

play10:53

right

play10:54

now

play10:56

this thing this is the simplest concept

play10:58

of inheritance the second thing is this

play11:00

derived class

play11:03

can Implement can have its own

play11:06

attributes and its own behavior

play11:09

right like we

play11:10

we can have our own behavior it's not

play11:13

like that everything the all the

play11:15

properties all the appearance properties

play11:16

and all the like our Behavior we inherit

play11:18

from our parents know

play11:20

we are having our own identity maybe

play11:23

their their you know uh

play11:26

uh their way of doing some work is

play11:28

different our way of doing our work is

play11:30

different so we can have different

play11:31

Behavior so like this the derived class

play11:33

can also have its own attribute and

play11:36

methods so right now just I'm not taking

play11:38

the attributes I'm taking just methods

play11:40

so I'm just defining a method one more

play11:43

method like maybe flood

play11:47

no offense

play11:49

it's just an example don't take it

play11:51

seriously

play11:52

so I can

play11:55

see

play11:57

these now see using this mail how many

play12:00

thing you can access male dot

play12:03

eat and work from Human class and flirt

play12:06

from male class all the three methods I

play12:09

can access right so if I access this

play12:12

slot so it will print yeah I can flirt

play12:15

right and both if I want to access like

play12:19

male 1 Dot and work

play12:22

this is also fine see I can plot I can

play12:25

work

play12:27

right so this work

play12:30

this may class is accessing from human

play12:31

and this is its own method so our own

play12:33

attribute and methods we can Define this

play12:36

is second thing

play12:37

third Point important is

play12:40

this derived class can have its own

play12:44

implementation

play12:46

of the method that has that are already

play12:49

defined in human class

play12:51

now what does that mean let's see

play12:53

practically if you're not getting my

play12:55

words see

play12:57

I am

play12:59

taking the same method work

play13:03

from Human class work right but my way

play13:06

of work is different from Human class so

play13:08

I can redefine this method here

play13:12

so my way of work is

play13:15

like maybe I'm specific I can call

play13:20

so I have just I am my I'm having my own

play13:23

implementation of this work method and

play13:26

this women class is having

play13:27

implementation of this work method I can

play13:29

work but I am having I can code

play13:31

so this particular thing is called

play13:34

overridden this is what

play13:37

override concept method over it

play13:40

overriding

play13:41

means you have overruled this method

play13:44

work method previously this but you have

play13:46

overrun this method right now if you

play13:49

want to access using this object mail

play13:50

one dot C flirt walk from male eat from

play13:55

Human

play13:56

see this work is from male you are

play13:59

accessing not from Human because I am

play14:01

having my own implementation

play14:04

of this method so if you print

play14:06

work then it will print not I can walk

play14:09

it will print I can code because you

play14:11

have overridden this method I can plot I

play14:13

can code

play14:14

right

play14:16

this is overriding concept

play14:18

okay fine now next point is

play14:21

if you want if you want to have this

play14:25

this definition also this Implement

play14:27

implementation also as well as you want

play14:29

to add something extra

play14:32

in that definition of that method

play14:36

see means I want to print I can work as

play14:39

well as I can code

play14:41

right I am not

play14:43

ah implementing I am not defining this

play14:46

method from the scratch I want the

play14:48

previous definition from the base class

play14:50

and I want to add some extra

play14:54

right

play14:55

so that what how you can do that you can

play14:58

access with the help of we have a

play15:00

function

play15:00

super

play15:04

just call this function dot what method

play15:08

see with the help of super it indicates

play15:10

that

play15:12

from Human class from Super means

play15:14

superclass we know the base class so

play15:16

Base Class is what class you have passed

play15:19

here human so this is human is what the

play15:21

base class so from Base Class you can

play15:24

access all the attributes and methods of

play15:26

that class

play15:27

right with the help of with the super

play15:30

Super Dome now work see human and eat so

play15:35

work

play15:37

sorry

play15:39

and I want to access this thing super

play15:42

dot work right

play15:44

now see let's print mail one dot work

play15:48

see I can plot and I can work I can code

play15:54

now this work definition is

play15:57

because we have accessed this

play15:59

implementation from Super so it will

play16:01

print I can work as well as now I have

play16:03

added my extra feature means one more

play16:05

statement I can code so both think it

play16:07

will print and now if you see here

play16:10

this work is from mail only not from

play16:14

human male only

play16:16

right

play16:18

so I hope you got this thing right I can

play16:21

figure out I can work I can code

play16:23

so these kind of thing you can do now

play16:26

this is about methods if suppose I am

play16:29

having attribute 2.

play16:31

so so this super class the super

play16:34

function is basically give you the

play16:36

access to all the methods and attributes

play16:38

of parent class right so let's define

play16:42

some attribute also I am having a init

play16:44

function and there

play16:46

I'm having like two attribute all the

play16:49

human are having like maybe two eyes

play16:51

maybe not not maybe yeah definitely they

play16:54

are having two eyes so numb underscore

play16:57

ions equal to I'm setting two right and

play17:02

nose

play17:05

number of no is equal to one only two

play17:08

attribute I am taking number five is the

play17:09

number of nodes so obviously male is

play17:11

also having the same thing number of

play17:13

eyes and knows two eyes and one knows

play17:15

right

play17:16

now see

play17:21

with the help of

play17:23

this male object

play17:26

now you can access and number of nose

play17:30

from Human class and number of eyes from

play17:33

Human class right so if you want to

play17:35

print Nam underscore ions

play17:37

I want to print this thing so let's

play17:39

print

play17:41

how many number of eyes uh this male

play17:43

object are having

play17:45

so let's run this and it will show you

play17:48

see I can flirt I can work I can code

play17:51

random number of eyes are two

play17:54

number five is two

play17:55

we haven't defined any attribute in mail

play17:59

but it is accessing from parent class

play18:02

like this

play18:04

and if

play18:06

rather than eyes and nose I want to

play18:09

specify one more attribute specifically

play18:11

for mail so that also you can do with

play18:13

the help of depth I want to

play18:16

initialize and like

play18:19

I want to pass one parameter name name

play18:22

of the mail

play18:23

with this class right so while calling

play18:26

this now creating object you have to

play18:27

pass name

play18:29

so I'm writing like self thought name is

play18:33

equal to

play18:34

name

play18:36

right now it will give error while

play18:39

calling this object because see

play18:42

in line number 17 while you are creating

play18:45

this object mail type error mail in it

play18:47

missing one required positional argument

play18:49

name because in defining in this

play18:52

Constructor you have to pass

play18:55

this parameter name so let's pass this

play18:58

name

play19:00

maybe Akash

play19:05

let's send this

play19:07

mail object has no attribute Namaz

play19:12

see

play19:13

no attribute

play19:15

num eyes

play19:18

why so because see last time

play19:22

in line number 20 because you are

play19:23

printing numbers and mail object is not

play19:25

having attribute namise in mail object

play19:28

now I am having only name attribute

play19:31

but see now at this point of time

play19:34

let's see let me just show you this

play19:36

thing

play19:37

if you want to print mail One Note

play19:40

then name

play19:43

Namaz are from human number of nose from

play19:46

human and maybe you think you can access

play19:49

let's access number of nose

play19:51

maybe you think it should print let's

play19:54

comment out this these lines work and

play19:57

all

play19:58

I'll just print number of nodes

play20:01

see it is not printing anything male

play20:03

object has no attribute num nose why you

play20:05

are getting this attribute because this

play20:07

is dried glass we can access all the

play20:09

attribute from here

play20:11

because now you have defined your own

play20:14

init function here

play20:16

right

play20:18

now if you want to access these

play20:20

attributes

play20:21

if you want to access these attributes

play20:23

then you have to use super function

play20:25

function so yeah we can use these

play20:30

attributes as well but for that in this

play20:33

init function in this derived class init

play20:35

function you have to call

play20:39

this super function

play20:42

Dot

play20:44

in it

play20:46

init function of super

play20:48

right

play20:50

and now that's fine

play20:53

now let me just run this see if again

play20:56

mail one don't

play20:59

name number of nodes number of eyes from

play21:02

human and work plot and these are the

play21:04

methods eat as well as from human right

play21:06

so number of nose or number of eyes I

play21:09

want to print

play21:10

let's run this and it will print number

play21:12

of eyes are two

play21:14

okay

play21:16

if I'll print the name

play21:20

mail one dot

play21:22

name so it will print

play21:25

C Akash

play21:26

right so this is what the beauty of this

play21:30

super function it will give you the

play21:32

access

play21:33

of all the attributes and methods of

play21:36

superclass

play21:37

right

play21:38

because here you have if you don't

play21:40

Define your own init function it's okay

play21:42

without any super function you can use

play21:45

this thing but if you if you have

play21:47

defined

play21:48

your own init function in that case you

play21:51

have to call Super function to access

play21:52

the attribute of

play21:54

parent class

play21:56

see the difference here right in case

play21:59

here also I want to pass one argument

play22:02

like maybe

play22:05

self Dot

play22:08

heart equal to

play22:11

foreign

play22:24

because this time this initialization is

play22:27

also having one parameter

play22:28

if you run this like this

play22:30

see

play22:31

in line number 19 because you are

play22:34

calling this Akash and in line number 12

play22:36

super init this type error women don't

play22:40

init missing one required positional

play22:41

argument num heart

play22:45

in line number 12 in line number

play22:48

12.

play22:49

so you have to pass one argument num

play22:53

hard any other name you can take it's

play22:56

not like that you have to use this

play22:58

number same name so I am calling suppose

play23:00

heart only heart

play23:03

right

play23:05

okay

play23:06

still it will give an error see now

play23:09

maybe you think we have passed one

play23:10

parameter name as well now why it is

play23:12

giving an error let's run this here

play23:15

heart is not defined as well as see this

play23:16

line line number 19

play23:18

Akash

play23:21

here also we got an error see you are

play23:26

you are accessing all the attributes

play23:28

from this human class right means one

play23:31

parameter is nam hard so rather than

play23:33

name one more you have to pass here

play23:38

one more attribute for number of heart

play23:40

to the seed number of R so hard right

play23:44

and whatever you write down the

play23:46

parameter name you have to pass the same

play23:48

parameter name in super init

play23:51

it's not compulsive that these should be

play23:53

same as this parameter this can be

play23:55

different right now it will work fine

play23:57

now it will give error because Akash as

play24:00

well as okay let's run this whatever you

play24:02

will get see

play24:04

while calling this mail one while

play24:06

creating this object this is missing one

play24:08

required positional argument r so

play24:11

obviously you have to pass your name as

play24:12

well as R so Akash comma one heart

play24:17

and if you print here

play24:21

using male 1 Dot

play24:26

see you can access now

play24:29

this attribute of human class

play24:39

foreign or if suppose I want to define a

play24:43

function here

play24:45

one more function like display and

play24:47

simply I want to print here

play24:52

using F string

play24:54

like High

play24:56

m

play24:58

name of this object

play25:00

on which you are calling so I am

play25:03

the name obviously we can

play25:06

Fetch with the help of we have a name

play25:08

attribute here name so I am self dot

play25:11

name

play25:12

and suppose I want profit I have

play25:15

number of Parts how many number of part

play25:18

so this much

play25:20

hard

play25:21

from where you can fetch the value of

play25:24

this heart

play25:26

from

play25:28

self dot C

play25:30

um hard from Human

play25:34

and other than this just call this

play25:38

display

play25:39

mail one dot

play25:42

display let's run this

play25:45

hi I'm Akash and I have one heart

play25:47

although the smell is not having any

play25:50

attribute numb heart but still we are

play25:52

using self-load numb heart because we

play25:54

are accessing this from the parent class

play25:56

the same attribute num underscore heart

play25:58

not simply hard or hard just a parameter

play26:02

name

play26:03

the attribute name is nam underscore

play26:05

Heart Like This

play26:09

so now you got the main purpose or need

play26:12

or use or you can say advantage of

play26:14

inheritance concept

play26:16

what main purpose of inheritance is

play26:19

reusability of the code because we can

play26:21

create

play26:22

a new class from already existing class

play26:26

right we can inherit all the features

play26:28

and attributes the methods from already

play26:30

created class rather than creating our

play26:33

class derived class from scratch right

play26:35

and that will definitely save a lot of

play26:38

time

play26:39

right so it enhanced the reusability of

play26:41

the code and your code would be more

play26:43

maintainable more readable

play26:45

right and as well as a child class can

play26:48

provide its own specific implementation

play26:51

of the methods that

play26:54

inherit that the class derived class

play26:56

inherit from base class right or you can

play27:00

add in child class also you can add your

play27:02

own attribute and methods right

play27:05

so now I hope you have a basic idea of

play27:08

inheritance and why we need inheritance

play27:09

some use of inheritance or you can say

play27:11

advantage of using this concept right

play27:13

now in the lecture we will see types of

play27:15

inheritance so now it's in the next

play27:17

video till then bye bye take care

Rate This

5.0 / 5 (0 votes)

Ähnliche Tags
Python BasicsObject-OrientedInheritanceCoding TutorialProgramming ConceptsCode ReusabilitySuper ClassMethod OverridingClass MethodsCoding Best Practices
Benötigen Sie eine Zusammenfassung auf Englisch?