Inheritance in java
To acquire the properties of one class inside the another class we go with inheritance.
Inheritance provide the concept of child and super class.
That mean sub class can inherit the all properties of super class. For inheriting sub class use extend keyword. When we create the object for sub class then first super class members will get loaded then sub class,But super class can’t inherit the feature of sub class.
Super class is known as parent class, inherit class and base class while sub class is known as child class and inheriting class.
Types of inheritance:
Inheritance is of 5 types as given below:
Single Level inheritance: When one sub class inheriting the feature of one super class at immediate level is known as single inheritance.
For example : class A is the sub class of Class B that mean class A is inheriting the feature of class B.
Multilevel Inheritance : When one sub class inherits the feature of one super class at multilevels that is known as multi level inheritance.
For example : Class A is the sub class of Class B and Class B is the sub class of Class C that mean Class A will be able to inherit the feature of Class B & Class C. Class B will be able to inherit the feature of Class C.
Hierarchical Level inheritance: When one sub class inherit the more than one super class at immediate level that is known as hierarchical inheritance.
For example : Class A and class B is the sub class of Class C at immediate level . That mean class A and class B both are able to access the properties of Class A.
Multiple Inheritance: When one sub class is able to inheriting the properties of more than one super class that concept is known as multiple inheritance.
For example: Class A is inheriting the Class B and Class C but using class its not possible. Because if both class B and C having same methods then when create object for Class A which class of method will be called first Class B or Class C,because of ambiguity we can’t get the multiple inheritance using class.
For getting multiple inheritance Interface used because interface having incomplete(abstract method), which will be implemented by the class.
Hybrid Inheritance: Combination of more than one type of inheritance is known as Hybrid inheritance .
example: Combination of Single inheritance, Hierarchical inheritance, Multilevel inheritance.
Note: In C++ Cyclic inheritance is also used . Cyclic inheritance means when sub class inherit the feature of super class and super class inherit the feature of sub class.
Cyclic inheritance is not possible in java. In java only sub class can inherit the feature of super class.
Use of Inheritance:
If across the application common functionality are there, then we make one super class and push all the common functionalities there, which can be access by the multiple sub classes.
This provides advantages as-
(i) Code reusability: That mean same code we can use in the multiple sub classes.
(ii) Code Maintenance: If we want to change in the common code which are reflecting in the other code we will not need to change in all places . We will change only in the super class code because all other classes are using the same code of super class.
(iii)Overriding:For getting the overriding inheritance is necessary. Without getting inheritance we can’t achieve overriding.
(iv)Up Casting: Conversion the sub class type to super class type is known as up casting. To achieve up casting inheritance is used.
This is basic concept about inheritance. To learn about the all other topics and getting deep knowledge on java learn Java in Infocampus Software Training Institute.
Infocampus is a java training center bangalore. It provides core and advance java training with the placement support. Attend 4 days free demo classes on Advance java training . Training is given by the experienced trainers. Syllabus is divided into three modules on java course for the convenience of learners.
Mock test and mock interviews are conducted every weekend for the preparation of students.
Java/j2ee classes are available on flexible timing on weekdays and weekend.
To attend the free demo class Contact at: 9738001024 or for more details on best core java training in bangalore visit : http://www.infocampus.co.in/java-training-bangalore.html