Thursday, 6 June 2024

Inheritance

Inheritance in C#/Java/python is a fundamental concept of Object-Oriented Programming (OOP). It allows one class to inherit the properties and methods of another class. Inheritance provides code re-usability. In place of writing the same code, again and again, we can simply inherit the properties of one class into the other. Code reuse can reduce time and errors The members defined in one class can be consumed by another class by establishing a parent/child relationship between the classes. Main features of inheritance : Code Reusablility :- when existing functionality is required in your new class(new version of exiting object), then inherit it instead of writing duplicate code. Extensibility :- Maintainability Notes : Base classes are automatically instantiated before derived classes. Parent Class constructor executes before child class constructor. C# supports only single class inheritance. Multiple Inheritance not possible with normal class, can be achieve through interface. child class is a specialization of base class. Inheritance in C#/Java/python is a fundamental concept of Object-Oriented Programming (OOP). It allows one class to inherit the properties and methods of another class. The key to understanding Inheritance is that it provides code re-usability. In place of writing the same code, again and again, we can simply inherit the properties of one class into the other. Inheritance is a technique of modelling real-world relationships,OOP is all about real-world objects. one class inherits from another features : Code Reusablility :-The Child/derived/sub class inherits the fields and methods of the Parent/base/super class. This helps with the code reusability in C#. extensibility maintainability :- Code Organization and Maintenance An "is-a" link between classes is established by inheritance, where a subclass is a customized version of its superclass. Disadvantages of Inheritance Inherited functions work slower than normal function as there is indirection. Improper use of inheritance may lead to wrong solutions. Often, data members in the base class are left unused which may lead to memory wastage. Inheritance increases the coupling between base class and derived class. A change in base class will affect all the child classes. OOP supports six different types of inheritance as given below: Single Inheritance Multi-level Inheritance Multiple Inheritance Multipath Inheritance Hierarchical Inheritance Hybrid Inheritance Terms Used in inheritance Class: A class is a collection of objects with similar attributes. It's a blueprint or template from which items are made. Sub Class/Child Class: Subclass is a class which inherits the other class. It is also called a derived class, extended class, or child class. Super Class/Parent Class: The features of a subclass are inherited from the superclass. It's also known as a parent class or a base class. Reusability: Reusability, as the name implies, is a feature that allows you to reuse the fields and methods of an existing class while creating a new one. The fields and methods defined in the preceding class can be reused. Inheritance is one of the fundamental attributes of object-oriented programming. It allows you to define a child class that reuses (inherits), extends, or modifies the behavior of a parent class. The class whose members are inherited is called the base class. The class that inherits the members of the base class is called the derived class. https://learn.microsoft.com/en-us/dotnet/csharp/fundamentals/tutorials/inheritance https://www.programiz.com/csharp-programming/inheritance#google_vignette

No comments:

Post a Comment

Abstraction

Real Meaning of abstract :- abstract is summary of you entire implementation & methods. abstract is short info of you lengthy code and ...