Q:

Ruby program to call a superclass constructor from sub-class using the super() method

0

In this program, we will implement single inheritance using the "<" operator and call the superclass constructor from the sub-class using the super() method.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program/Source Code:

The source code to call the superclass constructor from the sub-class using the super() method is given below. The given program is compiled and executed successfully.

# Ruby program to call a superclass constructor 
# from sub-class using super() method

class SuperClass
	def initialize
		puts "SuperClass constructor";
	end
end

class SubClass < SuperClass
	def initialize
	    super();
	    puts "SubClass constructor";
	end
end

subObj = SubClass.new;

Output:

SuperClass constructor
SubClass constructor

Explanation:

In the above program, we created two classes SuperClassSubClass. We inherited SuperClass into SubClass. And, we used the super() method to call the superclass constructor from the subclass and printed appropriate messages.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Ruby program to initialize instance variables usin... >>
<< Ruby program to call the overridden superclass met...