PROGRAM OF SINGLE INHERITANCE:
class Add
{
int a = 20;
int b = 38;
}
class Sum extends Add
{
public void CalculatedSum ()
{
System.out.println("Sum of a and b is :" + (a+b));
}
class Main
{
public static void main(Strings args[])
{
Sum obj = new Sum ();
obj.CalculatedSum();
}
}
OUTPUT:
Sum of a and b is : 58
PROGRAM OF MULTILEVEL INHERITANCE:
class Rectangle
{
int a = 30;
int b = 50;
}
class Example1 extends Rectangle
{
int c = 20;
public void Product()
{
System.out.println("Product of a and b is :" +(a*b));
}
}
class Example2 extends Example1
{
public void Sum()
{
System.out.println("Sum of a, b and c is :"+(a+b+b));
}
}
class Main
{
public static void main(String args[])
{
Example2 obj = new Example();
obj.Sum();
obj.Product();
}
}
OUTPUT:
Sum of a and b is : 80
Sum of a, b and c is:100
PROGRAM OF HIERARCHICAL INHERITANCE:
class Square
{
int a = 60;
int b = 40;
}
class Sum extends Sqaure
{
public void CalculatedSum()
{
System.out.println("Sum is :" +(a+b));
}
}
class Difference extends Square()
{
public void CalculatedDifference()
{
System.out.println("Difference is :" +(a-b));
}
}
class Main()
{
public static void main(String args[])
{
Difference obj = new Difference();
obj.CalculatedDifference();
Sum obj1 = new Sum();
obj1.CalculatedSum();
}
}
OUTPUT:
Sum is : 100
Difference is: 20
READ MORE PAGES: