ADVERTISEMENT
ADVERTISEMENT

Ruby program to call the overridden superclass method from the subclass

Ruby Example: Write a program to call the overridden superclass method from the subclass.
Submitted by Nidhi, on January 27, 2022

Problem Solution:

In this program, we will override the method of superclass into sub-class. Then we will access the superclass method from the subclass method using the super() method.

Program/Source Code:

The source code to call the overridden superclass method from sub-class is given below. The given program is compiled and executed successfully.

# Ruby program to call the overridden superclass 
# method from the subclass.

# 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
	
	def SayHello
	    super();
		puts "Say hello from SubClass";
	end
end

subObj = SubClass.new;
subObj.SayHello;

Output:

SubClass constructor
Say hello from SuperClass
Say hello from SubClass

Explanation:

In the above program, we created two classes SuperClass, SubClass. And, we override the SayHello() method into SubClass by inheriting SuperClass into SubClass, and accessing the superclass method from subclass using the super() method. After that, we created an object of SubClass, and called SayHello() method of SubClass.

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.