Inheritance In Java
Inheritance is a mechanism to acquire properties like methods and attributes of one class to other class. Inheritance are provide an ability to reuse all the features of existing class and make a new class which is capable to implement new functionality.
Terminology of inheritance
When we are correlated the relation of two class there are following terminology will used.
Base/Supper class: Base class is a class which are provide the functionality to other class. or other word base class is a class which are inherited by other class and which are used their properties. This is also known as parent class.
derived Class/Sub Class: This class are use the properties of other class. And implements new features and functionality. This is also known as child class.
So let start inheritance concept are implement in Java Programming. First view an example to implement base and child class relation.
//Base class B
class B{
//public function of B class
public void description(){
System.out.println("This is B class Function description");
}
}
//Derived class B, Inherit Base class B
public class D extends B {
public void about(){
System.out.println("This is D class Function about");
}
public static void main(String args[]){
//Make object of derived class
D obj = new D();
obj.about();//execute on function
obj.description();//execute base class function
}
}
Output
This is D class Function about
This is B class Function description
In this example class D is derived class and class B is base class because class B is inherited by class D. Observe that extends keyword are used to inherit base class properties to derived class.
Note that in this example we are create separate object of derived and base class and access their properties. But inheritance is provide a mechanism to use base class properties by derived class object. And derived class is accessing only public properties of base class. They can't use private methods.
What do you mean by inheritance if you'll are need one example then you can see this.
class Records{
//instance variable of record class
int x, y;
//Constructor of class
Records(){
//This is automatic executes when
//derived class are not execute
//base class constructor
x=10;
y=20;
}
}
class Student extends Records{
int z;
public Student(int z){
this.z=z;
}
//Display instance variable values
public void display(){
System.out.println("x : "+x);
System.out.println("y : "+y);
System.out.println("z : "+z);
}
public static void main(String[] args) {
//Make a object of class
Student smith = new Student(30);
smith.display();
}
}

