Junior — Middle
What is meant by a subclass in object-oriented programming?
sobes.tech AI
Answer from AI
In object-oriented programming, a subclass is a class that inherits properties and methods from another class — the superclass (or base class). A subclass can extend or override the behavior of the superclass.
Example in Python:
class Animal:
def speak(self):
print("Animal makes a sound")
class Dog(Animal): # Dog is a subclass of Animal
def speak(self):
print("Woof")
my_pet = Dog()
my_pet.speak() # Outputs: Woof
Here, Dog inherits from Animal and overrides the speak method. Thus, a subclass allows creating more specialized versions of the base class.