Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Instance Variables Inheritance

Can someone explain how a class can access the instance variables of its superclass and how that is not inheritance? I'm talking about 'The Ruby Programming Language' and the example

class Point
  def initialize(x,y) # Initialize method 
     @x,@y = x, y      # Sets initial values for instance variables
  end

end

class Point3D < Point
 def initialize(x,y,z)
   super(x,y)
   @z = z
 end
 def to_s
   "(#@x, #@y, #@z)"  # Variables @x and @y inherited?
 end
end

Point3D.new(1,2,3).to_s => "(1, 2, 3)"

How can class Point3D access x and y inside to_s if they're not inherited? The book says:

"The reason that they sometimes appear to be inherited is that instance variables are created by the methods that first assign values to them, and those methods are often inherited or chained."

but I can't figure out what it really means.

like image 535
Zed Avatar asked Nov 16 '12 21:11

Zed


People also ask

Are instance variables inherited?

I know that instance variable are not inherited but they can be accessed in sub class. If they can be accessed in sub class then does that means that they are shared between super class and subclass or both super class and subclass have different copy.

Do instance variables get inherited in Java?

In this example, an object of type Mammal has both the instance variable weight and the method eat() . They are inherited from Animal . A class can extend only one other class. To use the proper terminology, Java allows single inheritance of class implementation.

Are instance variables inherited in Ruby?

Instance variables are not inherited. If a method is written in the subclass with the same name and parameters as one in the parent class, the super class' method is overwritten.

What is inherited variable?

In the definition of the inherited class, you only need to specify the methods and instance variables that are different from the parent class (the parent class, or the superclass, is what we may call the class that is inherited from. In the example we're discussing, Pet would be the superclass of Dog or Cat ).


2 Answers

You are right, the book is wrong, or at least poorly worded


I would argue that the book is simply wrong, or at best, it's making a quite muddy explanation.

In all OO languages, the superclass and derived class don't have separate objects. When you create an instance of the derived class, it is also an instance of the superclass. There is one object and it is both classes at once.

Since there is only one object, there is only one set of instance variables.

This is the same as all other OO systems. The weird argument that book makes about how it just matters which method is run and how the methods themselves are what are really inherited does not add much in the way of clarity.

The problem with the terminology is that, sure, in a dynamically typed system there is no declaration in the first place, and so certainly the definition of the subclass doesn't inherit any field declarations ... because of course there aren't any. But just because there are no types to inherit doesn't make the opposite statement ("instance variables are not inherited") any more true, and it adds quite a bit of confusion because it implies that somehow the parent would have different instance variables, which is the nonsensical result of trying to talk about the objects the way they do.

like image 75
DigitalRoss Avatar answered Oct 19 '22 15:10

DigitalRoss


super(x,y) calls the constructor of the base class, which is the initialize method. If you take super(x,y) out, then the variables @x and @y won't appear in the derived class.

like image 33
Candide Avatar answered Oct 19 '22 15:10

Candide