Python Tutorial - 18. Inheritance

codebasics · Beginner ·🛠️ AI Tools & Apps ·10y ago

Key Takeaways

Inheritance in Python, deriving classes, creating objects, and implementing inheritance in Python programming

Full Transcript

hello and welcome to Cod Basics coding tutorial today we are going to cover a fun topic called inheritance and here is the list of items we are covering in this video okay let's understand inheritance by taking the example of vehicle when you think about Vehicles you have uh so many vehicles available for use such as car trucks motorcycle Etc but they provide a same purpose which is transportation and when it when you go to specific type of vehicles such as car and motorcycle uh they have their own purposes and own characteristic for example our car has four wheels it has no roof it has a roof whereas motorcycle has only two wheels and it has got no roof car also has specific usages such as commute to work and vacation with family whereas motorcycle is primarily used for road trip and racing so as shown in this diagram both of these vehicles cars and motorcycle can be thought of as subclasses of a base class called vehicle and they share the same properties of a vehicle class which is to provide transport ation but on top of that same shared property they have their own specific characteristics and purposes such as Wheels roof difference and specific usages so here you can say that we have derived car and motorcycle subclass from a class vehicle or you can also say that these two classes inherit from a vehicle class so that's essentially the definition of inheritance here now what we're going to do next is we are going to implement this same example in Python so I have a python uh py Cham code editor open here and I'm going to write uh our first class which which is vehicle now if you remember from my last video uh the way you write classes is using a class keyword followed by the name of the class and column and then you start typing your uh methods so here I'm going to create one method in my vehicle class which is the general usage and the general usage is I'm going to say okay the general use of vehicle car vehicle class is to provide Transportation now I'm going to derive my first subass called car from vehicle and the way you inherit a class from another class is by writing the name of that class in in bracket like this so here it means my car class is derived from vehicle class or it's inherited from a vehicle class here I'm going to write an init method which is same as a Constructor and the first thing I'm going to do is I'm going to say okay I am car okay and I'm going to now Define certain properties such as wheel is 4 and has roof equal to True okay the second method I'm going to write is specific usage so car has got some specific usage uh which is uh commute to work vacation with family Etc okay so this is how I derive my class from another class okay now let's let's write our second class which is motorcycle and this is again deriv from a vehicle class and I'm it's essentially I'm just going to copy this code here and here I'm going to say okay I'm M motorcycle and the specific use will be different than that of the car and the specific use here is road trip and racing okay so we just derived two classes motor and car from vehicle now let's create some objects of it and let's try to realize like what real benefit this inheritance thing is giving us so the first object is the car object so I'm going to say C is equal to car and first print c. General Usage Now notice that car class doesn't have a method called General usage and still I can call that method why because I have inherited that method from vehicle class here here since this vehicle since this car class is derived from vehicle class I have an access to use all the methods and properties from my parent class okay so think about this being a parent and these two are its children okay so C do General usage and C do specific usage okay now let's first execute this and see what happens so when I run this here it is saying I'm am car so when I created the class object here it executed this Constructor and that's why it is saying I am car then the second thing I did is c. General usage so c. General usage was Transportation so as you see here it says general usage transportation and that's what it printed here and when I say c. specific usage it is going to print the specific usage as shown here which is commute to work and vacation with family so you can confirm that that's what it printed okay now let's create a an object of class motorcycle so here I'm just creating an object of motorcycle class and again I'm going to print the general usage and the specific usage so when I run this what happens here is it says I'm motorcycle my general usage is transportation and my specific use is road trip and racing okay so key takeaway from this exercise is you can call a method or a property on your from your parent class using the object of your derived class so these car and motorcycle are derived classes C and M are objects of derived classes and using those objects I calling this General usage method of my parent class and then you can call the specific methods from those sub classes now if you don't want to call this usage property um method explicitly you can call that within uh your specific usage um specific usage method so here in specific usage method I can say self. General usage so remember self is an object of car class and since it is derived from vehicle you'll be able to call General usage here and same thing you can do in motorcycle class self. General usage okay so here I'm creating an instance of car and motorcycle class and just calling specific usage method so if I execute this I get the same output as what we saw previously so it calls specific usage meth uh method then it comes here it calls General usage method which is this guy and then it calls the specific usage code okay that was that that was a simple way to illustrate inheritance using vehicle uh example if you have any doubts or any questions please post in the comments uh below uh the next uh topic next section we covering is the benefits of inheritance the there are three primary benefits of inheritance number one is code re reuse as you saw in this example of vehicle you can reuse the code of your parent class so let's say uh when you're programming in Python and someone has return a class already and you want to use all the code in that class but on to on top of that you want to do some customizations of your own then using inheritance you can do that easily that's why it provides a code reuse the second benefit is extensibility uh and it kind of overlaps with this first benefit that we just talked about uh in vehicle example we extended our vehicle class to derive new classes such as Scar and motorcycle the third benefit is readability uh it provides a very nice structure to your program especially when you're writing a big program you want your your code to be such that it remains readable because if the code is simple few lines it's easy to read but if the code is less million line of code it's very hard to read it uh for troubleshooting purpose and for developing new features uh using inheritance you will be able to gain this readability as it will provide a nice and uniform structure to your program okay one last thing we want to quickly cover is is is instance and is subass methods so here uh in the same example what I'm going to do is uh so I just created two class objects I'm I'm not going to call any method here uh so I have car instance and motorcycle instance and let me first call is instance method so is instance is a built-in function available in Python and what it will do is it will tell you if an object is an instance of a specific class or not for example here C is an instance of our car class so when I run this and let me just print this okay when I run this uh it is saying I'm car I'm motorcyle true so this the execution of this resulted into true which means C is indeed an object of class but if I do motorcycle here it will say false because C is not an object of motorcycle class okay so that is is instance now there is another method called e subplus what it means is if car is a subass of vehicle or not so you want to figure out if one class is a subass of another then just use is subclass Method when you run this you get true because car is a subass of vehicle but if I say car is a subass of motorcycle then it's going to print false because car is not derived from motorcycle you know car is more like a sibling of motorcycle uh it's not a child of MOT motorcycle okay so that was all about inheritance thank you for watching

