ADVERTISEMENT
ADVERTISEMENT

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

Ruby Example: Write a program to call a superclass constructor from sub-class using the super() method.
Submitted by Nidhi, on January 28, 2022

Problem Solution:

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

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 SuperClass, SubClass. We inherited SuperClass into SubClass. And, we used the super() method to call the superclass constructor from the subclass and printed appropriate messages.

Ruby Constructors/Destructors, Inheritance Programs »


ADVERTISEMENT
ADVERTISEMENT


Comments and Discussions!



ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.