Ruby Inheritance

        Inheritance is an important concept in object-oriented programming, and Ruby supports it fully. It allows a class to inherit properties (instance variables) and behaviors (methods) from another class, called the superclass. The subclass can then add or override these properties and behaviors as needed.

To demonstrate inheritance in Ruby, let's define a superclass called Animal:

    class Animal
      attr_accessor :name, :age

      def initialize(name, age)
        @name = name
        @age = age
      end

      def talk
        puts "I am an animal"
      end
    end

        This defines an Animal class with two attributes (name and age) and a method called talk. This method simply prints out a string indicating that the object is an animal.

Now, let's define a subclass called Dog that inherits from Animal:

  class Dog < Animal
    def talk
      puts "Woof!"
    end
  end

        This defines a Dog class that inherits from Animal. It overrides the talk method defined in the superclass with its own implementation that prints "Woof!" instead of "I am an animal".

Now we can create an instance of Dog and call the talk method:

    dog = Dog.new("Fido", 3)
    dog.talk # prints "Woof!"

        Since Dog inherits from Animal, it has access to the name and age attributes and the talk method defined in the superclass. It also has its own implementation of the talk method that overrides the one defined in the superclass.

        Inheritance allows us to create classes that share common properties and behaviors, while allowing us to customize them as needed in subclasses. This helps to avoid duplication of code and makes it easier to maintain and extend our code.