- Published on
4 Principles of Object-Oriented Programming
- Authors
- Name
- Khánh
4 Principles of Object-Oriented Programing
Abstraction
Abstraction is the ability of a program to focus on essential aspects while ignoring irrelevant details. It means concentrating on core values that are necessary for the task at hand.
Abstraction is demonstrated by the idea that an original object may share common characteristics with many other objects as an extension of itself. However, the original object may not have specific methods.
Abstract classes are mainly for inheritance where other classes may derive from them. We cannot create an instance of an abstract class.
An abstract class typically includes one or more abstract methods or property declarations. The class which extends the abstract class must define all the abstract methods.
abstract class Person {
name: string;
abstract age: number;
constructor(name: string){
this.name = name;
}
displayName(): void{
console.log(this.name);
}
abstract find(name: string): Person;
}
class Employee extends Person {
empCode: number;
age: number;
constructor(name: string, code: number) {
super(name); // must call super()
this.empCode = code;
this.age = 20;
}
find(name:string): Person {
return new Employee(name, 1);
}
}
let emp: Person = new Employee("James", 100);
emp.displayName(); //James
Encapsulation
Encapsulation is the act of hiding information, preventing the user from altering the internal attributes and methods of an object. How much access users have to internal values and methods depends on the programmer.
class Person {
public name: string;
private age: number;
protected gen: string;
constructor(name: string, age: number){
this.name = name;
this.age = age;
this.gen = "ADN";
}
}
const khanhPerson = new Person("khanh", 24);
console.log(khanhPerson.name);
// "khanh"
khanhPerson.age =12;
// Property 'age' is private and only accessible within class 'Person'.(2341)
console.log(khanhPerson.age);
// Property 'age' is private and only accessible within class 'Person'.(2341)
Inheritance
In object-oriented programming (OOP), inheritance is the process of inheriting the attributes and methods of parent classes to facilitate easy extension and avoid code duplication.
class Person {
public name: string;
constructor(name: string){
this.name = name;
}
public getName(){
return this.name;
}
}
class KhanhPerson extends Person {
constructor(name: string){
super(name);
}
}
const khanhPerson = new KhanhPerson("Q.Khanh");
console.log(khanhPerson.getName());
Polymorphism
Polymorphism refers to the ability to perform an action in multiple ways.
Method overloading: This involves having multiple methods with the same name but different parameters.
class student{
Foo(value : string ) : void {};
Foo(value : number) : void {};
Foo(value : number, value2 : string) : void {};
Foo(value2 : string, value : number) : void {};
}
Method overriding is the process of creating a method in a child class that has the same name as a method in its parent class but provides a different implementation.
class Parent{
constructor() {}
Foo(value : any) {}
}
class Child extends parent{
constructor() { super(); }
Foo(value : any ) {}
}