java Programs on interface.

interface in java:
in this java tutorial we are learn about interface.. an interface is a blue print in java.interface is a another way of abstraction in java.an interface is completely abstraction class that is group related method with empty body.it is use to achieve abstraction.and multiple inheritance in java.
in the other world , you can say that interface can have abstract method and variable. it can not have method body.
  • IA-S relationship is also represent java interface.
  • it can not be instance just like abstraction class.
why the java interface is used?
  1. it is used achive abstraction
  2. functionality of multiple inheritance are supported by interface.
  3. loose coupling is achieve by interface.
how to declare an interface ?
interface keyword is used to declare interface.it provide total abstraction, it means empty body in the interface are declare all the interface.and all the field are public, static and final by default.a.

syntax:
interface <interface name>{

//declare constant fields
//declare method that abstract
//by default
}

//java Programs on interface.

 

interface area

 

{

public float compute(float x);

 

}

 

class circle implements area{

 

public float compute(float x)

{

 

float pi=3.142f;

 

return(pi*x*x);

 

}

}

 

class inter1

 

{

 

public static void main(String[] args) { circle c1= new circle();

 

area a1;

 

a1=c1;

 

System.out.println("Area of circle is "+a1.compute(3.0f));

 

}

}

 

Output:-

interface

Code:

 

import java.util.Scanner;

 

interface student

{

 

String nm="kiran";

 

public void get();

 

public void display();

}

 

abstract class result implements student

 

{

 

float avg;

int rno,m1,m2;

 

public void get()

 

{

 

Scanner s=new Scanner(System.in);

 

System.out.print("Enter Roll No.:");

rno=s.nextInt();

 

System.out.print("Enter Marks1:");

 

m1=s.nextInt();

 

System.out.print("Enter Marks2:");

m2=s.nextInt();

 

}

 

}

 

class grade extends result

{

 

public void display()

 

{

 

avg=(m1+m2)/2f;

System.out.println("Name:"+nm+"\nRoll

 

No.:"+rno+"\nM1:"+m1+"\nM2:"+m2);

 

System.out.println("Avg:"+avg);

 

}

}

 

class inter3multi

 

{

public static void main(String[] args) { grade r=new grade();

 

r.get();

 

r.display();

 

}

}

 

Output:-

interface

 

Post a Comment

0 Comments