Class Inheritance

Neok of recently asked the question:


Just a quick question. If say in the original script, a class is defined as follows:
class Window_ChoiceList < Window_Command
and later under Materials, I define it as follows:
class Window_ChoiceList
What changes inside it, now that I’ve removed the ‘Window_Command’ inheritance?


To which my answer was…


If the normal code (ie, class Window_ChoiceList < Window_Command) is still there, it will not ‘affect’ anything.  If you have removed the original code, there would be no inheritance when the code is initialized. For example :

class Game_Actor
  def example_method
    p "Just showing text"

Will simply add a new method into the game actor class, providing the Game_Actor class has been previously defined, it does not remove Game_Actors inheritance from Game_Battler.


DRS of then stated:


That’s interesting! I thought it would be over written since it wasn’t making the original inheritance call.


This prompted me to write a short explanation of inheritance and how to include new inheritance within your classes. So here it is…


As long the inheritance is being applied at ‘some point’ along the line of code it will work. For example…

I have defined this code within my projects script editor…

class Person
  def intialize
    @name = "Newborn"
    @age  = 1
  def name
  def age

class Dekita < Person
  def intialize
    @name = "Dekita"
    @age  = 24

This creates and defines the ‘Dekita’ class. Which inherits from ‘Person’ class. Now, I want to add a new method into ‘Dekita’ class called ‘gender’.

class Dekita
  alias :init_alias :initialize
  def initialize
    @gender = "??"
  def gender

As you can see, I have removed the class inheritance from my new methods code, but because its already defined prior to my new modification, it does not affect any inheritance already included. This can be tested by calling the code below (when the code from above is also there);

dekita =
p dekita.age
p dekita.gender

Also – lets say we wanted to include a new ‘inheritance’. We can do this by creating a new module and including it in any class we choose, for this example, I am placing it within the ‘Person’ class.. This will mean even descendants of ‘Person’ will be entitled to access the methods.

module Person_New
  def new_stuff
    return "New Stuff"
class Person
  include Person_New

Can be tested by calling.

p dekita.new_stuff

For convenience, I have included all the code from above in one handy script – ready to put in your script editor and mess around with to your hearts content. If can be found HERE 🙂


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s