{"id":26828013,"url":"https://github.com/clifftech123/oop-in-typescript","last_synced_at":"2025-03-30T12:18:23.706Z","repository":{"id":284770988,"uuid":"620018216","full_name":"Clifftech123/oop-in-typescript","owner":"Clifftech123","description":null,"archived":false,"fork":false,"pushed_at":"2023-04-05T20:41:04.000Z","size":7968,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-27T15:55:49.281Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Clifftech123.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-03-27T21:40:58.000Z","updated_at":"2023-04-02T23:02:10.000Z","dependencies_parsed_at":"2025-03-27T15:55:52.204Z","dependency_job_id":"95adc3eb-d851-431d-8aaf-7b88f71766af","html_url":"https://github.com/Clifftech123/oop-in-typescript","commit_stats":null,"previous_names":["clifftech123/oop-in-typescript"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Clifftech123%2Foop-in-typescript","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Clifftech123%2Foop-in-typescript/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Clifftech123%2Foop-in-typescript/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Clifftech123%2Foop-in-typescript/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Clifftech123","download_url":"https://codeload.github.com/Clifftech123/oop-in-typescript/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246314142,"owners_count":20757463,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-03-30T12:18:23.246Z","updated_at":"2025-03-30T12:18:23.695Z","avatar_url":"https://github.com/Clifftech123.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Object oriented Programming in TypeScript\n\n\n This repo cover a range of topics  of object oriented programming in TypeScript. \n\n## Topics\nThe topics the 4 pillars of object oriented programming are covered in this repo. They are:\n\n- Encapsulation\n- Inheritance\n- Polymorphism\n- Abstraction\n\n\n## Encapsulation: \n\nencapsulation is the process of binding data and methods together in a single unit. It is also known as data hiding. It is used to hide the implementation details from the user. It is used to achieve security and data integrity.\n\nEncapsulation is one of the fundamental principles of object-oriented programming. It involves hiding the implementation details of a class from the outside world, and providing a public interface for interacting with the class. This can help to keep the code modular, maintainable, and flexible.\n\nIn TypeScript, encapsulation can be achieved by using access modifiers on class properties and methods. There are three access modifiers in TypeScript:\n\npublic: The property or method is accessible from anywhere, both inside and outside the class.\nprivate: The property or method is only accessible from within the class. It cannot be accessed from outside the class, not even from subclasses.\nprotected: The property or method is only accessible from within the class and its subclasses. It cannot be accessed from outside the class hierarchy.\n\n here is an example of encapsulation in TypeScript:\n\n```typescript\n\nclass Person {\n    private name: string;\n    private age: number;\n\n    constructor(name: string, age: number) {\n        this.name = name;\n        this.age = age;\n    }\n\n    getName(): string {\n        return this.name;\n    }\n\n    getAge(): number {\n        return this.age;\n    }\n}\n\nconst person = new Person('John', 20);\n\nconsole.log(person.getName()); // John\nconsole.log(person.getAge()); // 20\n\n```\n\nin the above example, the name and age properties are private. This means that they can only be accessed within the class. The getName() and getAge() methods are public. This means that they can be accessed from outside the class. The getName() and getAge() methods are used to access the private properties.\n\n\n## Inheritance:\n\n Inheritance is the process of deriving a new class from an existing class. The new class is called the derived class and the existing class is called the base class. The derived class inherits the properties and methods of the base class. The derived class can also add its own properties and methods.\n Inheritance is another fundamental principle of object-oriented programming. It involves creating a hierarchy of classes, where subclasses inherit properties and methods from their parent classes. This can help to keep the code organized, reusable, and extensible.\n\nIn TypeScript, inheritance can be achieved by using the extends keyword to create a subclass that inherits from a parent class. The subclass can then add its own properties and methods, or override the ones inherited from the parent class\n here is an example of inheritance in TypeScript:\n\n```typescript\n\nclass Person {\n\n    // base class properties\n    private name: string;\n    private age: number;\n\n// constructor of the base class\n    constructor(name: string, age: number) {\n        this.name = name;\n        this.age = age;\n    }\n\n// methods to access the private properties\n    getName(): string {\n        return this.name;\n    }\n\n// methods to access the private properties\n    getAge(): number {\n        return this.age;\n    }\n}\n\n// derived class Student from base class Person\nclass Student extends Person {\n    private studentId: number;\n\n    constructor(name: string, age: number, studentId: number) {\n        super(name, age);\n        this.studentId = studentId;\n    }\n\n// methods to access the private properties\n    getStudentId(): number {\n        return this.studentId;\n    }\n}\n\nconst student = new Student('John', 20, 12345);\n\nconsole.log(student.getName()); // John\n\nconsole.log(student.getAge()); // 20\n\nconsole.log(student.getStudentId()); // 12345\n\n```\n\nin the above example, the Student class is derived from the Person class. The Student class inherits the properties and methods of the Person class. The Student class also has its own properties and methods.\n\n\n## Polymorphism:\n\n Polymorphism is the ability of an object to take on many forms. The most common use of polymorphism in OOP occurs when a parent class reference is used to refer to a child class object.\n\n here is an example of polymorphism in TypeScript:\n\n```typescript\n\nclass Person {\n\n    // base class properties\n    private name: string;\n    private age: number;\n\n    constructor(name: string, age: number) {\n        this.name = name;\n        this.age = age;\n    }\n\n// methods to access the private properties\n    getName(): string {\n        return this.name;\n    }\n\n\n// methods to access the private properties\n    getAge(): number {\n        return this.age;\n    }\n}\n\n\n// derived class Student from base class Person\nclass Student extends Person {\n    private studentId: number;\n\n    constructor(name: string, age: number, studentId: number) {\n        super(name, age);\n        this.studentId = studentId;\n    }\n\n// methods to access the private properties\n    getStudentId(): number {\n        return this.studentId;\n    }\n}\n\n// person and student are of type Person\nconst person = new Person('John', 20);\n\nconst student = new Student('John', 20, 12345);\n\n// person and student are of type Person\nfunction printPerson(person: Person) {\n    console.log(person.getName());\n    console.log(person.getAge());\n}\n\n\n// person and student are of type Person\nprintPerson(person); // John 20\n\nprintPerson(student); // John 20\n\n```\n\n\n## Abstraction:\n\n Abstraction is the process of hiding the implementation details from the user. It is used to achieve security and data integrity. It is achieved by using access modifiers. There are three access modifiers in TypeScript: public, private and protected.\n\n Abstraction is an important concept in object-oriented programming (OOP) that allows you to hide the implementation details of a class or object from the user of that class or object. In TypeScript, abstraction can be achieved through the use of abstract classes and interfaces.\n\nAbstract Classes:\nAn abstract class is a class that cannot be instantiated directly and is meant to be subclassed. Abstract classes are typically used to define a common interface for a set of subclasses, without implementing all the methods required for those subclasses. Abstract classes can define abstract methods, which must be implemented by their subclasses. Abstract classes can also define concrete methods, which can be called by subclasses.\n\n here is an example of abstraction in TypeScript:\n\n```typescript\n\n\nclass Person {\n\n    // base class properties\n    private name: string;\n    private age: number;\n\n    constructor(name: string, age: number) {\n        this.name = name;\n        this.age = age;\n    }\n\n// methods to access the private properties\n\n    getName(): string {\n        return this.name;\n    }\n\n// methods to access the private properties\n\n    getAge(): number {\n        return this.age;\n    }\n}\n\nconst person = new Person('John', 20);\n\nconsole.log(person.getName()); // John\n\nconsole.log(person.getAge()); // 20\n\n```\n\nin the above example, the name and age properties are private. This means that they can only be accessed within the class. The getName() and getAge() methods are public. This means that they can be accessed from outside the class. The getName() and getAge() methods are used to access the private properties.\n\n## Conclusion\n\nIn this repo, we have covered the 4 pillars of object oriented programming in TypeScript. They are:\n\n- Encapsulation\n- Inheritance\n- Polymorphism\n- Abstraction\n\n## Author\n\n- [Twitter](https://twitter.com/Clifftech_Dev)\n- [Github](hhhttps://github.com/Clifftech123)\n- [LinkedIn](https://www.linkedin.com/in/isaiah-clifford-opoku-a506a51b2/)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fclifftech123%2Foop-in-typescript","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fclifftech123%2Foop-in-typescript","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fclifftech123%2Foop-in-typescript/lists"}