Skip to content

Latest commit

 

History

History
41 lines (32 loc) · 646 Bytes

polymorphism.md

File metadata and controls

41 lines (32 loc) · 646 Bytes

Polymorphism

Polymorphism (having different functions at multiple levels of an inheritance chain with the same name).

//Pseudo code
class Vehicle {
	engines = 1

	ignition() {
		output( "Turning on my engine." )
	}

	drive() {
		ignition()
		output( "Steering and moving forward!" )
	}
}

class Car inherits Vehicle {
	wheels = 4

	drive() {
		inherited:drive()
		output( "Rolling on all ", wheels, " wheels!" )
	}
}

class SpeedBoat inherits Vehicle {
	engines = 2

	ignition() {
		output( "Turning on my ", engines, " engines." )
	}

	pilot() {
		inherited:drive()
		output( "Speeding through the water with ease!" )
	}
}