Leo Umesh avatar

INEHRITANCE IN JAVA PROGRAMMING!

leoumesh

Published: 02 Jul 2020 › Updated: 02 Jul 2020INEHRITANCE IN JAVA PROGRAMMING!

INEHRITANCE IN JAVA PROGRAMMING!

INEHRITANCE IN JAVA PROGRAMMING!

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

Leave INEHRITANCE IN JAVA PROGRAMMING! to:

Written by

I am just a passionate blogger.

Read more #hive-122108 posts


Best Posts From Leo Umesh

We have not curated any of leoumesh's posts yet. But you can encourage our curation team to review posts by visiting them regularly and by referring other readers. Because we give priority to frequently read content.

More Posts From Leo Umesh