Broad Network


Java Interface Basics

Java Just After the Basics – Part 7

Forward: In this part of the series, I explain Java Interface Basics.

By: Chrysanthus Date Published: 2 Sep 2012

Introduction

This is part 7 of my series, Java Just After the Basics. In this part of the series, I explain Java Interface Basics.

Constant Declaration
An example of a symbolic constant declaration is:

    double E = 2.718282;

Well, this is also an initialization statement for a double data type.

Method Signature
Consider the following method definition:

    int mthd1 (boolean boo, int no, double dou)
        {
            System.out.println(boo);
            System.out.println(no);
            System.out.println(dou);
        }

The first line in this method definition, without the block, and if ending with a semicolon, is an example of a method signature. So the method signature of the above method is:

    int mthd1 (boolean boo, int no, double dou);

A method signature is the first line of a method definition, ending in semicolon, without the block of the method.

Interface
In simple terms, an interface is a kind of class that has only symbolic constant declarations and/or method signatures. The following is an example:

interface NewInterface
    {
        //constant declarations
        double E = 2.718282;
        int theInt = 10;

        //method signatures
        public int mthd1 (boolean boo, int no);
        public void mthd2 (char c, double dou);
    }

To define an interface, you begin with the reserved word, interface, in lowercase. This is followed by the name of the interface. Then you have a block. In the block you type the symbolic constant declarations and the method signatures. Note the reserved word, public, preceding the method signatures. I will explain the use of this word, later.

Many interfaces have mainly method signatures and few or no constant declarations

Implementation of Interface
The body (block) of a method signature is defined in a different class. There can be many classes defining the method signatures. A class must define all the method signatures of the interface or none of the signatures. A constant can be used in any of the method definitions.

When a class defines all the methods, we say the class is implementing the interface. In the following implementation of the above interface, all the constants and methods are employed:

class TheClass implements NewInterface
    {
        public int mthd1(boolean boo, int no)
            {
                System.out.println(E);
                System.out.println(boo);
                System.out.println(no);
                return no;
            }

        public void mthd2(char c, double dou)
            {
                System.out.println(theInt);
                System.out.println(c);
                System.out.println(dou);
            }
    }

Since this class employs all the signatures, after the name of the class you have, “implements NewInterface”. Here, implements, is a reserved word. It is following by the name of the interface. In your code, you will follow this with your own interface name. That is how it is done.

Inside the class, you have the definition (implementation) of the method signatures. You implement a method as if you were defining it for the first time, but remember to use the same name as in the interface and do not write the semicolon after the parameter list (closing parenthesis). I have preceded the definitions of the methods with the reserved word, public. I will explain the use of the word later.

Program Example
The following program shows how an interface can be defined, implemented, and used (in main method):

interface NewInterface
    {
        //constant declarations
        double E = 2.718282;
        int theInt = 10;

        //method signatures
        public int mthd1 (boolean boo, int no);
        public void mthd2 (char c, double dou);
    }

class TheClass implements NewInterface
    {
        public int mthd1(boolean boo, int no)
            {
                System.out.println(E);
                System.out.println(boo);
                System.out.println(no);
                return no;
            }

        public void mthd2(char c, double dou)
            {
                System.out.println(theInt);
                System.out.println(c);
                System.out.println(dou);
            }
    }

class Inter
    {
        public static void main(String[] args)
            {
                TheClass anObj = new TheClass();
                anObj.mthd1(true, 5);
                anObj.mthd2('F', 6.6);
            }
    }

Syntax
The syntax to implement an interface is:

class ImplementationClassName implements InterfaceName
    {
        //methond definitions
    }

The implementation class can have other method definitions and variables; not only the ones (signatures and constants) from the interface.

That is it for this part of the series.

Chrys

Related Articles

Java Course

Comments

Become the Writer's Fan
Send the Writer a Message