Class
May 20, 2023
A class is a blueprint or a template for creating objects in programming languages like Javascript. It defines a set of attributes and behaviors that an object will have, and how it should interact with other objects. Each object created from a class is known as an instance of that class.
Classes are used in object-oriented programming (OOP) to organize code and make it more reusable. They also help to encapsulate data and functionality, meaning that the internal workings of an object can be hidden from the rest of the program, making it more secure and easier to maintain.
Creating a Class
To create a class, we use the class
keyword followed by the name of the class. The name of the class should start with a capital letter and follow the same naming conventions as variables.
class MyClass {
// class definition
}
Inside the class definition, we can define attributes and methods that will be shared by all instances of the class. Attributes are variables that hold data, while methods are functions that perform actions.
Attributes
To define an attribute, we use the constructor
method. This method is automatically called when an object is created from the class and is used to set the initial values of the attributes.
class Person {
constructor(name, age) {
this.name = name;
this.age = age;
}
}
In the above example, we have defined a Person
class with two attributes – name
and age
. When an object is created from the Person
class, we pass in values for these attributes and they are set using the this
keyword. The this keyword refers to the object that is being created.
const person1 = new Person('John', 30);
console.log(person1.name); // "John"
console.log(person1.age); // 30
We create a new Person
object using the new
keyword, passing in values for the name
and age
attributes. We can then access these attributes using dot notation.
Methods
Methods are functions that are defined inside the class, and can be used to perform actions on the object’s attributes. To define a method, we simply add it to the class definition.
class Person {
constructor(name, age) {
this.name = name;
this.age = age;
}
sayHello() {
console.log(`Hello, my name is ${this.name}`);
}
}
In the above example, we have defined a sayHello
method that simply logs a message to the console using the object’s name
attribute. We can then call this method on a Person
object.
const person1 = new Person('John', 30);
person1.sayHello(); // "Hello, my name is John"
Inheritance
Inheritance is a key feature of OOP that allows classes to inherit attributes and behaviors from other classes. This helps to reduce code duplication and makes it easier to extend existing code.
To create a subclass that inherits from a parent class, we use the extends
keyword.
class Student extends Person {
constructor(name, age, grade) {
super(name, age);
this.grade = grade;
}
sayHello() {
console.log(`Hello, my name is ${this.name} and I'm in grade ${this.grade}`);
}
}
In the above example, we have defined a Student
subclass that inherits from the Person
superclass. We have also added a grade
attribute that is unique to the Student
class.
By calling the super
method inside the constructor
, we are calling the constructor
of the Person
class, and passing in the name
and age
attributes. This allows us to reuse code from the parent class.
We have also overridden the sayHello
method to add the grade
attribute to the message.
const student1 = new Student('Jane', 16, 11);
student1.sayHello(); // "Hello, my name is Jane and I'm in grade 11"
We create a new Student
object, passing in values for the name
, age
, and grade
attributes. We can then call the sayHello
method on the student1
object, which logs a message that includes the name
and grade
attributes.
Static Methods
Static methods are methods that belong to the class itself, rather than to an instance of the class. They can be called on the class without creating an object first.
To define a static method, we use the static keyword.
class MathUtils {
static add(a, b) {
return a + b;
}
}
In the above example, we have defined a MathUtils
class with a static add
method that returns the sum of two numbers.
We can then call the add
method directly on the MathUtils
class.
console.log(MathUtils.add(2, 3)); // 5