How do you define a subclass in python
This is an article on Python Subclasses and inheritance. Before reading on, if you haven’t done so already, I strongly recommend you check out my write up on Python Classes. Show Let’s get cracking! A Python Sub-what?Let’s say you have a class already set up. In my previous article on classes, I created what I’m going to refer to as a
“single tier” Person class. That is, you use the What if I wanted some more depth though? Let’s use vehicles as an example. I’ll get really simplistic here. Vehicle > Car > Mercedes. See that? Now we’re talking! Multiple levels! Not only that, but the Mercedes we just created will inherit all of the attributes and functions associated with the That is, the Mercedes will be deemed a car vehicle in that it has four wheels and a windshield as opposed to a motorbike vehicle which only has two wheels and a tiny windshield. Both the car and the motorbike are vehicles but just two different types of vehicles. Get it? A Familiar Example
To demonstrate this in code, I wrote up a Imagine if you would, a game with a Boss in it. I can imagine myself coding up a Boss class similar to the below. I’d then use this code to create different types of Bosses:
A boss would have a Now, let’s start working on the Boss Subclasses. What kind of Bosses do we want to be able to make? How about a
What did I do here? It’s very similar to coding the parent class. Note the differences though:
Phew! Okay. Next up, we can define some GoodBoss specific class functions. These can only be used by an object created
using the
What does a good boss do? He/She nurtures talent to help employees grow! They also encourage their teams to keep them motivated! These two class functions simply print out the specified message when called. If this were a game, rather than just printing, we could have these functions perform transactions like “increase In the same way, we can define a Who’s the Boss?!Time for some fun! I’ll be the good boss, Bob can be the bad boss. (Sorry brother!). I’ve got a file called
Once imported, we can create a “standard”
We can then use the class functions associated with both the
I wish I really had that effect on people! Also, this is inheritance in action! We “inherited” the Ending on a lowI know we’re on a roll but Bad Bosses always have a way of ruining things:
ConclusionHow cool are classes, subclasses and inheritance?! Not only has this helped me better plan my code but it’s also allowed me to better appreciate other code. I can only imagine how many classes and subclasses exist in video games (World of Warcraft for example!). Once you get the hang of them, classes really are quite easy to use. They’re a series of set and forget templates/blueprints for you to call at any time. Oh the possibilities… Yum! Oh and speaking of templates. I created a framework/blank class and subclass template for Day 77 of our 100 days of code challenge. Check it out here! Keep Calm and Code in Python! — Julian (Psst! Did anyone get the Simpsons Reference?) How do you define a subclass Python?The class from which a class inherits is called the parent or superclass. A class which inherits from a superclass is called a subclass, also called heir class or child class. Superclasses are sometimes called ancestors as well.
How do you define class and subclass in Python?The process of creating a subclass of a class is called inheritance. All the attributes and methods of superclass are inherited by its subclass also. This means that an object of a subclass can access all the attributes and methods of the superclass.
How do you define subclass?Definition: A subclass is a class that derives from another class. A subclass inherits state and behavior from all of its ancestors. The term superclass refers to a class's direct ancestor as well as all of its ascendant classes.
How do you create a subclass?Creating a subclass can be as simple as including the extends clause in your class declaration (such as in the declaration in ImaginaryNumber above). However, you usually have to make other provisions in your code when subclassing a class, such as overriding methods.
|