An Object is a unique entity that contains properties like.
- Variable type properties
- Methods properties
- Object properties
- Array properties
- And more....
let person = {
//Properties
first_name: 'Jhon',
last_name: 'Dee',
//method
getName:()=> {
return (`The name of the person is ${person.first_name} ${person.last_name}`)
}
}
console.log(person.getName());
Classes are blueprints of an Object
- A class can have many Objects because
- Class is a template while Objects are instances of the class
- Using let or var to declare variables inside a class is unnecessary because class properties are automatically scoped to the class instance and don't require explicit variable declarations
class Person {
//Properties
first_name='Jhon'
last_name='Dee'
//method
getName() {
return (`The name of the person is ${this.first_name} ${this.last_name}`)
}
}
const person1 = new Person();
console.log(person1.getName());
Class constructor is a magic method
- Constructor execute automatically when object is created
- Constructor can take parameter
- Constructor method can't return any result
class Person {
constructor() {
console.log('I am a constructor')
}
}
const person1 = new Person();
class Person {
constructor(msg) {
console.log(msg)
}
}
const person1 = new Person('I am a constructor');
Class constructor is a magic method
- The static keyword is used to define a static method or property of a class.
- To call the static method we do not need to create an instance or object of the class.
- We create a static variable in JavaScript to prevent replication
- Fixed configuration, and it is also useful for caches
class Person {
//Properties
static first_name='Jhon'
static last_name='Dee'
//method
static getName() {
return (`The name of the person is ${this.first_name} ${this.last_name}`)
}
}
console.log(Person.getName());
- Keyword "extends" is used to create inherit relationship between class
- For inherit relationship child class can use parent class properties
class Father {
//Properties
first_name='Jhon'
last_name='Dee'
//method
getName() {
return (`The name of the person is ${this.first_name} ${this.last_name}`)
}
}
class Son extends Father{
}
const SonObj = new Son();
console.log(SonObj.getName());
- Overriding works for inheritance relationship
- When child class change his parent properties , that is overriding
class Father {
//Properties
first_name='Jhon'
last_name='Dee'
//method
getName() {
return (`The name of the person is ${this.first_name} ${this.last_name}`)
}
}
class Son extends Father{
// Overriding Occurs
first_name='Jhon Junior'
}
const SonObj = new Son();
console.log(SonObj.getName());
- Method overloading allows you to define multiple methods with the same name but different parameter lists
- JavaScript does not natively support method overloading
- The polymorphism is a core concept of an object-oriented paradigm that provides a way to perform a single action in different forms.
- We already know about Inheritance,Overriding,Overloading - those concepts together represent polymorphism
- Keyword "abstract" use for class abstraction
- You can't create object for abstract class
- Abstract class Always needs its inherited child class to perform
- JavaScript does not have built-in support for abstract