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.

advertisement
  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

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

advertisement
advertisement
advertisement
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn