10 تغريدة 3 قراءة Apr 19, 2023
JavaScript Class Inheritance 🚀
👇🧵
Don't forget to bookmarked it for future reference...
Class Inheritance
• Inheritance is a useful feature that allows code reusability.
• Using class inheritance, a class can inherit all the methods and properties of another class.
• To use class inheritance, you use the extends keyword.
• Example :
• In the above example, the Student class inherits all the methods and properties of the Person class.
Hence, the Student class will now have the name property and the greet() method.
Then, we accessed the greet() method of Student class by creating a student1 object.
JavaScript super() keyword
• super keyword used inside a child class denotes its parent class.
• Example :
• In the above example, super inside Student class refers to the Person class.
Hence, when the constructor of Student class is called, it also calls the constructor of the Person class which assigns a name property to it.
Overriding Method/Property
• If a child class has the same method or property name as that of the parent class, it will use the method and property of the child class. This concept is called method overriding.
• Example :
• In the above example, the occupation property and the greet() method are present in parent Person class and the child Student class.
Hence, the Student class overrides the occupation property and the greet() method.
Uses of Inheritance
• Once a functionality is developed, you can simply inherit it. No need to reinvent the wheel. This allows for cleaner code and easier to maintain.
• Since you can also add your own functionalities in the child class.
End thread 🧵
If you liked this thread:
-RT the first thread and share it with folks on Twitter.
-Follow @personalvipin for more such insightful threads.
Thanks for reading!

جاري تحميل الاقتراحات...