I Help You
Programming

Inheritance

In object-oriented programming (OOP), inheritance is a mechanism that allows a class to inherit the properties and methods from another class.

Inheritance sample codeJava
package in.ihelpyou;

class Calc {

    public int a, b;

    public int addition() {
        return a + b;
    }

    public int subtraction() {
        return a - b;
    }
}

// SciCalc inherits a, b, addition() and subtraction() from Calc and adds its own method.
class SciCalc extends Calc {

    public int squareOfSum() {
        int sum = addition();   // inherited method
        return sum * sum;
    }

    public static void main(String[] args) {

        SciCalc sciobj = new SciCalc();

        sciobj.a = 5;           // inherited fields
        sciobj.b = 2;

        System.out.println("Sum: " + sciobj.addition());              // 7
        System.out.println("Square of sum: " + sciobj.squareOfSum()); // 49
    }
}