x : 10
y : 20
z : 30
In this example
Overriding Superclass Methods
When derived class properties is similar to base class properties that means derived class are providing new definition of superclass properties. This process is called Overriding propties of superclass.
This is very effective when base class properties are not fulfill requirement of derived class and base class methods are not entirely appropriate for the subclass objects. In this situation we are make new method which are exist in base class and implement new logic to them. In other word we are creating a new variant to base class method.
//Base class Base
class Base{
public void sum(int a,int b){
System.out.println(" Sum of ("+a+" + "+b+") : "+(a+b));
}
}
//Derived class , Inherit Base class
public class Derived extends Base {
//Defining new variant of sum method
public void sum(int a,int b,int c){
System.out.println(" Sum of ("+a+" + "+b+" + "+c+") : "+(a+b+c));
}
public static void main(String args[]){
//Make object of derived class
Derived obj = new Derived();
obj.sum(1,2); //execute base class method
obj.sum(4,2,6); //execute derived class method
}
}
Output
Sum of (1 + 2) : 3
Sum of (4 + 2 + 6) : 12
Note that there are possible to define and implement exact definition of two different inherit class. and we can also implement new parameter list (data and parameter sequence) in different way to base class. The advantage to this process we can access both class functionality.
In above example derived class object is capable to used two different method by same name. That function calling process is based on passed parameter list values. when both function of base and derived class are exactly same in this case then compile program are execute always derived class method.
Method with not overloaded by child class
There are three method which are not overload by child class.
Static method : When we are declare a static method in base class that cannot be override by derived (inherit) class. See this situation.
//Base class Base
class Base{
static public void newSalary(int salary){
salary=salary+2000;
System.out.println("Salary + Overtime : "+(salary));
}
}
//Derived class , Inherit Base class
class Derived extends Base {
//Override variant of base class
public void newSalary(int salary){
salary=salary+10000; //10000 extra bonus
System.out.println("Salary + Bonus : "+salary);
}
public static void main(String args[]){
//Make object of derived class
Derived obj = new Derived();
obj.newSalary(50000); //execute derived class method
}
}
Output
Derived.java:13: error: newSalary(int) in Derived cannot override newSalary(int) in Base
public void newSalary(int salary){
^
overridden method is static
1 error
In this example there are is newSalary() method which are override by derived class. But addSlary is an static method of base class so we cannot override this method in derived class. We can solve this problem in a two ways. first is remove static keyword in base class function declaration then override the method in derived class.
//Base class Base
class Base{
public void newSalary(int salary){
salary=salary+2000;
System.out.println("Salary + Overtime : "+(salary));
}
}
//Derived class , Inherit Base class
class Derived extends Base {
//Override variant of base class
public void newSalary(int salary){
salary=salary+10000; //10000 extra bonus
System.out.println("Salary + Bonus : "+salary);
}
public static void main(String args[]){
//Make object of derived class
Derived obj = new Derived();
obj.newSalary(50000); //execute derived class method
}
}
Output
Salary + Bonus : 60000
In Second method, we can be override static method of base class in form of static method.
//Override base class static method by derived class static method
//Base class Base
class Base{
static public void newSalary(int salary){
salary=salary+2000;
System.out.println("Salary + Overtime : "+(salary));
}
}
//Derived class , Inherit Base class
class Derived extends Base {
//Override variant of base class
static public void newSalary(int salary){
salary=salary+10000; //10000 extra bonus
System.out.println("Salary + Bonus : "+salary);
}
public static void main(String args[]){
//Make object of derived class
Derived obj = new Derived();
newSalary(50000); //execute derived class method
}
}
Output
Salary + Bonus : 60000
In case override static method of base class. When we are need to use variant of non private static method of base class then we are use Class name of base class to call static method. See this example.
//Access static member of base class
//Base class Base
class Base{
//static variable of base class
static int salary;
public Base(){
salary=0;
}
static public void newSalary(int amount){
salary=amount+2000;
System.out.println("Salary + Overtime : "+(salary));
}
}
//Derived class , Inherit Base class
class Derived extends Base {
//Override variant of base class
static public void newSalary(int amount){
//call base class method
Base.newSalary(amount); //note Base is class name
salary+=10000; //10000 extra bonus
System.out.println("Salary + Bonus : "+salary);
}
public static void main(String args[]){
//Make object of derived class
Derived obj = new Derived();
newSalary(50000); //execute derived class method
}
}
Output
Salary + Overtime : 52000
Salary + Bonus : 62000
When we are used non static method of base class. Which is redefine of inside a derived class then we can use to execute base class method by super keyword. Static method are no need to execute any object of a class. So in this situation base class name is identified of static method are execute.
The conclusion is, static method cannot directly overridden by derived class. There can be override by static method of derived class. In this case base class static method are hide its features. And when we are need to use this hidden method then we can call this method by Base class name.
Final method : This is another case of which are not override final method of inherited class. See this example.
//Base class Base
class Base{
//static variable of base class
static int salary;
public Base(){
salary=0;
}
//final method
public final void newSalary(int amount){
salary=amount+2000;
System.out.println("Salary + Overtime : "+(salary));
}
}
//Derived class , Inherit Base class
class Derived extends Base {
//Override variant of base class
public void newSalary(int amount){
//call base class method
super.newSalary(amount); //using super keyword
salary+=10000; //10000 extra bonus
System.out.println("Salary + Bonus : "+salary);
}
public static void main(String args[]){
//Make object of derived class
Derived obj = new Derived();
obj.newSalary(50000); //execute derived class method
}
}
Output
Derived.java:19: error: newSalary(int) in Derived cannot override newSalary(int) in Base
public void newSalary(int amount){
^
overridden method is final
1 error
In Java Programming final keyword are used to define constant. So we cannot override final method which are define by base class.
Method within final class: When we are create a class by define final keyword then inside all method are acts as constant method and we cannot inherit this class by base class to derived class.
Constructor Execution in inheritance
When we are not define any constructor of base and derived class in this situation when we are create a instance of derived class, Java automatically supplied default constructor. Or when are provide non parameterized constructor then they are also automatically execute this constructor. See this example.
//Base class Base
class Base{
public Base(){
System.out.println("Base");
}
}
//Derived class , Inherit Base class
public class Derived extends Base {
public Derived(){
System.out.println("Derived");
}
public static void main(String args[]){
//Make object of derived class
Derived obj = new Derived();
}
}
Output
Base
Derived
Note that first executes base class constructor, after that derived class. But in special case non parameterized constructor are exist in base class and derived class are implemented parameterized constructor and instance of derived class are execute on constructor. In this situation are required to execute manually superclass constructor. See the situation
//Base class Parent
class Parent{
int parentAge;
//parameterized constructor Parent class
public Parent(int age){
//set age
this.parentAge=age;
System.out.println("Parent class constructor");
}
}
//derived class Child , Inherit Base parent
public class Child extends Parent {
int childAge;
//parameterized constructor of Child class
public Child(int parentAge,int childAge){
this.childAge=childAge;
System.out.println("child class constructor");
}
public static void main(String args[]){
//Make object of Child class
Child obj = new Child(30,4);
}
}
Error
Child.java:18: error: constructor Parent in class Parent cannot be applied to given types;
public Child(int parentAge,int childAge){
^
required: int
found: no arguments
reason: actual and formal argument lists differ in length
1 error
Because parent class constructor are not execute manually by Java and there defination are provide by programmer so programmer responsibility to executes superclass constructor inside a derived class constructor. super keyword are used to execute parent class constructors.
//Base class Parent
class Parent{
int parentAge;
//parameterized constructor Parent class
public Parent(int age){
//set age
this.parentAge=age;
System.out.println("Parent class constructor");
}
}
//derived class Child , Inherit Base parent
public class Child extends Parent {
int childAge;
//parameterized constructor of Child class
public Child(int parentAge,int childAge){
super(parentAge); //execute supper class constructor
this.childAge=childAge;
System.out.println("child class constructor");
}
public static void main(String args[]){
//Make object of Child class
Child obj = new Child(30,4);
}
}
Output
Parent class constructor
child class constructor
Super keyword after that within the parentheses are passing the parameter list of superclass constructor. When we are execute superclass constructor inside a derived class constructor, That are first statement should be use super. That is process first execute derived class constructor and linking to superclass constructor.
When more than two parameterized constructor are exist in superclass, then parameter list is provides which constructor are executing.
Accessing Superclass Methods
When we are inherited one class properties to other class, Then normally derived class object are use the methods of base class. When special case, We are define a method which are already exist in base class. There are exact same signature as base class. So derived class object in this case execute its class method. See this example.
//Base class Parent
class Parent{
public void details(){
System.out.println("Parent Class details Function");
}
}
//derived class Child , Inherit Base parent
public class Child extends Parent {
public void details(){
System.out.println("Child Class details Function");
}
public static void main(String args[]){
//Make object of Child class
Child obj = new Child();
obj.details();//execute derived class method
obj.details();//execute derived class method
}
}
Output
Child Class details Function
Child Class details Function
Note that in this case both class are exist same details() method. And so object of derived class are work its define details function. So how to use base class details() method, without creating base class object?. In this situation we are used super.details to execute base class function.
//Base class Parent
class Parent{
public void details(){
System.out.println("Parent Class details Function");
}
}
//derived class Child , Inherit Base parent
public class Child extends Parent {
public void details(){
System.out.println("Child Class details Function");
super.details(); //execute base class method
}
public static void main(String args[]){
//Make object of Child class
Child obj = new Child();
obj.details();//execute derived class method
}
}
Output
Child Class details Function
Parent Class details Function
In this example derived class object are executes its class method, and inside derived class details() we are execute superclass class details() method by using super keyword.
Advantage of Inheritance
There are many advantage of inheritance. In this section are given overview, why that is very effective.
Reusability: Reusability is a way to use existing functionality to new development. That is very effective approach to generalize new development with existing features.
Reduce errors: Inheritance is an reusability of codes. That means inherited properties are already test and work. So less possibility to fail or produce error in existing code.
Time consuming process: Inheritance are reduced the time and effort by use existing codes. And programmer are familiar to existing codes. This process are provide fast development.
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment