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

Java p10

This document discusses implementing interfaces in Java through 3 examples: 1. Creating a class that implements more than one interface. 2. Creating an interface that extends another interface. 3. Creating a class that implements an interface and accessing the class object through an interface reference.

Uploaded by

Khan.ali
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views

Java p10

This document discusses implementing interfaces in Java through 3 examples: 1. Creating a class that implements more than one interface. 2. Creating an interface that extends another interface. 3. Creating a class that implements an interface and accessing the class object through an interface reference.

Uploaded by

Khan.ali
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Practical 10:- Create a program in Java to implement an interface.

1. Create a class implementing more than one interfaces.


2. Implement an interface implementing other interfaces
3. Create a class implementing the other interface.
4. Access class objects through references.

1. INTRODUCTION

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 achieve abstraction. There can be only abstract
methods in the Java interface, not method body. It is used to achieve abstraction and
multiple inheritance in Java. In other words, you can say that interfaces can have abstract
methods and variables. It cannot have a method body.
Java Interface also represents the IS-A relationship. It cannot be instantiated just like the
abstract class.
One interface can inherits another interface by use of the keywords extends. When it’s
class implement an interface that inherit another Interface , it must provide
implementations for all methods in the both Interface.
Interface do not have constructors, main method, static keyword, final keyword or private
methods.

1.1 Syntax of creating interface

interface interface_name{
……….
//Methods or variables
……………
}

1.2 Implementing interface to another interface using extends keywords

interface inter_face-1{
…………. // methods
}

interface inter_face-2 extends inter_face-1{


…………// implement methods of above interface
}
1.3 Implementing interface to another class using implements keyword

class class-name implements interface_name{


……….
}

//Java program to implement Interference to a class


interface ad1 { Interface ad1

void Display();
}
class TestClass implements ad1 { Class TestClass
public void Display() {
System.out.println("\nThis Method is inside Interface.");
}
}
public class Interface1 {
public static void main(String args[]) {
System.out.print("\nTHIS IS PROGRAM FOR INTERFACE.");
TestClass obj1 = new TestClass();
obj1.Display();
}

}
//Output

//Java program to implement more than 1 interface to a class


interface ad1 {
Interface ad1 Interface ad2
void Display();
}

class TestClass
interface ad2 {
void ADD();
}
class TestClass implements ad1, ad2 {
public void Display() {
System.out.println("\nThis Method is inside Interface.");
}
public void ADD() {
int A = 5;
int B = 8;
System.out.println("\nSum of A and B= " + (A + B));
}
}

public class Interface2 {


public static void main(String args[]) {
System.out.print("\nTHIS IS PROGRAM FOR INTERFACE.");
TestClass obj1 = new TestClass();
obj1.Display();
obj1.ADD();
}
}
//Output

//Java program to implement interface inheriting other interface


interface ad1 {
int A=13;
}

interface ad2 extends ad1 {


int B=51;
}

interface ad3 extends ad1, ad2 {


void ADD();
}
class TestClass implements ad3 {
public void ADD() {
System.out.println("\nSum of A and B =" + (A + B));
}
}

public class Interface3 {


public static void main(String args[]) {
System.out.print("\nTHIS IS PROGRAM FOR INTERFACE.");
TestClass obj1 = new TestClass();
// obj1.Display();
obj1.ADD();
}
}
//Output

//Java program to access class object through reference.


interface ad1 {
int A = 2;
int B = 2;
//public void ADD();
}
class TestClass implements ad1{
public void ADD() {
System.out.println("\nSum of A and B =" + (A + B));
}
}
public class Interface4 {
public static void main(String args[]) {
System.out.print("\nTHIS IS PROGRAM FOR INTERFACE.");
TestClass obj1 = new TestClass();
ad1 objint;
objint = obj1;
obj1.ADD();
}
}
//Output

You might also like