Junior — Middle
What is the difference between subclasses or derived classes in object-oriented programming?
sobes.tech AI
Answer from AI
In object-oriented programming, a subclass (or derived class) is a class that inherits properties and methods from another class called the superclass (or base class).
The difference between them is that the subclass extends or overrides the functionality of the superclass by adding new methods or changing the behavior of existing ones.
Example in Java:
class Animal {
void sound() {
System.out.println("Some sound");
}
}
class Dog extends Animal {
@Override
void sound() {
System.out.println("Bark");
}
}
Here, Dog is a subclass that inherits from Animal and overrides the sound method.