This is below is an example of superclass/subclass construct :

C:\>irb --simple-prompt
>> class Parent
>> @@x = 10
>> end
=> 10
>> class Child < Parent
>> @@x = 12
>> end
=> 12
>> class Parent
>> puts "@@X = #{@@x}"
>> end
@@X = 12
=> nil

And the above is also understood.But I wanted to check if possible or not when two class are defined seprately as an standalone class anyway to define the super/sub relation between them?

I tried the below but it doesn't work. May be not the way I tried:

C:\>irb --simple-prompt
>> class Parent
>> @@X = 10
>> end
=> 10
>> class Child
>> @@x = 15
>> end
=> 15
>> class Child < Parent
>> def show
>> p "hi"
>> end
>> end
TypeError: superclass mismatch for class Child
        from (irb):7
        from C:/Ruby193/bin/irb:12:in `<main>'
>>
Member Avatar for LastMitch

I got no idea what you are running.

You can try this (not really tested):

class Parent
  def show  
    puts "@@x = 10"  
  end  
end  

class Child 
  def show   
    puts "@@x = 15"  
  end  
end  

class Child < Parent  
  def show   
    puts "hi"  
  end  
end 

irb = Child.new  
irb.show  

Nopes, Not working.

C:\>irb --simple-prompt
>> class Parent
>> def show
>> puts "@@X=10"
>> end
>> end
=> nil
>> class Child
>> def show
>> puts "@@X=15"
>> end
>> end
=> nil
>> class Child < Parent
>> def show
>> puts "hi"
>> end
>> end
TypeError: superclass mismatch for class Child
        from (irb):11
        from C:/Ruby193/bin/irb:12:in `<main>'
>>

My ruby version is :

C:\>ruby -v
ruby 1.9.3p0 (2011-10-30) [i386-mingw32]

C:\>
Member Avatar for LastMitch

TypeError: superclass mismatch for class Child

Since you got the same error from the code I got.

Then it has to do with class Child being repeat twice somewhere in your other or this one code.

Try to rename this class Child to class Childs in here:

class Childs < Parent

What error appear now when you run the code?

You can not change the superclass of a class after it has been defined. That's just not possible. If you just asked out of curiosity, that's basically all there is to say.

However if you asked because you have a specific problem that you think could be solved by changing the superclass of a class after the fact, you should describe that problem, so that we can think of alternative ways to solve that problem.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.