No announcement
 

Inheritance

Inheritance is the process wherein one class acquires the properties (methods and fields) of another class. Inheritance is a way of managing the information is a hierarchical order.

The class that inherits the properties of the other class is known as the subclass (or derived class/child class) and the class whose properties are inherited is known as the superclass (or parent class/base class).

extends Keyword

extends keyword used to inherit the properties of a class.

Syntax:

class SuperClass
{
    // Declarations
}

class SubClass extends SuperClass
{
    // Declarations
}

Example:

InheritanceDemo.java

package com.codingnous.tutorial;

public class InheritanceDemo extends Operations {

	// Performs multiplication of two numbers.
	public int multiplication(int num1, int num2) {
		return num1 * num2;
	}

	public static void main(String[] args) {

		int a = 10, b = 5, result = 0;

		InheritanceDemo d = new InheritanceDemo();
		result = d.addition(a, b);
		System.out.println("Addition result: " + result);
		result = d.subtraction(a, b);
		System.out.println("Subtraction result: " + result);
		result = d.multiplication(a, b);
		System.out.println("Multiplication result: " + result);
	}

}

class Operations {

	// Performs addition of two numbers.
	public int addition(int num1, int num2) {
		return num1 + num2;
	}

	// Performs subtraction of two numbers.
	public int subtraction(int num1, int num2) {
		return num1 - num2;
	}
}

Output
Addition result: 15 Subtraction result: 5 Multiplication result: 50

In the above program, when an object of InheritanceDemo class is created; a copy of the contents of the superclass is made within it. This is why, using the object of the subclass we can access the members of a superclass.

Super keyword

The super keyword is similar to this keyword. Following are the scenarios when the super keyword is used.

  1. It is used to differentiate members of the superclass from the members of the subclass, if they have same names.
  2. It is used to invoke the superclass constructor from the subclass.

Example:

SuperKeywordDemo.java

package com.codingnous.tutorial;

public class SuperKeywordDemo extends OperationsDemo {

	// Performs addition of two numbers with additional 30.
	public int addition(int num1, int num2) {
		return num1 + num2 + 30;
	}

	public void executeOperations() {
		int a = 10, b = 5, result = 0;
		
		// Sub class addition method invoke.
		result = this.addition(a, b);
		System.out.println("Addition result with additional 30: " + result);
		
		// Super class addition method invoke.
		result = super.addition(a, b);
		System.out.println("Addition result: " + result);
	}

	public static void main(String[] args) {

		SuperKeywordDemo d = new SuperKeywordDemo();

		d.executeOperations();

	}

}

class OperationsDemo {

	// Constructor
	public OperationsDemo() {
		System.out.println("Super class constructor.");
	}

	// Performs addition of two numbers.
	public int addition(int num1, int num2) {
		return num1 + num2;
	}

}

Output
Super class constructor. Addition result with additional 30: 45 Addition result: 15