The source code to implement single inheritance is given below. The given program is compiled and executed successfully.
# Ruby program to implement
# single inheritance.
# Super class
class SuperClass
def initialize
puts "SuperClass constructor";
end
def SayHello
puts "Say hello from SuperClass";
end
end
class SubClass < SuperClass
def initialize
puts "SubClass constructor";
end
end
subObj = SubClass.new;
subObj.SayHello;
Output:
SubClass constructor
Say hello from SuperClass
Explanation:
In the above program, we created two classes SuperClass, SubClass. And, we inherited the SuperClass into SubClass using the "<" operator. After that, we created the object of SubClass and called the SayHello() from the SubClass object and printed the appropriate message.
Program/Source Code:
The source code to implement single inheritance is given below. The given program is compiled and executed successfully.
Output:
Explanation:
In the above program, we created two classes SuperClass, SubClass. And, we inherited the SuperClass into SubClass using the "<" operator. After that, we created the object of SubClass and called the SayHello() from the SubClass object and printed the appropriate message.
need an explanation for this answer? contact us directly to get an explanation for this answer