What is Inheritance?
Inheritance is another important concept of OOPs. In this post we will discuss about another important OOPs concept i.e. "Inheritance". In general term it means something that is being inherited. When we write programs in OOPs languages like JAVA we can use this concept for creating relationships between classes. We can't write code for each and every entity repeating common functionality.
Consider a simple scenario, we need to write programs for managing employees now there are two categories of employees part time and full time. If we write two separate Class for managing these two categories then we need to repeat lot of code like managing basic details, showing details when required etc. Below are the sample code for both classes without inheritance.
In this scenario we need to identify which are common properties and behavior that both entities have and taking those properties and behavior we can create a "Super Class" or "Parent Class". We can clearly see in above classes only implementation of calculateSalary() is going to be different. For part time employees it is going to be based on number of hours whereas for full time employee salary is a fixed monthly amount.
So if we take out common properties and behaviors out and create a new class, we can create a "Supper Class" employee as shown below.
Why we have defined methods with public access specifier, because we want them to be available for other classes as well. Now we can create "Sub Class" using above class which will have all the properties and behavior(only public ones) from it. Additionally they can have specialized properties which will not be common.
In Java we have extends keyword for creating sub-classes. After implementing inheritance concept we will get following implementation of PartTimeEmployee and FullTimeEmployee.
As you can see our implementation got simpler now without any repetition. This is what inheritance is all about, creating generalized class having common properties and behavior and then extending that class for creating specialized classes.
Some important terms that we need to remember in relation to inheritance :
Super Class or Parent Class : This is a generalized class having common properties and behavior which can be inherited by other classes. This class can be extended using extends keyword.
Sub Class or Child Class : This is a specialized class which extends super class and have some specialized properties and behavior which are not there in super class.
There are different types of inheritance that we can come across :
Single Inheritance : This is the simplest form of inheritance. In this type of inheritance there is one parent class and one child class inherits from the parent class.
Multi-level Inheritance : In this type of inheritance child class can also be extended. Consider below example.
Hierarchical Inheritance : In this type of inheritance more than one sub class are created extending the same parent class. The example that we have done in starting of this post is this type of inheritance.
Multiple Inheritance : In this type of inheritance one sub class extends more than one parent class. But in Java multiple inheritance is not possible because extends keyword can only be applied to one parent class.
Note : From Java 8 onward there is possibility of multiple inheritance using interfaces by defining default methods.
From implementation point of view, there are two types of Inheritance :
1. Implementation inheritance (aka class inheritance): You can extend an application's functionality by reusing functionality in the parent class by inheriting all or some of the operations already implemented. In Java, you can only inherit from one superclass. Implementation inheritance promotes re-usability but improper use of class inheritance can cause programming nightmares by breaking encapsulation and making future changes a problem. With implementation inheritance, the subclass becomes tightly coupled with the superclass. This will make the design fragile because if you want to change the superclass, you must know all the details of the subclasses to avoid breaking them. So when using implementation inheritance, make sure that the subclasses depend only
on the behaviour of the superclass, not on the actual implementation.
2. Interface inheritance (aka type inheritance): This is also known as subtyping. Interfaces provide a mechanism for specifying a relationship between otherwise unrelated classes, typically by specifying a set of common methods each implementing class must contain. Interface inheritance promotes the design concept of program to interfaces not to implementations. This also reduces the coupling or implementation dependencies between systems. In Java, you can implement any number of interfaces. This is more flexible than implementation inheritance because it won’t lock you into specific implementations which make subclasses difficult to maintain. So care should be taken not to break the implementing classes by modifying the interfaces.
This is all for inheritance, we will discuss about polymorphism in next post.
Inheritance is another important concept of OOPs. In this post we will discuss about another important OOPs concept i.e. "Inheritance". In general term it means something that is being inherited. When we write programs in OOPs languages like JAVA we can use this concept for creating relationships between classes. We can't write code for each and every entity repeating common functionality.
Consider a simple scenario, we need to write programs for managing employees now there are two categories of employees part time and full time. If we write two separate Class for managing these two categories then we need to repeat lot of code like managing basic details, showing details when required etc. Below are the sample code for both classes without inheritance.
class PartTimeEmployee{
private String empName;
private String empId;
private void storeDetails(){}
private void showDetails(){}
private void calculateSalary(){}
}
class FullTimeEmployee{
private String empName;
private String empId;
private void storeDetails(){}
private void showDetails(){}
private void calculateSalary(){}
}
In this scenario we need to identify which are common properties and behavior that both entities have and taking those properties and behavior we can create a "Super Class" or "Parent Class". We can clearly see in above classes only implementation of calculateSalary() is going to be different. For part time employees it is going to be based on number of hours whereas for full time employee salary is a fixed monthly amount.
So if we take out common properties and behaviors out and create a new class, we can create a "Supper Class" employee as shown below.
class Employee{
private String empName;
private String empId;
public void storeDetails(){}
public void showDetails(){}
}
Why we have defined methods with public access specifier, because we want them to be available for other classes as well. Now we can create "Sub Class" using above class which will have all the properties and behavior(only public ones) from it. Additionally they can have specialized properties which will not be common.
In Java we have extends keyword for creating sub-classes. After implementing inheritance concept we will get following implementation of PartTimeEmployee and FullTimeEmployee.
class PartTimeEmployee extends Employee{
private void calculateSalary(){}
}
class FullTimeEmployee extends Employee{
private void calculateSalary(){}
}
As you can see our implementation got simpler now without any repetition. This is what inheritance is all about, creating generalized class having common properties and behavior and then extending that class for creating specialized classes.
Some important terms that we need to remember in relation to inheritance :
Super Class or Parent Class : This is a generalized class having common properties and behavior which can be inherited by other classes. This class can be extended using extends keyword.
Sub Class or Child Class : This is a specialized class which extends super class and have some specialized properties and behavior which are not there in super class.
There are different types of inheritance that we can come across :
Single Inheritance : This is the simplest form of inheritance. In this type of inheritance there is one parent class and one child class inherits from the parent class.
Multi-level Inheritance : In this type of inheritance child class can also be extended. Consider below example.
class A{
//this is the parent class
}
class B extends A{
//this is child class of A
}
class C extends B{
//this is child class of B
}
Hierarchical Inheritance : In this type of inheritance more than one sub class are created extending the same parent class. The example that we have done in starting of this post is this type of inheritance.
Multiple Inheritance : In this type of inheritance one sub class extends more than one parent class. But in Java multiple inheritance is not possible because extends keyword can only be applied to one parent class.
Note : From Java 8 onward there is possibility of multiple inheritance using interfaces by defining default methods.
From implementation point of view, there are two types of Inheritance :
1. Implementation inheritance (aka class inheritance): You can extend an application's functionality by reusing functionality in the parent class by inheriting all or some of the operations already implemented. In Java, you can only inherit from one superclass. Implementation inheritance promotes re-usability but improper use of class inheritance can cause programming nightmares by breaking encapsulation and making future changes a problem. With implementation inheritance, the subclass becomes tightly coupled with the superclass. This will make the design fragile because if you want to change the superclass, you must know all the details of the subclasses to avoid breaking them. So when using implementation inheritance, make sure that the subclasses depend only
on the behaviour of the superclass, not on the actual implementation.
2. Interface inheritance (aka type inheritance): This is also known as subtyping. Interfaces provide a mechanism for specifying a relationship between otherwise unrelated classes, typically by specifying a set of common methods each implementing class must contain. Interface inheritance promotes the design concept of program to interfaces not to implementations. This also reduces the coupling or implementation dependencies between systems. In Java, you can implement any number of interfaces. This is more flexible than implementation inheritance because it won’t lock you into specific implementations which make subclasses difficult to maintain. So care should be taken not to break the implementing classes by modifying the interfaces.
This is all for inheritance, we will discuss about polymorphism in next post.
No comments:
Post a Comment