write a program of single, multilevel and hierarchical inheritance using java.

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:

1.explain polymorphism and compare between compile time polymorphism and run time polymorphism with program

 


Md Ashraf

'KNOWLEDGE WITH ASHRAF' is the platform where you find all the type of knowledge especially on programming based. Our goal is to give you a deeper grasp of technology in specifics that will help you increase your knowledge.

Previous Post Next Post

Contact Form