Original Description

Video without background music: https://youtu.be/Z7D9yv21tig In today’s python tutorial we will discuss “inheritance”. The video will give insights regarding what is an inheritance, how to implement inheritance in python, how to derive a class from another class, how to create objects of class and the benefits of inheritance. Topics that are covered in this Python Video: 0:00 Introduction 0:13 what is Inheritance 2:05 Implement Inheritance in python 3:01 derive a class from another class 5:29 create objects of class 9:50 benefits of inheritance (code reuse, extensibility, readability ) Code used in this tutorial: https://github.com/codebasics/py/blob/master/Basics/18_inheritance.py Do you want to learn technology from me? Check https://codebasics.io/ for my affordable video courses. Next Video: Python Tutorial - 19. Multiple Inheritance: https://www.youtube.com/watch?v=4CyTWTFn6JY&list=PLeo1K3hjS3usILfyvQlvUBokXkHPSve6S&index=21 Website: https://codebasics.io/ Facebook: https://www.facebook.com/codebasicshub Twitter: https://twitter.com/codebasicshub
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from codebasics · codebasics · 27 of 60

1 Python Tutorial - 1. Install python on windows
Python Tutorial - 1. Install python on windows
codebasics
2 Python Tutorial - 2. Variables
Python Tutorial - 2. Variables
codebasics
3 Python Tutorial - 3. Numbers
Python Tutorial - 3. Numbers
codebasics
4 Python Tutorial - 4. Strings
Python Tutorial - 4. Strings
codebasics
5 Python Tutorial - 5. Lists
Python Tutorial - 5. Lists
codebasics
6 Python Tutorial - 6. Install PyCharm on Windows
Python Tutorial - 6. Install PyCharm on Windows
codebasics
7 PyCharm Tutorial - 7. Debug python code using PyCharm
PyCharm Tutorial - 7. Debug python code using PyCharm
codebasics
8 Python Tutorial -  8. If Statement
Python Tutorial - 8. If Statement
codebasics
9 Python Tutorial - 9. For loop
Python Tutorial - 9. For loop
codebasics
10 Python Tutorial -  10. Functions
Python Tutorial - 10. Functions
codebasics
11 Python Tutorial - 11. Dictionaries and Tuples
Python Tutorial - 11. Dictionaries and Tuples
codebasics
12 Python Tutorial - 12. Modules
Python Tutorial - 12. Modules
codebasics
13 Python Tutorial - 13. Reading/Writing Files
Python Tutorial - 13. Reading/Writing Files
codebasics
14 How to install Julia on Windows
How to install Julia on Windows
codebasics
15 Python Tutorial - 14. Working With JSON
Python Tutorial - 14. Working With JSON
codebasics
16 Julia Tutorial - 1. Variables
Julia Tutorial - 1. Variables
codebasics
17 Julia Tutorial - 2. Numbers
Julia Tutorial - 2. Numbers
codebasics
18 Python Tutorial - 15. if __name__ == "__main__"
Python Tutorial - 15. if __name__ == "__main__"
codebasics
19 Julia Tutorial - Why Should I Learn Julia Programming Language
Julia Tutorial - Why Should I Learn Julia Programming Language
codebasics
20 Python Tutorial  - 16. Exception Handling
Python Tutorial - 16. Exception Handling
codebasics
21 Julia Tutorial - 3. Complex and Rational Numbers
Julia Tutorial - 3. Complex and Rational Numbers
codebasics
22 Julia Tutorial - 4. Strings
Julia Tutorial - 4. Strings
codebasics
23 Python Tutorial -  17. Class and Objects
Python Tutorial - 17. Class and Objects
codebasics
24 Julia Tutorial - 5. Functions
Julia Tutorial - 5. Functions
codebasics
25 Julia Tutorial - 6. If Statement and Ternary Operator
Julia Tutorial - 6. If Statement and Ternary Operator
codebasics
26 Julia Tutorial - 7. For While Loop
Julia Tutorial - 7. For While Loop
codebasics
Python Tutorial  - 18. Inheritance
Python Tutorial - 18. Inheritance
codebasics
28 Julia Tutorial - 8. begin and (;) Compound Expressions
Julia Tutorial - 8. begin and (;) Compound Expressions
codebasics
29 Python Tutorial - 12.1 - Install Python Module (using pip)
Python Tutorial - 12.1 - Install Python Module (using pip)
codebasics
30 Julia Tutorial - 9. Tasks (a.k.a. Generators or Coroutines)
Julia Tutorial - 9. Tasks (a.k.a. Generators or Coroutines)
codebasics
31 Julia Tutorial - 10. Exception Handling
Julia Tutorial - 10. Exception Handling
codebasics
32 Python Tutorial  - 19. Multiple Inheritance
Python Tutorial - 19. Multiple Inheritance
codebasics
33 Python Tutorial - 20. Raise Exception And Finally
Python Tutorial - 20. Raise Exception And Finally
codebasics
34 Python Tutorial - 21. Iterators
Python Tutorial - 21. Iterators
codebasics
35 Python Tutorial - 22. Generators
Python Tutorial - 22. Generators
codebasics
36 Python Tutorial - 23. List Set Dict Comprehensions
Python Tutorial - 23. List Set Dict Comprehensions
codebasics
37 Python Tutorial - 24. Sets and Frozen Sets
Python Tutorial - 24. Sets and Frozen Sets
codebasics
38 Python Tutorial - 25. Command line argument processing using argparse
Python Tutorial - 25. Command line argument processing using argparse
codebasics
39 Debugging Tips - What is bug and debugging?
Debugging Tips - What is bug and debugging?
codebasics
40 Debugging Tips - Conditional Breakpoint
Debugging Tips - Conditional Breakpoint
codebasics
41 Debugging Tips - Watches and Call Stack
Debugging Tips - Watches and Call Stack
codebasics
42 Python Tutorial - 26. Multithreading - Introduction
Python Tutorial - 26. Multithreading - Introduction
codebasics
43 Git Tutorial 3:  How To Install Git
Git Tutorial 3: How To Install Git
codebasics
44 Git Tutorial 1: What is git / What is version control system?
Git Tutorial 1: What is git / What is version control system?
codebasics
45 Git Tutorial 2 : What is Github? | github tutorial
Git Tutorial 2 : What is Github? | github tutorial
codebasics
46 Git Tutorial 4: Basic Commands: add, commit, push
Git Tutorial 4: Basic Commands: add, commit, push
codebasics
47 Git Tutorial 5: Undoing/Reverting/Resetting code changes
Git Tutorial 5: Undoing/Reverting/Resetting code changes
codebasics
48 Git Tutorial 6: Branches (Create, Merge, Delete a branch)
Git Tutorial 6: Branches (Create, Merge, Delete a branch)
codebasics
49 Git Github Tutorial 10: What is Pull Request?
Git Github Tutorial 10: What is Pull Request?
codebasics
50 Git Tutorial 7: What is HEAD?
Git Tutorial 7: What is HEAD?
codebasics
51 Git Tutorial 9: Diff and Merge using meld
Git Tutorial 9: Diff and Merge using meld
codebasics
52 Difference between Multiprocessing and Multithreading
Difference between Multiprocessing and Multithreading
codebasics
53 Python Tutorial - 27. Multiprocessing Introduction
Python Tutorial - 27. Multiprocessing Introduction
codebasics
54 Python Tutorial - 28. Sharing Data Between Processes Using Array and Value
Python Tutorial - 28. Sharing Data Between Processes Using Array and Value
codebasics
55 Git Tutorial 8 - .gitignore file
Git Tutorial 8 - .gitignore file
codebasics
56 Python Tutorial - 29. Sharing Data Between Processes Using Multiprocessing Queue
Python Tutorial - 29. Sharing Data Between Processes Using Multiprocessing Queue
codebasics
57 Python Tutorial - 30. Multiprocessing Lock
Python Tutorial - 30. Multiprocessing Lock
codebasics
58 Python Tutorial - 31. Multiprocessing Pool (Map Reduce)
Python Tutorial - 31. Multiprocessing Pool (Map Reduce)
codebasics
59 What is code?
What is code?
codebasics
60 Python unit testing - pytest introduction
Python unit testing - pytest introduction
codebasics

This video tutorial teaches the concept of inheritance in Python, including how to derive a class from another class and create objects of that class. It provides a step-by-step guide on implementing inheritance in Python programming.

Key Takeaways
  1. Define a base class
  2. Derive a new class from the base class
  3. Create objects of the derived class
  4. Access attributes and methods of the base class
  5. Override methods of the base class
💡 Inheritance allows for code reuse and facilitates the creation of a hierarchy of related classes

Related Reads

📰
Meshy AI 3D Generator Review 2026: 185 MB Per House, $193 to Ship It
Learn how Meshy AI 3D Generator works and its implications on data storage and shipping costs
Medium · AI
📰
5 Claude Tricks That Feel Almost Like Cheating
Boost productivity with Claude by using 5 tricks that simplify workflows and enhance efficiency
Medium · Data Science
📰
[Video] I Tested Grok 4.7 With One Simple Job: Turn My vi Task Files Into a Bash Agenda
Learn how to use Grok 4.7 to automate task management by converting vi task files into a Bash agenda
Dev.to AI
📰
oh-my-agent 15: HyperFrames replaces Remotion, Serena guard ships
Learn about the latest updates in oh-my-agent CLI 15.0.0, including the replacement of Remotion with HyperFrames and new features for code intelligence and Serena daemons
Dev.to AI

Chapters (6)

Introduction
0:13 what is Inheritance
2:05 Implement Inheritance in python
3:01 derive a class from another class
5:29 create objects of class
9:50 benefits of inheritance (code reuse, extensibility, readability )
Up next
How to Get Your Brand Cited by ChatGPT, Claude and Gemini
Arvow
Watch →