Pageviews

Thursday 13 June 2013

Multilevel Inheritance in Java

by Unknown  |  in Object Oriented Programming at  Thursday, June 13, 2013

Multilevel Inheritance: It is another type of inheritance supported by Java.In Multilevel inheritance, base class is inherited by the derived class,this derived class is further inherited by the other derived class.So,the inheritance goes in levels,that is why it is known as Multilevel inheritance.The diagram shows the criteria;


In the diagram,Class A is base class and Class B and C are derived classes of Class,but Class B acts as a Base class to the Class C,so further if there is any other class for that Class C will act as base class and the process goes on.

Now,we are going to show you an example of how this Inheritance type works.We had created a Simple Calculator which performs basic Arithmetic Operations using Multilevel Inheritance.

WAP to implement the use of Multilevel Inheritance

import java.util.Scanner;
class sport{                                                                           //Base Class declared
int a,b;
void add()
{

Scanner p=new Scanner(System.in);
System.out.println("Enter first number");
a=p.nextInt();
System.out.println("Enter second number");
b=p.nextInt();
int s;
s=a+b;
System.out.println("Sum is"+s);
}
}
class port extends sport{                                                     // Derived class 1 declared
void mul(){
int k;
k=a*b;
System.out.println("Multiplication of 2 nos is:"+k);
}
}
class bot extends port{                                                       // Derived class 2 declared
void sub()
{
int d;
d=a-b;
System.out.println("Subtraction is"+d);
}
}

class pot extends bot{                                                        // Derived class 3 declared
void div()
{
int e;
e=a/b;
System.out.println("Division is"+e);
}
}
class Calculator{
public static void main(String args[])
{
pot p=new pot();
p.add();
p.sub();
p.div();
p.mul();
}
}


Here is the output of the program:

Inheritance









0 comments:

Proudly Powered by Blogger.