Program in Java to show concept of multilevel inheritance

by anupmaurya
0 comment

In this example, you’ll how in Java to show concept of multilevel inheritance.

import static java.lang.Math.*; class A { private int num1,num2,sum; public void set(int x,int y) { num1=x; num2=y; sum=num1+num2; } public int get1() { return(sum); } } class B extends A { public void display() { System.out.println("sum of two numbers is"+get1()); } } class C extends B { private double sqr; public void sqrs() { System.out.println("square root of sum is"+sqrt(get1())); } } class ABCMain { public static void main(String args[]) { C obj1=new C(); obj1.set(100,200); System.out.println("first number is 100"); System.out.println("second number is 200"); obj1.display(); obj1.sqrs(); } }
Code language: Java (java)


This program does a pretty good job. Here is a sample execution

first number is 100
second number is 200
sum of two numbers is 300
square root of sum is 17.3205080

Here are some more examples

You may also like