Inheritance is a fundamental concept in object-oriented programming (OOP) that allows a class to inherit properties and methods from another class. This promotes code reuse and establishes a natural hierarchy between classes.
Key Concepts
- Base Class (Parent Class): The class whose properties and methods are inherited.
- Derived Class (Child Class): The class that inherits from the base class.
extendsKeyword: Used to indicate that a class is inheriting from another class.
Why Use Inheritance?
- Code Reusability: Common functionality can be written once in the base class and reused in derived classes.
- Hierarchical Classification: Establishes a natural hierarchy between classes, making the code more organized and understandable.
- Polymorphism: Allows objects to be treated as instances of their parent class, enabling flexible and dynamic code.
Example: Basic Inheritance
Let's start with a simple example to illustrate inheritance in Groovy.
Base Class: Animal
Derived Class: Dog
Using the Classes
def dog = new Dog() dog.name = "Buddy" dog.eat() // Output: Buddy is eating. dog.bark() // Output: Buddy is barking.
Explanation
- The
Animalclass has a propertynameand a methodeat(). - The
Dogclass extendsAnimal, inheriting its properties and methods. - The
Dogclass also has its own methodbark(). - An instance of
Dogcan access both the inheritedeat()method and its ownbark()method.
Overriding Methods
Derived classes can override methods from the base class to provide specific implementations.
Example: Overriding the eat Method
class Dog extends Animal {
void eat() {
println("${name} is eating dog food.")
}
void bark() {
println("${name} is barking.")
}
}
def dog = new Dog()
dog.name = "Buddy"
dog.eat() // Output: Buddy is eating dog food.
dog.bark() // Output: Buddy is barking.Explanation
- The
Dogclass overrides theeat()method from theAnimalclass. - When
eat()is called on aDoginstance, the overridden method is executed.
Practical Exercise
Task
- Create a base class
Vehiclewith propertiesmakeandmodel, and a methodstart(). - Create a derived class
Carthat extendsVehicleand adds a methoddrive(). - Create an instance of
Car, set its properties, and call bothstart()anddrive()methods.
Solution
class Vehicle {
String make
String model
void start() {
println("${make} ${model} is starting.")
}
}
class Car extends Vehicle {
void drive() {
println("${make} ${model} is driving.")
}
}
def car = new Car()
car.make = "Toyota"
car.model = "Corolla"
car.start() // Output: Toyota Corolla is starting.
car.drive() // Output: Toyota Corolla is driving.Explanation
- The
Vehicleclass has propertiesmakeandmodel, and a methodstart(). - The
Carclass extendsVehicleand adds a methoddrive(). - An instance of
Caris created, its properties are set, and bothstart()anddrive()methods are called.
Common Mistakes
- Forgetting to Use
extends: Ensure that the derived class uses theextendskeyword to inherit from the base class. - Overriding Methods Incorrectly: When overriding methods, ensure the method signature matches exactly with the base class method.
Summary
In this section, we covered the concept of inheritance in Groovy, including:
- The basics of inheritance and its benefits.
- How to create base and derived classes.
- Overriding methods in derived classes.
- Practical examples and exercises to reinforce the concepts.
Next, we will explore Interfaces and Traits in Groovy, which provide additional ways to achieve polymorphism and code reuse.
