0% found this document useful (0 votes)
44 views

Java Programming

This document discusses Java interfaces and provides an example program. It introduces interfaces as blueprints for classes that define abstract methods but no method bodies. An interface cannot be instantiated like an abstract class. The example program defines an interface with two abstract methods and a class that implements the interface, providing method bodies for the abstract methods. When run, the program outputs "implementation of method1" and "method2", demonstrating how interfaces require classes to implement abstract method signatures.

Uploaded by

shanthi prabha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views

Java Programming

This document discusses Java interfaces and provides an example program. It introduces interfaces as blueprints for classes that define abstract methods but no method bodies. An interface cannot be instantiated like an abstract class. The example program defines an interface with two abstract methods and a class that implements the interface, providing method bodies for the abstract methods. When run, the program outputs "implementation of method1" and "method2", demonstrating how interfaces require classes to implement abstract method signatures.

Uploaded by

shanthi prabha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Advanced Java Programming Lab

MS.R.SHANTHI PRABHA, M.Sc., M.Phil.,


Assistant professor
department of computer science
sacwc.
Introduction
 Interfaces
 Interfaces References
 Sample Program
 Example Program with Output
Introduction of Interface
 An interface in java is a blueprint of a class
 It has Static Constants and abstract methods.
 The interface in java is a mechanism to archieve abstraction
 There can be only absrtact methods in the java interface not
method body.
 It cannot be instantiated like abstract class.
Sample Interface Program
interface MyInterface
{
/* All the methods are public abstract by default * As
you see they have no body
*/
public void method1();
public void method2();
}
Example Program
interface MyInterface
{
/* compiler will treat them as:
public abstract void method1();
* public abstract void method2();
*/ public void method1();
public void method2();
}
Con..
class Demo implements MyInterface
{
/* This class must have to implement both the abstract methods
else you will get compilation error
*/
public void method1()
{
System.out.println("implementation of method1");
}
Con..
public void method2()
{ System.out.println("method2");
}
public static void main(String args[])
{
MyInterface obj = new Demo();
obj.method2();
}
}
Output
implementation of method1

You might also like