Inheritance in Java
Inheritance is one of the key features of Object Oriented Programming. Inheritance provided mechanism that allowed a class to inherit property of another class. When a Class extends another class it inherits all non-private members including fields and methods. Inheritance in Java can be best understood in terms of Parent and Child relationship, also known as Super class(Parent) and Sub class(child) in Java language.
Inheritance defines is-a relationship between a Super class and its Sub class. extends
and implements
keywords are used to describe inheritance in Java.
Let us see how extend keyword is used to achieve Inheritance.
class Vehicle. { ...... } class Car extends Vehicle { ....... //extends the property of vehicle class. }
Now based on above example. In OOPs term we can say that,
- Vehicle is super class of Car.
- Car is sub class of Vehicle.
- Car IS-A Vehicle.
Purpose of Inheritance
- To promote code reuse.
- To use Polymorphism.
Simple example of Inheritance
class Parent { public void p1() { System.out.println("Parent method"); } } public class Child extends Parent { public void c1() { System.out.println("Child method"); } public static void main(String[] args) { Child cobj = new Child(); cobj.c1(); //method of Child class cobj.p1(); //method of Parent class } }
Output :
Child method Parent method
Another example of Inheritance
class Vehicle { String vehicleType; } public class Car extends Vehicle { String modelType; public void showDetail() { vehicleType = "Car"; //accessing Vehicle class member modelType = "sports"; System.out.println(modelType+" "+vehicleType); } public static void main(String[] args) { Car car =new Car(); car.showDetail(); } }
Output :
sports Car
Types of Inheritance
- Single Inheritance
- Multilevel Inheritance
- Heirarchical Inheritance
NOTE :Multiple inheritance is not supported in java
Why multiple inheritance is not supported in Java
- To remove ambiguity.
- To provide more maintainable and clear design.
super keyword
The super is java keyword. As the name suggest super is used to access the members of the super class.It is used for two purposes in java.
The first use of keyword super is to access the hidden data variables of the super class hidden by the sub class.
e.g. Suppose class A is the super class that has two instance variables as int a and float b. class B is the subclass that also contains its own data members named a and b. then we can access the super class (class A) variables a and b inside the subclass class B just by calling the following command.
super.member;
Here member can either be an instance variable or a method. This form of super most useful to handle situations where the local members of a subclass hides the members of a super class having the same name. The following example clarify all the confusions.
class A{ int a; float b; void Show(){ System.out.println("b in super class: " + b); } } class B extends A{ int a; float b; B( int p, float q){ a = p; super.b = q; } void Show(){ super.Show(); System.out.println("b in super class: " + super.b); System.out.println("a in sub class: " + a); } public static void main(String[] args){ B subobj = new B(1, 5); subobj.Show(); } }
Output:
C:\>java B
b in super class: 5.0
b in super class: 5.0
a in sub class: 1
Use of super to call super class constructor:
The second use of the keyword super in java is to call super class constructor in the subclass. This functionality can be achieved just by using the following command.
super(param-list);
Here parameter list is the list of the parameter requires by the constructor in the super class. super must be the first statement executed inside a super class constructor. If we want to call the default constructor then we pass the empty parameter list. The following program illustrates the use of the super keyword to call a super class constructor.
class A{ int a; int b; int c; A(int p, int q, int r){ a=p; b=q; c=r; } } class B extends A{ int d; B(int l, int m, int n, int o){ super(l,m,n); d=o; } void Show(){ System.out.println("a = " + a); System.out.println("b = " + b); System.out.println("c = " + c); System.out.println("d = " + d); } public static void main(String args[]){ B b = new B(4,3,8,7); b.Show(); } }
Output: C:\>java B a = 4 b = 3 c = 8 d = 7
What is not possible using java class Inheritance?
1. Private members of the superclass are not inherited by the subclass and can only be indirectly accessed.
2. Members that have default accessibility in the superclass are also not inherited by subclasses in other packages, as these members are only accessible by their simple names in subclasses within the same package as the superclass.
3. Since constructors and initializer blocks are not members of a class, they are not inherited by a subclass.
4. A subclass can extend only one superclass
Q. Can you use both this() and super() in a Constructor?
NO, because both super() and this() must be first statement inside a constructor. Hence we cannot use them together.
post a comment