By the name of the topic, it is clear in itself that we are going to deal with the subclass of any class. To give you a general idea, think that a square is also a rectangle, which means that it is a subclass of rectangle.
Let's make it clearer.
Suppose we define a rectangle with some length and breadth. Now, a square is also a rectangle but having the same length and breadth.

I think, it has given you a feel that square can be a subclass of rectangle.
Instead of writing new data members or methods, one can inherit the members of an existing class. This existing class is called the base class or superclass, and the new class is called the derived class or sub-class.
Let's learn to do all these things in Ruby.
class Child
def initialize(name)
@name = name
end
def print_name
puts @name
end
end
class Student < Child
def initialize(name,roll)
super(name)
@roll = roll
end
def print_roll
puts @roll
end
end
a = Child.new("xyz")
a.print_name
b = Student.new("abc",12)
b.print_name
b.print_roll
abc
12
Let's start understanding this code by knowing the use of < first. < is used to make a sub-class. Student < Child means that Student is a sub-class of Child class and that's it.
Now, Student has its own constructor which takes 'name' and 'roll' also, whereas its superclass i.e., Child has a constructor which only takes 'name'. Keeping it simple, it means that Child will have a 'name' and Student will also have a 'name' but a 'roll' too. Since Student is a sub-class of Child, therefore while making an object of Student, its superclass i.e., Chlid must also be initialized and to initialize it, we need to pass a 'name' as needed by its constructor and this is exactly what super does.
super will call the constructor of the superclass and will initialize it. So, super(name) will call the constructor of the superclass ( Child ) and will pass 'name' to it and that's it.
Rest of the code is simple.
Method print_name is defined in superclass. So, it can be accessed from both superclass and sub-class or their objects.
Method print_roll is defined in the subclass, so it can be accessed only from that subclass or its objects.
class Child
def initialize(name)
@name = name
end
def print_name
puts @name
end
end
class Student < Child
#no constructor
end
a = Child.new("xyz")
a.print_name
b = Student.new("abc")
b.print_name
abc
Square and rectangle example
class Rectangle
def initialize(leng,br)
@length = leng
@breadth = br
end
#method to print length and breath
def print_dim
puts "#{@length}\n#{@breath}"
end
#method to return area of rectangle
def area
return @length*@breadth
end
end
class Square < Rectangle
def initialize(side)
#square is also a rectangle but having same length and breath
#square will have same length and breath
super(side,side)
@side = side
end
#method to print side of the square
def print_side
puts @side
end
end
s = Square.new(4)
s.print_dim
s.print_side
puts s.area
4
4
16
There is nothing new in the above code to explain. If you are facing any difficulty, then read the comments in the code.
You can ask questions in the discussion section anytime and feel free to ask.
First of all, convince yourself that you are the best because rest of your life is going to prove this to others.
-Wasim Akram