Java Interface

Java Interface

Learning Java for the first time was confusing, at first I was curious and excited then came the feeling of "will I ever fully understand?". I quickly developed a love-hate relationship with Java because as soon as I understand one concept another one was presented and I am back at square one, "I know nothing." Needless to say, I didn't have a choice, in order to complete my bootcamp, I had to finish the Java module. I decided to clear my mind from all the negative feelings towards Java and did some extra courses in order to understand clearly and know the why to the how.

Today I present you with an exciting concept in Java - Interface. Like always, I will do my best to explain this in the easiest way possible for all the Java-newbies like me.

Let's start form the very beginning assuming that you are familiar with the basics of Java. An interface is a blueprint of a class, it tells you what to do but not how to do it. It only contains the name of the method and the parameter types of a method. It is completely abstract. It is not an implementation. Any service requirement specification is considered as an interface. It is also a contract between the client (requirement specification) and the service provider (implementing the interface). Interfaces are used by developers to achieve total abstraction (privacy) and multiple inheritance in Java. See example of an interface below:

// interface

interface Animal {

  public void animalSound(); // interface method (does not have a body)
  public void run(); // interface method (does not have a body)

}
        

So now it's clear that an interface is completely abstract and in Java we work with classes so it's an abstract class right? Sadly no, an interface is not an abstract class and the answer as to why is easy. Remember that an interface is not an implementation. No implementation in an interface whereas an abstract class is a partially implemented class or it contains at least one abstract method. Side note, you can't create an object of an abstract class. Example :

abstract class Animal {

  public abstract void animalSound(); // abstract method - no body

  public void sleep() { // regular method - has a body

    System.out.println("Zzz");

  }
}

Animal myObj = new Animal(); // will generate an error        

You are probably wondering why is abstraction an important concept, the most important reason developers use abstraction is for security purposes. Abstraction is hiding internal implementation by making internal code private, just highlighting the services that is offered e.g. the UIG screen at any ATM. Abstraction is great because developers are able to update/change the internal design without effecting the end-user. In regards with interfaces, because there are no implementation code, it is achieving complete abstraction.

Let's look at how to implement an interface so that you can achieve more clarity on this subject. To implement the interface is super easy. We implement all the methods declared in the interface, remember that the important keyword is - implements. The class doesn't need to implement the variables off an interface but definitely each and every method. Look at the example below:

// Pig "implements" the Animal interface

class Pig implements Animal {

  public void animalSound() {

    // The body of animalSound() is provided here

    System.out.println("The pig says: wee wee");
  }

  public void sleep() {

    // The body of sleep() is provided here

    System.out.println("Zzz");
  }
}

class Main
 
  public static void main(String[] args) {

    Pig myPig = new Pig();  // Create a Pig object
    myPig.animalSound();
    myPig.sleep();
  }
}
        

Remember that if a class contains at least one abstract method within the implementation, it should be declared as an abstract class. An abstract method is a method without implementation, meaning it has no body. The body is provided by the inherited child class. Look at the example below:

// Abstract class

abstract class Animal {

  // Abstract method (does not have a body)

  public abstract void animalSound();

  // Regular method

  public void sleep() {

    System.out.println("Zzz");
  }
}

// Subclass (inherit from Animal)

class Pig extends Animal {

  public void animalSound() {

    // The body of animalSound() is provided here

    System.out.println("The pig says: wee wee");
  }
}        

Do you think that it is possible to instantiate an interface? When an interface contains no implementation and we also know now that we can't create an object of an abstract method or of an abstract class, then the answer should be obvious, it's a big no. It's not possible to instantiate an interface because it only contains abstract methods. For this same reason, we do not need to declare a constructor inside an interface because the methods are only declared, not defined.

The last important thing to take in consideration is to never override an interface method with visibility that is not pubic, that just won't make sense, why make it visible if the purpose of an interface is security and privacy. This will violate the encapsulation principle and is considered bad practice.

I hope this gives you clarity on interfaces. It's quite an interesting topic which I enjoyed and we can see how it's very useful for most businesses. For all my fellow newbies, I think working on an interface project will be a great benefit if you are thinking of applying to a Java based job.

Happy coding - Monique

要查看或添加评论,请登录

Monique Scholtz的更多文章

  • Explaining the unexplainable.

    Explaining the unexplainable.

    After doing an exciting coding bootcamp, learning how to be efficient in Python and Java, I had to learn a whole bunch…

社区洞察