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
-
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.
-
Inheritance Syntax:
- In Ruby, inheritance is denoted using the
<symbol.
- In Ruby, inheritance is denoted using the
-
Method Overriding:
- Subclasses can override methods defined in the superclass to provide specific behavior.
-
superKeyword:- Used to call methods from the superclass within the subclass.
Example
Let's look at a practical example to understand inheritance better.
Superclass: Animal
Subclass: Dog
Explanation
-
Superclass Definition:
- We define a class
Animalwith aninitializemethod and aspeakmethod. - The
initializemethod sets the@nameinstance variable. - The
speakmethod returns a string.
- We define a class
-
Subclass Definition:
- We define a class
Dogthat inherits fromAnimalusing the<symbol. - The
speakmethod inDogoverrides thespeakmethod inAnimal. - The
superkeyword is used to call thespeakmethod fromAnimaland append additional behavior.
- We define a class
Using the Classes
Output:
Practical Exercises
Exercise 1: Create a Subclass
- Task: Create a subclass
Catthat inherits fromAnimal. Override thespeakmethod to return "Meow! Meow!". - Solution:
class Cat < Animal
def speak
super + " Meow! Meow!"
end
end
cat = Cat.new("Whiskers")
puts cat.speakOutput:
Exercise 2: Add More Methods
- Task: Add a method
moveto theAnimalclass that returns "I can move". Override this method in theDogclass to return "I can run". - 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.moveOutput:
Common Mistakes and Tips
-
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.
- If you override a method and want to include the behavior of the superclass method, don't forget to call
-
Incorrect Inheritance Syntax:
- Ensure you use the
<symbol correctly to denote inheritance.
- Ensure you use the
-
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.
Ruby Programming Course
Module 1: Introduction to Ruby
Module 2: Basic Ruby Concepts
Module 3: Working with Collections
Module 4: Object-Oriented Programming in Ruby
- Classes and Objects
- Instance Variables and Methods
- Class Variables and Methods
- Inheritance
- Modules and Mixins
Module 5: Advanced Ruby Concepts
Module 6: Ruby on Rails Introduction
- What is Ruby on Rails?
- Setting Up Rails Environment
- Creating a Simple Rails Application
- MVC Architecture
- Routing
Module 7: Testing in Ruby
- Introduction to Testing
- Unit Testing with Minitest
- Behavior-Driven Development with RSpec
- Mocking and Stubbing
