Skip to main content

Inheritance in JAVA


Inheritance in JAVA

In this tutorial you will learn 1) What is inheritance in JAVA? 2) How Inheritance is useful? 3) What is super class and subclass? 4) Function Overriding 5) How we implement inheritance in JAVA? What is inheritance in JAVA Inheritance is such a mechanism in JAVA by which the methods and data members of one class are inherited form to another class. It must be kept in mind that inheritance is between the two classes which have a lot of features in common. Inheritance is ‘is a’ relationship between two classes. It helps us save code space and the structure


Submitted byAnonymous (not verified)on Sun, 03/14/2021 - 17:21

import java.util.Scanner; public class Calculator { public static void main(String[] args) { int num1,num2; char choice; System.out.print("Enter the first number "); Scanner inputScanner= new Scanner(; num1 = inputScanner.nextInt(); System.out.print("Enter the operation to be performed "); choice =;//1 for addition System.out.print("Enter the second number"); num2 = inputScanner.nextInt(); if(choice =='+'){ System.out.println(num1+num2); //add } else if(choice=='-'){ System.out.println(num1-num2); //subtract } else if(choice=='*'){ System.out.println(num1*num2); //multiply } else if(choice=='/'){ System.out.println(num1/num2); //divide } } }

Add new comment