Advantages:
A) End of Duplication of Code: In inheritance, we are creating parent/child relationship. With the help of this the child classes can use the members of parent class, instead of writing duplicate code in child classes. So, we can say that, this is a key benefit of inheritance.
B) Re-usability: Another main advantage of Inheritance is Re-usability. Because we can reuse all the member of base class except private members
C) Flexible to change: When we implement Inheritance in our application, the code is more flexible to alter, the reason is that we inherit our child classes from a base class, can be use interchangeably, if return type of child class method is base class.
D) Overriding: To providing a different implementation for functionality of an inherited method is called method overriding. With the help of overriding we can change the parent class method behavior according to child class method requirement or for output.
E) Overloading: If we wish to perform a same task on different parameters, like adding two int type variables or same things we want to perform on float data type. We add a method with the name of AddMethod() two times, once for int type data and second for float type data. because we are going two add the things. This is very useful to understand the task or things and also make sense. Overloading means that same name of method used with different parameters, sequence of parameters, number of parameters, types of parameters.
F) Different Views on Different Data: We can achieve abstract view level with the help of data encapsulation and data hiding (abstraction).
Disadvantage:
A) Performance: Time/Effort increase to execute the method of overloaded classes. Jump through all the levels. For example, if a class is a ten level abstraction above, then it will essentially take ten jumps to run through a function to define in each of those classes. Only in Constructors case.
B) Tightly Coupling: Parent and child classes are tightly coupled. Means that one can’t use independently each other.
C) Adding new Features: Also with time, during maintenance adding new features both base as well as derived classes are required to be changed. If a method signature is changed then we will be affected in both cases (inheritance & composition)
D) Deletion of Method's: If a method is deleted in the "super class" or aggregate, then we will have to re-factor in case of using that method. Here things can get a bit complicated in case of inheritance because our programs will still compile, but the methods of the subclass will no longer be overriding super class methods. These methods will become independent methods in their own right.
(Practice Makes a Man Perfect)
No comments:
Post a Comment