Inheritance is a fundamental concept in object-oriented programming (OOP) that allows a class to inherit attributes and methods from another class. This promotes code reuse and can make your programs more modular and easier to maintain.

Key Concepts

  1. Superclass and Subclass:

    • Superclass (Parent Class): The class whose properties and methods are inherited.
    • Subclass (Child Class): The class that inherits properties and methods from the superclass.
  2. Inheritance Syntax:

    • In Ruby, inheritance is denoted using the < symbol.
  3. Method Overriding:

    • Subclasses can override methods defined in the superclass to provide specific behavior.
  4. super Keyword:

    • Used to call methods from the superclass within the subclass.

Example

Let's look at a practical example to understand inheritance better.

Superclass: Animal

class Animal
  def initialize(name)
    @name = name
  end

  def speak
    "Hello, I am an animal."
  end
end

Subclass: Dog

class Dog < Animal
  def speak
    super + " Woof! Woof!"
  end
end

Explanation

  1. Superclass Definition:

    • We define a class Animal with an initialize method and a speak method.
    • The initialize method sets the @name instance variable.
    • The speak method returns a string.
  2. Subclass Definition:

    • We define a class Dog that inherits from Animal using the < symbol.
    • The speak method in Dog overrides the speak method in Animal.
    • The super keyword is used to call the speak method from Animal and append additional behavior.

Using the Classes

dog = Dog.new("Buddy")
puts dog.speak

Output:

Hello, I am an animal. Woof! Woof!

Practical Exercises

Exercise 1: Create a Subclass

  1. Task: Create a subclass Cat that inherits from Animal. Override the speak method to return "Meow! Meow!".
  2. Solution:
class Cat < Animal
  def speak
    super + " Meow! Meow!"
  end
end

cat = Cat.new("Whiskers")
puts cat.speak

Output:

Hello, I am an animal. Meow! Meow!

Exercise 2: Add More Methods

  1. Task: Add a method move to the Animal class that returns "I can move". Override this method in the Dog class to return "I can run".
  2. Solution:
class Animal
  def initialize(name)
    @name = name
  end

  def speak
    "Hello, I am an animal."
  end

  def move
    "I can move"
  end
end

class Dog < Animal
  def speak
    super + " Woof! Woof!"
  end

  def move
    "I can run"
  end
end

dog = Dog.new("Buddy")
puts dog.move

Output:

I can run

Common Mistakes and Tips

  1. Forgetting to Call super:

    • If you override a method and want to include the behavior of the superclass method, don't forget to call super.
  2. Incorrect Inheritance Syntax:

    • Ensure you use the < symbol correctly to denote inheritance.
  3. Overriding Methods:

    • Be cautious when overriding methods. Ensure that the new behavior is appropriate for the subclass.

Conclusion

Inheritance is a powerful feature in Ruby that allows you to create a hierarchy of classes, promoting code reuse and modularity. By understanding and using inheritance effectively, you can write more maintainable and scalable code. In the next section, we will explore modules and mixins, which provide another way to share functionality between classes.

© Copyright 2024. All rights reserved