Calling Private Methods from Subclasses in Ruby

Consider the following code:

class Car

	private

	def drive
		puts "I'm driving a car"
	end
end


class Toyota < Car
	def publicly_drive
		drive # can I call drive here?
	end
end

Now answer this quiz:

t = Toyota.new
t.drive # what do you think will happen? (remember, 'drive' is private)
t.publicly_drive # what do you think will happen?

The answers:

t = Toyota.new
t.drive 
# => calling_super_class_test.rb:20:in `<main>': private method `drive' called for #<Toyota:0x00007fb229d59e58> (NoMethodError)
t.publicly_drive # what do you think will happen?
# => I'm driving a car

Of course! It all makes sense once you type it out and test it yourself.

Written on September 25, 2025