Inheritance is a process of writing a new class by using existing class functionality. ‘The process by which one class acquires the properties(instance variables) and functionalities of another class is called inheritance’. This happens when one class adopts the non-private properties and methods of one class. These classes will then be referred to as superclass and subclass.
For ex.: Let's say that a student has attributes like age, location, and this class has existed for a long time. After some time, we need to add more attribute like pan card to student.
What options do we have?
Option 1 : We can modify class student
Option 2 : We need to extend student class and add an attribute in that new subclass
The first option is a violation of principles of java: 'Java classes are not open for modifications', so we should not modify an existing one. This will increase unit testing which will have to be done again for the existing class.
The second option is, without disturbing existing class, we add variable in another class by making subclass. So, the unit testing needs to be done for the child and not parent.
package com.javabykiran.Inheritance;
/*
* @author Kiran
*/
public class A {
int age;
int loc;
}
package com.javabykiran.Inheritance;
/*
* @author Kiran
*/
public class B extends A {
int pancard;
}
package com.javabykiran.Inheritance;
/*
* @author Kiran
*/
public class C {
private int a = 20;
public int b = 30;
public int c = 40;
}
package com.javabykiran.Inheritance;
/*
* @author Kiran
*/
public class D extends C {
public int c = 10;
}
package com.javabykiran.Inheritance;
/*
* @author Kiran
*/
public class CDTest {
public static void main(String[] args){
D d=new D();
//System.out.println(d.a);
//Error:as it not accessible;it is private,'a'is not coming to D
// b is of C class
System.out.println(d.b);
// c is of D class and the answer will be 10, not 40
System.out.println(d.c);
}
}
Output:
30
10
package com.javabykiran.Inheritance.scenarios;
/**
* @author Java By Kiran
*/
public class A {
int a = 10;
int b = 20;
void m1() {
System.out.println("I am in A-m1”);
}
void m2() {
System.out.println("I am in A-m2”);
}
}
package com.javabykiran.Inheritance.scenarios;
/**
* @author Java By Kiran
*/
public class B extends A{
int b = 30;
int c = 40;
void m2() {
System.out.println("I am in B-m2");
}
void m3() {
System.out.println("I am in B-m3");
}
}
// Scenario 1
package com.javabykiran.Inheritance.scenarios;
/**
* @author Java By Kiran
*/
public class Scenarios {
public static void main(String[] args){
// Scenario 1
A a =new A(); // Super class object a is eligible to
//call only A
System.out.println(a.a);
System.out.println(a.b);
//System.out.println(a.c);//Error; c does not exist in A
a.m1();
a.m2();
//a.m3(); //Error m3 does not exist in class A
}
}
Output:
10
20
I am in A-m1
I am in A-m2
// Scenario 2
Now change the main method with different scenarios:
B b = new B(); System.out.println(b.a); // a of A
System.out.println(b.b); // b of B
System.out.println(b.c); // c of B
b.m1();// m1 of A
b.m2();// m2 of B
b.m3(); // m3 of B
Output:
10
30
40
I am in A-m1
I am in B-m2
I am in B-m3
This example assigns sub class to super class.
This is allowed in java. i.e. A a = new B()
in Scenario 3
// Scenario 3
1) A a =new B();
2) System.out.println(a.a);// a of A
3) System.out.println(a.b);// b of A
4) //System.out.println(a.c); //Error c of A does not exist in A
5) a.m1();// m1 of A
6) a.m2();// m2 of B
7) //a.m3(); //Error m3 of A does not exist in A
Output:
10
20
I am in A-m1
I am in B-m2
Remember these Rules: In this case a is eligible to call
At compile time, everything of A class will be called other than that everything shows error at compile time. This means that in this case line no.(4) and line no.(7) will have problems.
At run time, all variables and methods of class A will get called, but all those methods which are overridden by class B will get executed. Or exactly similar methods of A into B will be called (Observe Output).
// Scenario 4
Try changing main method like below:
A a=new A();
B b=new B();
a=b; //This is equivalent to A a=new B(); refer 3rd Scenario
1) System.out.println(a.a); // a of A
2) System.out.println(a.b); // b of A
3) // System.out.println(a.c); //Error c of A not exist
4) a.m1(); // m1 of A
5) a.m2(); // m2 of B
6) // a.m3(); // Error m3 of A does not exist
// Scenario 5
B b=new A(); // syntax error
//Try calling everything by using b
// Scenario 6
A a=new B();
B b=new B();
b=(B)a; // looks like super class assigned to subclass but
//it translates internally to
B b=new B(); //It’s equivalent to 2nd scenario
Following are the various types of inheritances:
public class A {
public void methodA(){
System.out.println("Base class method");
}
}
public class B extends A {
public void methodB() {
System.out.println("Child class method");
}
public static void main(String args[]){
B obj = new B();
obj.methodA(); //calling super class method
obj.methodB(); //calling local method of B
}
}
Output:
Base class method
Child class method
public class X {
public void methodX() {
System.out.println("Class X method");
}
}
public class Y extends X {
public void methodY() {
System.out.println("Class Y method");
}
}
public class Z extends Y {
public void methodZ() {
System.out.println("Class Z method");
}
public static void main(String args[]) {
Z obj = new Z();
obj.methodX(); //calling X grand parent class method
obj.methodY(); //calling Y parent class method
obj.methodZ(); //calling Z class local method
}
}
Output:
Class X method
Class Y method
Class Z method
Note 1 : Multiple Inheritances are very rarely used in software projects. Using multiple inheritances often leads to problems in the hierarchy. This results in unwanted complexity when further extending the class
Note 2 : Most of the new object oriented languages like Small Talk, Java, and C# do not support multiple inheritances. Multiple Inheritances is supported in C++
From the diagram above, if class A has m1 and class B also has m1 and they will have different implementations
As per inheritance concepts both methods should inherited to class C
If somebody creates object of class C, which m1 will get called? This is ambiguity of methods and therefore not allowed in java
Class C extends A, B like this syntax is not allowed in java
In such kind of inheritance one class is inherited by many sub classes. In the example below, class B, C and D inherits the same class. Class A is parent class (or base class) of B, C and D (or derived classes).
public class A {
public void methodA(){
System.out.println("method of Class A");
}
}
public class B extends A {
public void methodB() {
System.out.println("method of Class B");
}
}
public class C extends A {
public void methodC() {
System.out.println("method of Class C");
}
}
public class D extends A {
public void methodD() {
System.out.println("method of Class D");
}
}
public class MyClass {
public void methodB() {
System.out.println("method of Class B");
}
public static void main(String args[]) {
B obj1 = new B();
C obj2 = new C();
D obj3 = new D();
obj1.methodA();
obj2.methodA();
obj3.methodA();
}
}
Output:
method of Class A
method of Class A
method of Class A
class A extends A{
}
/*This is not ok because it is like cyclic inheritance,
A inherits itself.*/
class A {
}
class B {
}
class C extends A,B {
}
/* Not ok, because multiple inheritance is not allowed.
Here C has two parent classes. */