0% found this document useful (0 votes)
14 views8 pages

Interfaces 27aug

The document provides three examples of using interfaces in Java. In the first example, a show interface with a display method is implemented by Date, Time, and Identity classes. In the second example, a methods interface with an area method is implemented by Rectangle and Ellipse classes. In the third example, a Polygon abstract class extends methods interface and Rectangle and Triangle classes extend Polygon while Ellipse implements methods directly.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views8 pages

Interfaces 27aug

The document provides three examples of using interfaces in Java. In the first example, a show interface with a display method is implemented by Date, Time, and Identity classes. In the second example, a methods interface with an area method is implemented by Rectangle and Ellipse classes. In the third example, a Polygon abstract class extends methods interface and Rectangle and Triangle classes extend Polygon while Ellipse implements methods directly.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Interface Example 1:

//Programming example using show interface

interface show{

void display();

class Date implements show{

int dd, mm, yy;

Date(int d, int m, int y){

dd = d;

mm = m;

yy = y;

public void display(){

System.out.println("Date = "+dd+'/'+mm+'/'+yy);

class Time implements show{

int hour, min, sec;

Time(int h, int m, int s){

hour = h;

min = m;

sec = s;

public void display(){

System.out.println("Time = "+hour+':'+min+':'+sec);

class Identity implements show{


String name;

Identity(String s){

name = s;

public void display(){

System.out.println("Name = "+name);

class ShowInterfaceDemo{

public static void main(String args[]){

show ref;

ref = new Date(5,3,2020);

ref.display();

ref = new Time(12,48,30);

ref.display();

ref = new Identity("Naveen");

ref.display();

}
Example 2

interface methods{

double PI = 3.14159;

double area();

class Rectangle implements methods{

double length, breadth;

Rectangle(double l, double b){

length = l;

breadth = b;

public double area(){

return length*breadth;

class Ellipse implements methods{

double rx, ry;


Ellipse(double a, double b){

rx = a;

ry = b;

public double area(){

return PI*rx*ry;

class MethodsInterfaceDemo{

public static void main(String args[]){

methods ref;

ref = new Rectangle(4,5);

System.out.println("Area of Rectangle is = "+ref.area());

ref = new Ellipse(2,2.5);

System.out.println("Area of Ellipse is = "+ref.area());

}
Interface Example 3:

interface methods{

double area();

abstract class Polygon{

double x, y;

abstract void display();

Polygon(double x1, double y1){

x = x1;

y = y1;

class Rectangle extends Polygon implements methods{

Rectangle(double l, double b){

super(l, b);

public double area(){


return x*y;

void display(){

System.out.println("Rectangle has four sides");

class Triangle extends Polygon implements methods{

Triangle(double b, double h){

super(b, h);

public double area(){

return 0.5*x*y;

void display(){

System.out.println("Triangle has three sides");

class Ellipse implements methods{

double rx, ry;

Ellipse(double a, double b){

rx = a;

ry = b;

public double area(){

return 3.14159*rx*ry;

class MethodsInterfaceDemo2{
public static void main(String args[]){

Polygon p;

methods ref;

Rectangle r1 = new Rectangle(4,5);

p = r1;

ref = r1;

p.display();

System.out.println("Area of rectangle is = "+ref.area());

Triangle t1 = new Triangle(4,5);

p = t1;

ref = t1;

p.display();

System.out.println("Area of Triangle is = "+ref.area());

Ellipse e1 = new Ellipse(2, 2.5);

ref = e1;

System.out.println("Area of Ellipse is = "+ref.area());

You might also like