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.