Creating and Using Interfaces in Java Program

Post on January 4th,2012 by |Posted in programming languages | Tagged in | No Comment | Send Email

When we create a class that uses an interface, we use the keyword implements and the interface name in the class header. Interface provides an empty method header without any instruction within that method and it is the class which implements that interface responsibility to include instructions for that method. In below program I will show you how to write a simple class which implements an interface. First let us create an interface file.


public interface SingSongs {
  public void sing();
}

Then create a class which will implement this interface with the interface method that print a line of output on the screen.

public class SingingBoy implements SingSongs {

public void sing() {

  System.out.println("I am a real singing boy!");

}

}

Finally create a new instance of the above class and use that class method.

public class RealBoySings {

  public static void main(String[] args) {

     SingingBoy boy = new SingingBoy();
     boy.sing();

  }

}

Run the last java file and you should get below outcome:

Next Page :

Javascript replace method review

Javascript replace method review

How to use Java Scanner Class

This article will show you how to use Java Scanner Class with simple Java code.

Show text on JFrame with Java program

Show text on JFrame with Java program!

The if…else statement in Java program

‘The if…else statement in Java program’ article will show you an example to use the statement.

Java Scanner Class

Java Scanner class tutorial will show you how to create an instance of the scanner class and read in the input from the user then print out the sum of those inputs.

Create a simple Java method

An article about how to create a simple Java method.

Leave a Reply

Your email address will not be published. Required fields are marked *

*


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>