INEHRITANCE IN JAVA PROGRAMMING!steemCreated with Sketch.

in #java4 years ago

Inheritance is one of the most important characteristic of object oriented programming language. It is the mechanism by which one class access the property like variable or field and method of another class. The class which inherits the main class is called Derived Class and which gets inherited is called Derived Class. The following is the simple example of inheritance in which class Division and Multiplication access the field of Calculation Class and then calculates the multiplication and division.

package Inheritance;

public class MainClass {

    public static void main(String[] args) {
        Multiplication mul= new Multiplication();
        Division div= new Division();
        
        mul.value(10, 5);
        div.value(10, 5);
        
        System.out.println(mul.multiply());
        System.out.println(div.divide());
    }

}


package Inheritance;

public class Calculation {

    protected int a;
    protected int b;
    
    public void value(int m, int n){
        a=m;
        b=n;
    }
}


package Inheritance;

public class Multiplication extends Calculation {

    public double multiply() {
        return (a*b);
    }

}


package Inheritance;

public class Division extends Calculation {
    
    public double divide() {
        return (a/b);
    }

}


Here's the output of the code:


Screenshot_4.png

Coin Marketplace

STEEM 0.30
TRX 0.12
JST 0.034
BTC 63688.35
ETH 3125.30
USDT 1.00
SBD 3.97