Intern
What capabilities does a subclass provide in object-oriented programming?
sobes.tech AI
Answer from AI
- Inheritance of properties and methods: The subclass gains access to the public and protected members of the base class.
- Overriding: Allows changing the implementation of base class methods.
- Adding new properties and methods: The subclass can extend functionality by adding its own members.
- Polymorphism: The ability to treat an object of the subclass as an object of the base class.
Example of method overriding:
class Animal {
void makeSound() {
// Emits a general sound
}
}
class Dog extends Animal {
@Override
void makeSound() {
// Emits a dog's sound
}
}