# Superclass table inheritance

**URL:** <https://www.ruby-forum.com/t/superclass-table-inheritance/125532>\
**Category:** Rails\
**Created:** [January 9, 2008, 11:11pm UTC](https://www.ruby-forum.com/t/superclass-table-inheritance/125532 "2008-01-09T23:11:45Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Russell\_McConnachie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b5ac83/32.png) [@Russell\_McConnachie](https://www.ruby-forum.com/u/Russell_McConnachie)\
**Post date:** [January 9, 2008, 11:11pm UTC](https://www.ruby-forum.com/t/superclass-table-inheritance/125532/1 "2008-01-09T23:11:45Z")

</div>

Hello,

I am trying to find a way to preserve the same class name without having  
to cut and paste code from one class to another. How do I do something  
such as:

## test1.rb

class A  
def initialize  
echo “Initailizing class A.”  
end  
end

## test2.rb

require ‘test1’

class A  
def initialize  
super  
echo “Initializing class A again.”  
end  
end

a = A.new

Expected output:

Initailizing class A  
Initializing class A again.

I know this is not best practice, and that I should be using a different  
name for my child class I am deriving off the superclass, but I was just  
wanting to know if it is actually doable.

Thanks.

---

<div class="post-metadata">

**Author:** ![Russell\_McConnachie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b5ac83/32.png) [@Russell\_McConnachie](https://www.ruby-forum.com/u/Russell_McConnachie)\
**Post date:** [January 9, 2008, 11:56pm UTC](https://www.ruby-forum.com/t/superclass-table-inheritance/125532/2 "2008-01-09T23:56:50Z")

</div>

Inheritance does it nicely:

class A  
def initalize  
puts “Initializing class A”  
end  
end

class B \< A  
def initialize  
super  
puts “Initalizing class B”  
end  
end

On Jan 10, 2008 8:41 AM, Russell McConnachie [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> def initialize  
> super
> 
> I know this is not best practice, and that I should be using a different  
> name for my child class I am deriving off the superclass, but I was just  
> wanting to know if it is actually doable.
> 
> Thanks.
> 
> >

–  
Ryan B.

> **[frozenplague.net](http://www.frozenplague.net)**
>
> This domain may be for sale!

Feel free to add me to MSN and/or GTalk as this email.

---

<div class="post-metadata">

**Author:** ![Russell\_McConnachie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b5ac83/32.png) [@Russell\_McConnachie](https://www.ruby-forum.com/u/Russell_McConnachie)\
**Post date:** [January 10, 2008, 12:03am UTC](https://www.ruby-forum.com/t/superclass-table-inheritance/125532/3 "2008-01-10T00:03:17Z")

</div>

Hi Ryan,

I was actually looking at using the same class name. I didn’t think it  
was possible but was wondering. In ruby you can override any method as  
you wish dynamically. So defining class A twice with two initialize  
functions will not work because the second defined class of the same  
name will override the first.

---

<div class="post-metadata">

**Author:** ![Russell\_McConnachie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b5ac83/32.png) [@Russell\_McConnachie](https://www.ruby-forum.com/u/Russell_McConnachie)\
**Post date:** [January 10, 2008, 12:07am UTC](https://www.ruby-forum.com/t/superclass-table-inheritance/125532/4 "2008-01-10T00:07:23Z")

</div>

Not quite override, more like extending.

Re-defining a class in Ruby extends that class, but redefining a method  
on a  
class will override that method, like what you’re finding out now.

On Jan 10, 2008 9:32 AM, Russell McConnachie [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > Inheritance does it nicely:  
> > puts “Initalizing class B”  
> > I am trying to find a way to preserve the same class name without  
> > end  
> > end  
> > different

–  
Ryan B.

> **[frozenplague.net](http://www.frozenplague.net)**
>
> This domain may be for sale!

Feel free to add me to MSN and/or GTalk as this email.

---

<div class="post-metadata">

**Author:** ![Russell\_McConnachie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b5ac83/32.png) [@Russell\_McConnachie](https://www.ruby-forum.com/u/Russell_McConnachie)\
**Post date:** [January 10, 2008, 1:33am UTC](https://www.ruby-forum.com/t/superclass-table-inheritance/125532/5 "2008-01-10T01:33:56Z")

</div>

On Jan 9, 2:11 pm, Russell McConnachie [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I know this is not best practice, and that I should be using a different  
> name for my child class I am deriving off the superclass, but I was just  
> wanting to know if it is actually doable.

No, there can only be one class by a given name, and it can’t derive  
from itself.

What is your goal?

///ark
