-
Notifications
You must be signed in to change notification settings - Fork 0
Inheritance
Inheritance is one of the major features of OOP. Aussom supports inheritance for both Aussom and external classes. Inheritance lets you define a super class and then create sub classes that inherit members and functions from the parent class. Lets start with a simple example.
In the example below, we have a super class 'animal' and a sub class of 'dog'. Notice in the class definition of dog that there is a colon and a reference to animal after it. This tells Aussom that dog is a subclass of animal. This will make all members and functions of animal available to dog.
// Super class.
class animal {
// Animal actions
public run() { sys.println("running ..."); }
public eat() { sys.println("eating ..."); }
public sleep() { sys.println("sleeping ..."); }
}
// Subclass of animal.
class dog : animal {
public bark() { sys.println("woof! ..."); }
public wag() { sys.println("wagging tail ..."); }
}
// So we can then do this.
myDog = new dog();
myDog.run(); // Defined in animal class.
myDog.eat(); // Defined in animal class.
myDog.bark(); // Defined in dog class.
The above example is great, but in Aussom we can inherit from multiple classes. The example below show dog inheriting from class animal and carnivore.
// Super class carnivore.
class carnivore {
public hunt() { sys.println("you don't see me ..."); }
}
// Subclass of animal and carnivore.
class dog : animal, carnivore {
public bark() { sys.println("woof! ..."); }
public wag() { sys.println("wagging tail ..."); }
}
// So we can do this then.
myDog = new dog();
myDog.run(); // Defined in animal class.
myDog.hunt(); // Defined in carnivore class.
myDog.bark(); // Defined in dog class.
Note: When inheriting from an external class, you may only inherit from that class at this time. More on that in the external classes and functions section.