Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9192797
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T21:00:53+00:00 2026-06-17T21:00:53+00:00

In the Chapter 7.3.5 Inheritance and Instance Variables of the book the ruby programing

  • 0

In the Chapter 7.3.5 “Inheritance and Instance Variables” of the book “the ruby programing language” says :

Because 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 superclass. 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. This can be done intentionally, to alter the behavior of the ancestor, or it can
be done inadvertently. In the latter case, it is almost certain to cause bugs. As with the
inheritance of private methods described earlier, this is another reason why it is only
safe to extend Ruby classes when you are familiar with (and in control of) the
implementation of the superclass.

I had made my own test , but seems instance variables from subclass does NOT affect the superclass

my Ruby version

bob@bob-ruby:~$ ruby --version
ruby 1.9.3p194 (2012-04-20 revision 35410) [i686-linux]
bob@bob-ruby:~$ 

Below is the code

class Point
   attr_accessor :x,:y
   def initialize(x,y)
     @x,@y=x,y
   end
end

class Point3D < Point
   attr_accessor :x,:y,:z
   def initialize(x,y,z)
     @x=x
     @y=y
     @z=z
   end
end

irb(main):052:0> p=Point.new(1,2)
=> #<Point:0x87e8968 @x=1, @y=2>
irb(main):053:0> q=Point3D.new(4,5,6)
=> #<Point3D:0x87e423c @x=4, @y=5, @z=6>
irb(main):054:0> q.x
=> 4
irb(main):055:0> p.x
=> 1
irb(main):056:0> 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T21:00:55+00:00Added an answer on June 17, 2026 at 9:00 pm

    The book (emphasis and addition mine):

    If a subclass uses an instance variable with the same name as a[n instance] variable used by one of its ancestors, it will overwrite the value of its ancestor’s variable.

    I know you don’t have two instances of the same class; we’re specifically discussing inheritance.

    When a subclass uses an instance variable with the same name as an instance variable used by the superclass, there’s a single instance variable. If the subclass changes the value of that instance variable, and the superclass accesses it, it gets the value set by the subclass.

    When a subclass is instantiated, it acts “as-if” it’s also an instance of the superclass. The way Ruby is implemented means that if the superclass has an instance variable @foo, the subclass can access it. This makes a distinction between the subclass’s @foo and the superclass’s @foo meaningless.

    This is how subclasses may alter superclass behavior: by setting a value the superclass might use. If a subclass sets @foo = 42, and a superclass method accesses @foo, it sees 42. This may or may not be intended, hence the warning. It can lead to spectacularly frustrating debugging sessions.

    class MyStack
      def initialize
        @my_array = []
      end
    
      def push(item)
        @my_array << item
      end
    end
    
    # Stack class that keeps a list 
    # of every item ever pushed.
    class TrackingStack < MyStack
      def initialize
        super
        @my_array = []
      end
    
      def push(item)
        super
        @my_array << item
      end
    
      def all_items_ever_pushed
        @my_array
      end
    end
    

    TrackingStack introduces a bug, because it inadvertently used the same name as the superclass’s array used to hold the stack contents. If you weren’t familiar with the superclass’s implementation, this would cause confusion and bugs until you dug deeply enough to understand where the unintended behavior came from.

    An instance of the superclass is just that: an instance of the superclass, and it’s meaningless to talk about how an instance of the subclass will affect it, because they’re completely unrelated.

    Here’s a rephrasing:

    Subclassing can be risky when you don’t control, or are unfamiliar with, the superclass implementation. One reason is because the introduction of an instance variable in the subclass may overwrite the value of a superclass instance variable, leading to unintended behavior.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In the security chapter of The Django Book , it says that I must
In the book Java in a Nutshell, chapter 3, section 3.5 Subclasses and Inheritance,
In chapter 6.2.1 of Mitchell's book (Concepts in Programming Languages), it mentioned that: Type
I have read in Chapter 4 of the NHibernate docs that all of a
I'm studying a chapter in java related to Inheritance, and i have a few
I have a superclass with 2 instance variables (say int a , and int
A chapter out of the book I have been reading has focused on memory
I'm currently studying the Inheritance chapter of a C# book. I'm unable to understand
Chapter 6 Responsive Interfaces of High Performance JavaScript book by Nicholas C. Zakas says
SICP Chapter 3.5.3 http://mitpress.mit.edu/sicp/full-text/book/book-Z-H-24.html#%_sec_3.5.3 In section Streams as signals , SICP gives an audio-visual

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.