Java program to find greatest between three numbers using multilevel inheritance

Greatest between three numbers

It is very simple to find greatest between three numbers.This program shows the concept how to find greatest between three numbers using concept multilevel inheritance.

  • In this program we have made three classes i.e. “base”,”derived1″ and “scan”.
  • In first class we have taken the input of “x” and “y” using function “bget()”
  • in Second Class “derived” we have taken the input of “z” using function “dget()”
  • Finally class “scan” gives the result which is greatest among three numbers by the help of function “display()”
  • We have made a object “d1” of class “scan” called all three function using same object

Picture repersentation of program

Greatest between three numbers

import java.util.*;
class base
{
	int x,y;	
 Scanner kb=new Scanner(System.in);
public void bget()
{
	
   System.out.println("Enter value of x:- ");
   x=kb.nextInt();
   System.out.println("Enter value of y:- ");
   y=kb.nextInt();
}
}
class derived1 extends base
{
   int z;
	public void dget()
	{
		
		System.out.println("Enter value of z:- ");
		z=kb.nextInt();
	}
 
}
class scan extends derived1
{
 public void display()
 {
	   if(x>y)
    {
	 if(x>z)
	  {
	   System.out.println(" x is Greater");
	  }
	  else
	  {
	  System.out.println(" z is Greater");
	  }
	  
	}
	else
	{
	if(y>z)
	 {
	 System.out.println(" y is Greater");
	 }
	 else
	 {
	 System.out.println(" z is Greater");
	 }
	}
 }
 public static void main(String arg[])
{
 scan d1=new scan();
 d1.bget();
 d1.dget();
 d1.display();
}
}

OUTPUT

greatest between three numbers

Leave a Reply

Your email address will not be published. Required fields are marked *