Basic example of Multilevel Inheritance in Java


Here is the simple program for printing basic example of Multilevel Inheritance in Java.

Source Code

import java.io.*;
import java.util.Scanner;
class class_A
{
  private int a,b,c;
  public class_A(int x,int y)
  {
    a=x;
    b=y;
  }
  public void add()
  {
    c=a+b;
    System.out.print("\nTotal : "+c);
  }
}
class class_B extends class_A
{

  private int a,b,c;
  public class_B(int x,int y,int z1,int z2)
  {
    super(z1,z2);
    a=x;
    b=y;
  }
  public void sub()
  {
    c=a-b;
    System.out.print("\nSub : "+c);
  }
}
class class_C extends class_B
{
  private int a,b,c;
  public class_C(int x,int y,int z1,int z2,int x1,int x2)
  {
    super(x1,x2,z1,z2);
    a=x;
    b=y;
  }
  public void mul()
  {
    c=a*b;
    System.out.print("\nMul : "+c);
  }
}

class class_main1
{
  public static void main(String args[])
  {
    class_C o =new class_C(100,10,100,50,10,2);
    o.mul();//100
    o.sub();//8
    o.add();//150
  }
}
To download raw file Click Here

Output

Mul : 1000
Sub : 8
Total : 150


Learn All in Tamil © Designed & Developed By Tutor Joes