Inheritance in Java.
Inheritance
1.The process of getting property of one class into another class is called Inheritance.
2.In other word we can say that the process of deriving a new class from an old class is called inheritance in which the new class is called derived or child or sub class and old class is called Base or Parent or Super class.
3.When a class inherits the property of a class it means it can access all the data member and meber function of that class except private element.
3.In this type of programming mainly two types of classes are used.
- Parent/Super/Base class
- Child/Sub/Derived class
Parent/Super/Base class
The class which is inherited by another class is called Parent or Super or Base class.
Child/Sub/Derived class
The class which inherits the property of another class is called Child or Sub or Derived class.
How to inherit one class into another
Derived class extends Base class
Example
class Subtraction extends Addition
Here Subtraction is a Derived class and Addition is a Base class and extends is a keyword which is used to inherit one class into another.
Example
//Base class class Addition { void add() { int x,y=30,z=10; x=y+z; System.out.println("Add="+x); } } //Derived class extending base class class Subtraction extends Addition { void sub() { int x,y=30,z=10; x=y-z; System.out.println("Sub="+x); } } class Easy { public static void main(String[] args) { //Creating instance(object) Subtraction obj=new Subtraction(); //calling base class method obj.add(); //calling derived class method obj.sub(); } } /* ### Output ### Add=40 Sub=20 */
Here class Addition is a base class and Subtraction is a derived class because Addition is inherited into Subtraction therefore we can call all the function using object of Subtraction.
Types of Inheritance
There are five types of inheritance in Java.
- Single Inheritance
- Multiple Inheritance
- Multilevel Inheritance
- Hierarchical Inheritance
- Hybrid Inheritance
Single Inheritance
In this types of inheritance only two classes are used in which one is inherited by another.//Base class class Addition { void add() { int x,y=30,z=10; x=y+z; System.out.println("Add="+x); } } //Derived class extending base class class Subtraction extends Addition { void sub() { int x,y=30,z=10; x=y-z; System.out.println("Sub="+x); } } class Easy { public static void main(String[] args) { //Creating instance(object) Subtraction obj=new Subtraction(); //calling base class method obj.add(); //calling derived class method obj.sub(); } } /* ### Output ### Add=40 Sub=20 */
In the above example you can see that there is only two classes are used in which Addition is inherited by Subtarction therefore using object of subtraction we can call function add() and sub().
Multiple Inheritance
1.When two or more than two classes are inherited by a single class simultaneously called multiple inheritance.
2.In other word we can say that in this type of inheritance Base class may be two or more than two but derived class should be one.
3.In this type of inheritance atleast three class are compulsary.
4.Java does not support multiple inheritance therefor interface are used to implement multiple inheritance.
5.interface is declared with interface keyword and it is implemented by a class while class is extended by a class.
6.we can not define function inside an interface ,only can be declared.
7.It is the responsibility of derive class to implement/define the method of interface.
//interface interface Addition { //declaring method //because we can not define function inside interface void add(); } //Derived class class Subtraction { void sub() { int x,y=30,z=10; x=y-z; System.out.println("Sub="+x); } } //Derived class extending base class //and implementing interface class Multiplication extends Subtraction implements Addition { //implementing method of interface public void add() { int x,y=30,z=10; x=y+z; System.out.println("Add="+x); } void multi() { int x,y=30,z=10; x=y*z; System.out.println("Multiply="+x); } } class Easy { public static void main(String[] args) { //Creating instance(object) Multiplication obj=new Multiplication(); obj.add(); obj.sub(); obj.multi(); } } /* ### Output ### Add=40 Sub=20 Multiply=300 */
In the above example you can see that there are two classes(Subtractio and Multiplication) and one interface (Addition) are used in which Subtraction is extended and Addition is implemented by Multiplication therefore using object of Multiplication we can call function add() , sub() and multiply().
Multilevel Inheritance
1.When first class is inherited by second class,second class is inherited by third class and so on called multlevel inheritance.
2.In this type of inheritance each derived class is the base class for the next class.
3.In this type of inheritance atleast three class are compulsary.
class Addition { public void add() { int x,y=30,z=10; x=y+z; System.out.println("Add="+x); } } //extending Addition class Subtraction extends Addition { void sub() { int x,y=30,z=10; x=y-z; System.out.println("Sub="+x); } } //extending Subtraction class Multiplication extends Subtraction { void multi() { int x,y=30,z=10; x=y*z; System.out.println("Multiply="+x); } } class Easy { public static void main(String[] args) { //Creating instance(object) Multiplication obj=new Multiplication(); obj.add(); obj.sub(); obj.multi(); } } /* ### Output ### Add=40 Sub=20 Multiply=300 */
Hierarchical Inheritance
1.When a single class is inherited by two or more than two classes simultaneously called hierarchical inheritance.
2.In other word we can say that in this type of inheritance derived class may be two or more than two but Base class should be one.
3.In this type of inheritance atleast three class are compulsary.
class Addition { public void add() { int x,y=30,z=10; x=y+z; System.out.println("Add="+x); } } //extending Addition class Subtraction extends Addition { void sub() { int x,y=30,z=10; x=y-z; System.out.println("Sub="+x); } } //extending same class Addition class Multiplication extends Addition { void multi() { int x,y=30,z=10; x=y*z; System.out.println("Multiply="+x); } } class Easy { public static void main(String[] args) { //Creating instance(object) Multiplication obj=new Multiplication(); //calling base class function obj.add(); //calling derive class function obj.multi(); } } /* ### Output ### Add=40 Multiply=300 */
In the above example you can see that there are three classes(Addition,Subtractio and Multiplication) are used in which Addition is inherited by Subtraction and Multiplication therefore using object of Multiplication we can call function only add() and multiply() because there is no relation between Subtraction and Multiplication therefore function sub() can not be called by object of Multiplication.Similarly by using object of class Subtraction we can call only function add() and sub().
Hybrid Inheritance
1.The combination of two or more than two inheritance is called Hybrid inheritance.
2.It can be combination of any two or more than two inheritance(single,multiple,multilevel,hierarchical).
3.In this type of inheritance atleast three class are compulsary.
//interface interface Addition { //declaring method //because we can not define function inside interface void add(); } //Derived class class Subtraction { void sub() { int x,y=30,z=10; x=y-z; System.out.println("Sub="+x); } } //Derived class extending base class //and implementing interface class Multiplication extends Subtraction implements Addition { //implementing method of interface public void add() { int x,y=30,z=10; x=y+z; System.out.println("Add="+x); } void multi() { int x,y=30,z=10; x=y*z; System.out.println("Multiply="+x); } } class Division extends Multiplication { void div() { int x,y=30,z=10; x=y/z; System.out.println("Div="+x); } } class Easy { public static void main(String[] args) { //Creating instance(object) Division obj=new Division(); obj.add(); obj.sub(); obj.multi(); obj.div(); } } /* ### Output ### Add=40 Sub=20 Multiply=300 Div=3 */
In the above example you can see that there are four classes(Addition,Subtractio,Multiplication and Division) in which Addition and Subtraction are inherited by Multiplication class so in class Addition,Subtractio and Multiplication there is Multiple inheritance but class Multiplication is inherited by Division so in class Multiplication and Division there is Single inheritance.Therefore the above program is a combination of Multiple and Single inheritance so it is called Hybrid Inheritance.
Advantage of Inheritance
1.Code Reusability: It means function inside base class is shared by all the derived class.
2.Time Saving:Because there is no need to define existing property(same code) of a class in another class.
3.Less Cost:Because existing code is reused, it leads to less development and maintenance costs.
4.It helps to reduce code redundancy.