Java Interface

 Interface

         Interface looks like a class but it is not a class.

         An interface can have methods and variables just like the class but the methods declared in the interface are by default abstract (only declaration, nobody)

         Also, the variables declared in an interface are constants

Syntax for defining an interface

interface MyInterface

{

   datatype variable=constant;

   public void method1();

   public void method2();

}


To implement an interface

class classname implements interfacename

{

         body of class

}


Example 01:

package Interface;


interface Animal {

void Sound();

void sleep();

}



Class Dog implements Animal

{

@Override

public void Sound() {

System.out.println("The Dog Sounds like : woof");

}

@Override

public void sleep() {

System.out.println("Dog Sleeping");

}

}


public class interfaceDemo{


public static void main(String args[]) {

Dog o =new Dog();

o.Sound();

o.sleep();

}

}


/*Interface looks like a class but it is not a class. An interface can

have methods and variables

just like the class but the methods declared in the interface are by

default abstract

(only method signatures, nobody, see: Java abstract method).

As mentioned above they are used for full abstraction. Since methods in

interfaces do not have body,

they have to be implemented by the class before you can access them.


The class that implements the interface must implement all the methods of

that interface.

Also, the Java programming language does not allow you to extend more

than one class,

however, you can implement more than one interface in your class.*/


Example 02:

package Interface;


//How Multiple inheritance can be achieved by implementing multiple interfaces


class Phone

{

void voiceCall()

{

System.out.println("Make VoiceClass");

}

void sms()

{

System.out.println("We Can send SMS");

}

}



interface Camera

{

void click();

void record();

}

interface player

{

void play();

void pause();

void stop();

}


class SmartPhone extends Phone implements Camera,player

{

@Override

public void click() {

System.out.println("Take a Selfi");

}

@Override

public void record() {

System.out.println("Take a video"); }

@Override

public void play() {

System.out.println("Play Music");

}

@Override

public void pause() {

System.out.println("Pause Music");

}

@Override

public void stop() {

System.out.println("Stop Music");

}

}

public class interfaceDemo2 {


public static void main(String[] args) {

SmartPhone o =new SmartPhone();

o.voiceCall();

o.sms();

o.click();

o.record();

o.play();

o.pause();

o.stop();

}

}


Example 03:

interface circle

{

         double pi=3.14;

         double radius=5.5;

         void compute();

}

 

class area implements circle

{

         public void compute()

         {

                 double result;

                 result=pi*radius*radius;

                 System.out.println(“Area of Circle=”+result);

         }

}

 

class circumference implements circle

{

         public void compute()

         {

                 double result;

                 result=2*pi*radius ;

                 System.out.println(“Circumference of Circle=”+result);

         }

}

  

class interfacedemo

{

         public static void main(String args[])

         {

                 area a=new area();

                 a.compute();

                

                 circumference c=new circumference();

                 c.compute();

         }

}




Post a Comment

0 Comments