Java Program to Illustrates Use of Abstract Class and Method

This is a Java Program to Illustrates Use of Abstract Class and Method.

Here we made add() and multiply() as abstract methods in parent class which are defined in child class and we can call all these mehods via making object of child class.

Here is the source code of the Java Program to Illustrates Use of Abstract Class and Method. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

  1. abstract class Calculation
  2. {
  3.     float a = 12, b = 6, c;
  4.     abstract void add();
  5.     void subtract()
  6.     {
  7.         c = a - b;
  8.         System.out.println("Result:"+c);
  9.     }
  10.     abstract void multiply();
  11.     void divide()
  12.     {
  13.         c = a / b;
  14.         System.out.println("Result:"+c);
  15.     }
  16. }
  17. public class AbstractionDemo extends Calculation
  18. {
  19.     void add()
  20.     {
  21.         c = a + b;
  22.         System.out.println("Result:"+c);
  23.     }
  24.     void multiply()
  25.     {
  26.         c = a * b;
  27.         System.out.println("Result:"+c);
  28.     }
  29.     public static void main(String[] args)
  30.     {
  31.         AbstractionDemo obj = new AbstractionDemo;
  32.         obj.add();
  33.         obj.subtract();
  34.         obj.multiply();
  35.         obj.divide();
  36.     }
  37. }

Output:

$ javac AbstractionDemo.java
$ java AbstractionDemo
 
Result:18.0
Result:6.0
Result:72.0
Result:2.0

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

advertisement
advertisement

Here’s the list of Best Books in Java Programming, Data Structures and Algorithms.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.