These days, I'm working on my Java programming skills especially object-oriented programming. I have taken a Coursera course for this purpose from UC San Diego. Today, I happened to come across an interesting example from Leo Porter demonstrating the need of inheritance in object-oriented programming. His explanation went as follows:
Inheritance:
Suppose you are working as a Software Engineer in a university. You have written a Person class that provides a blue-print for information of all individuals and it's working as expected. After a few days, the university administration comes to you saying we cannot have the same class for all individuals. We need to have separate classes for students and faculty respectively. One of the options to tackle this problem is to use a boolean operator and set if-else conditions for every method in your class.
Okay, so that works fine for a few days as well. Again, the university administration comes to you saying we cannot have the same class for all the students. We need separate classes for undergrad and grad students. Now, maybe you go for a nested if-else condition. But again you are asked to sort ful-time and part-time students. Now a nested if-else starts getting complicated.
Further, you are being told that you cannot have the same methods for students and faculty. Now you create two classes and name them Students and Faculty respectively. What do you think can go wrong with this?
The problem comes when you have to modify the basic structure of these classes. For example, earlier you had a single variable for storing name of a person. And now you have to store first name and last name in two separate variables. So now you have to make similar changes in both classes. The required changes might be necessary at multiple lines inside a class and the same have to be done line-by-line in another class as well. This is where the job gets tedious.
The solution to this is inheritance! Inheritance allows you to keep some common functionality in the parent class (say Person) and have distinct functionalities in child classes (Students and Faculty in this case).
Inheritance:
Suppose you are working as a Software Engineer in a university. You have written a Person class that provides a blue-print for information of all individuals and it's working as expected. After a few days, the university administration comes to you saying we cannot have the same class for all individuals. We need to have separate classes for students and faculty respectively. One of the options to tackle this problem is to use a boolean operator and set if-else conditions for every method in your class.
Okay, so that works fine for a few days as well. Again, the university administration comes to you saying we cannot have the same class for all the students. We need separate classes for undergrad and grad students. Now, maybe you go for a nested if-else condition. But again you are asked to sort ful-time and part-time students. Now a nested if-else starts getting complicated.
Further, you are being told that you cannot have the same methods for students and faculty. Now you create two classes and name them Students and Faculty respectively. What do you think can go wrong with this?
The problem comes when you have to modify the basic structure of these classes. For example, earlier you had a single variable for storing name of a person. And now you have to store first name and last name in two separate variables. So now you have to make similar changes in both classes. The required changes might be necessary at multiple lines inside a class and the same have to be done line-by-line in another class as well. This is where the job gets tedious.
The solution to this is inheritance! Inheritance allows you to keep some common functionality in the parent class (say Person) and have distinct functionalities in child classes (Students and Faculty in this case).
No comments:
Post a Comment