HomeToolsAbout

Object Oriented Programming

Class

What is a Constructor

Constructor enables you to provide a custom initialization that must be done before any other methods can be called on an instantiated object

If you don't provide a constructor, empty default constructor is provided

constructor() {}

Default constructor inheritance and role of super()

If your class is a derived class, the default constructor calls the parent constructor, passing any arguments that were provided:

// default when no constructor provided constructor(...args) { super(...args); }

When a constructor is provided to a child class, you must call the parent class constructor explicitly using super()

Implications of super()

When a method in a class has a super(), it means the superclass of the current class has a equivalent method it inherits from

So you need to understand what the parent’s method is doing

Class is an uninstantiated state

The methods in class are not accessible unless it is instantiated

  • A way around this is by assigning self.method to class. Which makes the method belong to a class itself without having to be instantiated.
AboutContact