1

I know that instance variables have nothing to do with inheritance:

class A

  def initialize
    @x = 2
  end

end

class B < A

  def puts_x
    puts @x
   end

  def x=(value)
    @x = value
  end

end

b = B.new
b.puts_x
b.x=3
b.puts_x

This outputs:

2
3

Here, class B inherits from class A, and @x in class B has nothing to do with inheritance.

But the output is 2. I want to understand it.

The "Ruby Inheritance" page says:

Since instance variables have nothing to do with inheritance, it follows that an instance variable used by a subclass cannot "shadow" an instance variable in the super-class. If a subclass uses an instance variable with the same name as a variable used by one of its ancestors, it will overwrite the value of its ancestor's variable.

I also want any examples for this.

3
  • In class B, you created a setter method x=(value), and since you already inherited the initialize method from class A and initially set your instance variable @x = 2, when you called b.x = 3, you called your setter method to change the value of that instance variable. So now @x = 3 from there on for that instance of b Commented Mar 1, 2016 at 9:17
  • 1
    "It is said that ..." - please include a source. Commented Mar 1, 2016 at 9:18
  • You can refer to rubylearning.com/satishtalim/ruby_inheritance.html Commented Mar 1, 2016 at 10:14

1 Answer 1

9

B inherits initialize from A.

At object creation, initialize is invoked. So you get @x set to 2 even for objects of class B.

I think, the sentences you are quoting refer to this scenario:

class A
  def initialize
    @x = 42
  end
end

class B < A
  def initialize
    @x = 23
  end
end

h = B.new

Now, h has just one instance variable @x with value 23. It is not like there is one @x from B and one from A. You can see this here:

class A
  def initialize
    @x = 42
  end

  def set_x_from_a
    @x = 12
  end

  def print_x_from_a
    puts @x
  end
end

class B < A
  def initialize
    @x = 23
  end

  def set_x_from_b
    @x = 9
  end

  def print_x_from_b
    puts @x
  end
end

h = B.new
h.print_x_from_a            # => 23
h.print_x_from_b            # => 23
h.set_x_from_a
h.print_x_from_b            # => 12
h.set_x_from_b
h.print_x_from_a            # => 9
Sign up to request clarification or add additional context in comments.

8 Comments

Thanx for your reply. your explanation is not that subclass change its ancestor's value.I think that superclass change its subclass's value.You can refer to rubylearning.com/satishtalim/ruby_inheritance.html
@Messi: You are right. But still ... there is just one @x existing in the object no matter which classes method you are accessing it from.
But h is still the instance of subclass and @x is instance variable of the subclass.The book says that changing the value of the instance variable of the subclass overwrites superclass's value.Where is instance variable of the superclass in your explanation?
@Messi: The point is, there is no instance variable in the superclass. The instance variable exists in the instance, in h (hence the name). There is just one.
So if there is no instance variable in the superclass, how does subclass change superclass's value?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.