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.
extends
Keyword: 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
Animal
class has a propertyname
and a methodeat()
. - The
Dog
class extendsAnimal
, inheriting its properties and methods. - The
Dog
class also has its own methodbark()
. - An instance of
Dog
can 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
Dog
class overrides theeat()
method from theAnimal
class. - When
eat()
is called on aDog
instance, the overridden method is executed.
Practical Exercise
Task
- Create a base class
Vehicle
with propertiesmake
andmodel
, and a methodstart()
. - Create a derived class
Car
that extendsVehicle
and 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
Vehicle
class has propertiesmake
andmodel
, and a methodstart()
. - The
Car
class extendsVehicle
and adds a methoddrive()
. - An instance of
Car
is created, its properties are set, and bothstart()
anddrive()
methods are called.
Common Mistakes
- Forgetting to Use
extends
: Ensure that the derived class uses theextends
keyword 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.