Java Program to Show the Implementation of Interface

»
This is a Java program to show the Implementation of Interface. We have to enter the length and breadth of the rectangle as an input to get Area of rectangle as output.An interface in Java is similar to a class, but the body of an interface can include only abstract methods and final fields (constants). A class implements an interface by providing code for each method declared by the interface.

Here is the source code of the Java Program to show the Partial Implementation of Interface. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

  1. import java.util.Scanner;
  2.  
  3. interface area
  4. {
  5.     public void dimensions();
  6.     public void area();
  7. }
  8. public class Interface_Implementation implements area
  9. {
  10.     int length,breadth,area;
  11.     public void dimensions() 
  12.     {
  13.         Scanner s=new Scanner(System.in);
  14.         System.out.print("Enter length:");
  15.         length=s.nextInt();
  16.         System.out.print("Enter breadth:");
  17.         breadth=s.nextInt();
  18.     }
  19.     public void area() 
  20.     {
  21.         area=length*breadth;
  22.         System.out.print("Area:"+area);
  23.     }
  24.     public static void main(String[] args) 
  25.     {
  26.         Interface_Implementation obj=new Interface_Implementation();
  27.         obj.dimensions();
  28.         obj.area();
  29.     }
  30. }

Output:

advertisement
$ javac Interface_Implementation.java
$ java Interface_Implementation
 
Enter length:6
Enter breadth:7
Area:42

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