Skip to main content

Inheritance

Inheritance is one of the most powerful and foundational concepts in object-oriented programming (OOP). It allows you to create new classes based on existing classes, making your code more organized, reusable, and maintainable. In EasyBite, inheritance is implemented in a very simple and readable way, making it especially friendly for beginners.


Table of Contents


What Is Inheritance?

Inheritance allows you to define a new class that inherits the attributes and behaviors (methods) of an existing class. This new class is called the child class (or subclass), while the existing class is called the parent class (or superclass).

This means the child class has access to everything the parent class has, unless it explicitly overrides it.

Real-life Analogy:

Imagine a general class called Animal. All animals can eat and sleep. Now we want to define a Dog. Instead of writing the eating and sleeping behavior again, we just make Dog inherit from Animal.


Why Use Inheritance?

Here are some important benefits of inheritance:

  • Code Reusability: Write common code once in the parent class and reuse it in multiple child classes.
  • Organization: Keep your code clean and structured. Similar classes can share a base class.
  • Extensibility: Easily extend base behavior without touching the original class.
  • Readability: Helps new developers understand relationships between classes.

Inheritance Syntax

In EasyBite, you use the inherit keyword to make one class inherit another.

class ChildClass inherit ParentClass
// child-specific code
end class

Example:

class Animal
method speak()
show("Animal makes a sound")
end method
end class

class Dog inherit Animal
end class

set mydog to new Dog()
mydog.speak()

Output:

Animal makes a sound

How Inheritance Works

When a child class inherits from a parent class:

  • It automatically gains all public methods and attributes of the parent.
  • It can override (replace) any method it wants.
  • It can add new methods or attributes of its own.
  • It cannot access secret (private) methods or attributes from the parent.

Calling Parent Methods

Sometimes, you want to override a method but still make use of the parent’s method. EasyBite allows this by using the parent keyword.

Example:

class Animal
method speak()
show("Animal sound")
end method
end class

class Dog inherit Animal
method speak()
show("Dog says:")
parent.speak()
end method
end class

set pet to new Dog()
pet.speak()

Output:

Dog says:
Animal sound

Here, Dog overrides the speak() method but still calls the speak() method from Animal using parent.speak().


Calling Parent Constructor

If the parent class has an init method (constructor), you can also call it from the child class using:

parent.init()

This is useful if the parent class performs some important setup that should not be skipped.

Example:

class Vehicle
init()
show("Vehicle initialized")
end init
end class

class Car inherit Vehicle
init()
show("Car setup begins")
parent.init()
show("Car initialized")
end init
end class

set c to new Car()

Output:

Car setup begins
Vehicle initialized
Car initialized

Overriding Methods

If a child class defines a method with the same name as the one in the parent class, it will override the parent version.

Example:

class Printer
method print()
show("Printing from Printer")
end method
end class

class PDFPrinter inherit Printer
method print()
show("Printing from PDFPrinter")
end method
end class

set printer to new PDFPrinter()
printer.print()

Output:

Printing from PDFPrinter

Even though PDFPrinter inherits from Printer, it replaces the print() method with its own version.


Examples with Output

Example 1: Basic Inheritance

class Person
method greet()
show("Hello!")
end method
end class

class Student inherit Person
end class

set st to new Student()
st.greet()

Output:

Hello!

Example 2: Adding New Methods in Child

class Animal
method eat()
show("Eating food")
end method
end class

class Cat inherit Animal
method meow()
show("Meow!")
end method
end class

set kitty to new Cat()
kitty.eat()
kitty.meow()

Output:

Eating food
Meow!

Example 3: Overriding and Using parent

class Notification
method send()
show("Sending general notification")
end method
end class

class EmailNotification inherit Notification
method send()
show("Sending email:")
parent.send()
end method
end class

set email to new EmailNotification()
email.send()

Output:

Sending email:
Sending general notification

Best Practices

  • Use inheritance when classes share common behaviors or properties.
  • Avoid deep inheritance trees (more than 3 levels) to keep things simple.
  • Use parent.method() or parent.init() when you want to extend, not replace.
  • Avoid inheriting just for the sake of reusing code — prefer composition in such cases.

Conclusion

Inheritance in EasyBite helps you write smarter, cleaner, and more organized code by reusing and extending behavior from parent classes. The inherit keyword connects child classes with parents, while parent.method() and parent.init() allow access to the parent’s logic when needed.

By mastering inheritance, you will write code that is not only easier to understand and maintain but also ready to scale as your projects grow.