Static Methods in Classes

Static Methods in Classes

·

1 min read

Static methods in a class is defined as the functions or methods that can be used without making any object of the class. You can also use method overriding for such methods in an inherited class.

class Employee {
    constructor(givenName, givenExperience) {
        this.name = givenName;
        this.experience = givenExperience;
    }
    static add(a, b) {
        return a + b;
    }
}

class WebDeveloper extends Employee {
    constructor(givenName, givenExperience, favLanguage) {
        super(givenName, givenExperience);
        this.language = favLanguage
    }
    static add(a, b, c) {
        return a + b + c;
    }

}
console.log(Employee.add(5, 6));
console.log(WebDeveloper.add(5, 6, 7))

The output of the above snippet is;

11
18