[vc_row css_animation=”” row_type=”row” use_row_as_full_screen_section=”no” type=”full_width” angled_section=”no” text_align=”left” background_image_as_pattern=”without_pattern”][vc_column][vc_column_text]

Interface in Java

[/vc_column_text][vc_separator type=”normal” up=”1″][vc_column_text css=”.vc_custom_1569936342211{background-color: #f6f6f6 !important;}”]

Interface in Java is a way of predetermining the methods a class should contain.

[/vc_column_text][vc_empty_space height=”20px”][vc_column_text]

WHAT IS AN INTERFACE IN JAVA ?

 

An interface determines what methods a class should have; this is very effective when we want to create several classes with similar properties. In larger projects, you may be several separate project groups that are coding for the same project. Then it is highly important to have predetermined requirements so that all programmers based on the same provision.

 

 

All the methods specified in the interface must be in the class that is being implemented, otherwise we will get a compilation error

 

 

Interface in Java,

  • It is a template of the properties/methods a class should have.
  • Additionally, it is one way to define a class so that all classes hold the same structure.
  • It is used for classes with a similar structure.
  • You create an interface in Java with the reserved order interface
  • To create a class from an interface, you use the reserved word implements

[/vc_column_text][vc_empty_space height=”20px”][vc_column_text]

[/vc_column_text][vc_empty_space height=”20px”][vc_separator type=”normal”][vc_empty_space height=”20px”][vc_column_text]

Syntax – Declaring an Interface in Java

 

The syntax for an interface in Java,

 

public interface Name {

    data-type   method1(input parameters);
    data-type2  method2(input parameters);

    And so on ...
}

 

  • You create an interface in Java with the reserved word interface
  • Within the brackets, the methods, that is, we state the characteristics of the class
  • You only specify what methods the class should have, not what the methods should do
  • The praxis is that the class name starts with a capital letter.
  • When you implement a class from an interface, the class automatically gets all the methods specified in the interface.
  • You create a class from an interface through the reserved command implements.

[/vc_column_text][vc_empty_space height=”30px”][vc_column_text]

[/vc_column_text][vc_empty_space height=”15px”][vc_separator type=”normal”][vc_empty_space height=”15px”][vc_column_text]

Example – Declaring an Interface

In this example, we want to program several different animals. Animals have many similar characteristics, so it is perfect to use an interface that each animal can rely on. In this way, every new animal we create can be based on the same template (interface) and have the same characteristics. In other words, you don’t have to program the same thing several times![/vc_column_text][vc_empty_space height=”30px”][vc_single_image image=”20055″ img_size=”large” alignment=”center” onclick=”link_image” qode_css_animation=””][vc_empty_space height=”30px”][vc_column_text]First, we create the interface named Animals,

 

public interface Animal {
    String whatDoesTheAnimalDoes();
    String whatDoesTheAnimalSay();
    void setName(String name);
    void setAge(int age);
}

 

In this interface we decide that all classes that are of the type “Animal” should have four methods:

  • whatDoesTheAnimalDoes()
  • whatDoesTheAnimalSay()
  • setName()
  • setAge()

 

Further, if we, for example, want to create class for a Cow, we write:

 

public class Cow implements Animal {
    // Class Variables
    String name;
    int age;

    //Defines the constructor
    Cow(String name, int age){
        this.name = name;
        this.age = age;
    }

    @Override
    public String whatDoesTheAnimalDoes() {
        return null;
    }

    @Override
    public String whatDoesTheAnimalSay() {
        return null;
    }

    @Override
    public void setName(String name) {

    }

    @Override
    public void setAge(int age) {

    }

}

Note:

  • In some IDEs, such as IntelliJ, the methods are automatically created when the implements command is specified.
  • The methods are created automatically by the constructor when the implements command is specified, they have to be found in the interface
  • To show that the methods have been introduced, we use the @Override command.

 

The methods are automatically created by the compiler when the implements command is used. All the methods specified in the interface must be in the class that is being implemented, otherwise we will get a compilation error. That the methods have been implemented correctly is shown by the compiler with @Override. This makes it easier to find any errors, for example if a method happens to be misspelled.

 

 

Let’s put some operations in the methods that we have in our cow class.

 

public class Cow implements Animal {
    // Class Variables
    String name;
    int age;

    //Defines the constructor
    Cow(String name, int age){
        this.name = name;
        this.age = age;
    }

    @Override
    public String whatDoesTheAnimalDoes() {
        return "The cow is eating";
    }

    @Override
    public String whatDoesTheAnimalSay() {
        return "The cow says Moo";
    }

    @Override
    public void setName(String name) {
        this.name = name;
    }

    @Override
    public void setAge(int age) {
        this.age = age;
    }
}

[/vc_column_text][vc_empty_space height=”36px”][vc_column_text]Further, if we want to create a Pig class we declare it in a similar way and enter information for each method in each class.

 

 

public class Pig implements Animal {
    // Class Variables
    String name;
    int age;

    //Defines the constructor
    Cow(String name, int age){
        this.name = name;
        this.age = age;
    }

    @Override
    public String whatDoesTheAnimalDoes() {
        return "The pig is sleeping";
    }

    @Override
    public String whatDoesTheAnimalSay() {
        return "The pig says grunt grunt";
    }

    @Override
    public void setName(String name) {
        this.name = name;
    }

    @Override
    public void setAge(int age) {
        this.age = age;
    }
}

[/vc_column_text][vc_empty_space height=”36px”][vc_column_text]Finally, we create a main-class and create an object from each class (the Cow and the Pig)

 

public class Farm {

    public static void main(String args[]){
        Cow cow = new Cow("Rose", 3);
        Pig pig = new Pig("Piggy", 1);

        System.out.println(cow.whatDoesTheAnimalSay());
        System.out.println(pig.whatDoesTheAnimalSay());
    }
}

 


The print out from the method:  whatDoesTheAnimalSay() when called from the each of the objects is:

 

The cow says Moo 
The pig says grunt grunt

[/vc_column_text][vc_empty_space height=”36px”][vc_column_text]

[/vc_column_text][vc_empty_space height=”20px”][vc_separator type=”normal”][vc_empty_space height=”40px”][/vc_column][/vc_row][vc_row css_animation=”” row_type=”row” use_row_as_full_screen_section=”no” type=”full_width” angled_section=”no” text_align=”left” background_image_as_pattern=”without_pattern”][vc_column][vc_column_text]

Previous Page    |    Next Page

[/vc_column_text][vc_empty_space][/vc_column][/vc_row]