-2

I was going through an interface in Java from W3c

In Why And When To Use Interfaces?, they have mentioned

1) To achieve security - hide certain details and only show the important details of an object (interface).

but I wasn't able to comprehend how is interface helps in hiding details or security.

In their notes of an interface, they have the following point

  • Interface methods are by default abstract and public
  • Interface attributes are by default public, static and final

Based on my understanding of interface, it looks more like interface helps in decreasing the number of error in code because

  • Interface methods do not have a body - the body is provided by the "implement" class
  • On implementation of an interface, you must override all of its methods

So we declare all the methods with their accessibility, return type and name and when we implement those methods, if we forgot to create a method which we haven't declared in our interface, it's going to throw an error.

Can someone help me in comprehending the relation of the interface in security?

Krishna Sony
  • 1,286
  • 13
  • 27
Alwaysblue
  • 9,948
  • 38
  • 121
  • 210
  • No interfaces does not decrease the numbers of errors in the code. An interface is a contract that a class has promised to follow if it has signed the contract, that is it implements the interface – Joakim Danielson May 31 '20 at 06:44
  • @JoakimDanielson *An interface is a contract that a class has promised to follow if it has signed the contract, that is it implements the interface* What if it `interface a`have some method `public void doSomething()` and `class ABC implements a` but does not have that method `doSomething()`? What will happen then? like what happens if we don't override all the methods? – Alwaysblue May 31 '20 at 06:47
  • @JoakimDanielson Also anything about the security? – Alwaysblue May 31 '20 at 06:48
  • 1
    @Hardik A concrete class _must_ implement all abstract methods inherited from interfaces or abstract superclasses. The compiler enforces this. – Slaw May 31 '20 at 06:50
  • 1
    if you don’t implement a method you get a compilation error sure but this is not relevant to why one uses interfaces in the design – Joakim Danielson May 31 '20 at 06:50
  • Does this answer your question? [What does it mean to "program to an interface"?](https://stackoverflow.com/questions/383947/what-does-it-mean-to-program-to-an-interface) – Progman May 31 '20 at 11:33

1 Answers1

1

1) To achieve security - hide certain details and only show the important details of an object (interface).

Interface contains only those methods which are mandatory to be implemented by its implementing concrete class (omitting default methods since Java 8)

For example,

interface StudentOps {
    void insert(Student);
    void delete(Student);
    void update(Student);
}
class JdbcStudentOpsImpl implements StudentOps {
    public void insert(Student s) {
       // code
    }
    public void delete(Student s) {
       // code
    }
    public void update(Student s) {
       // code
    }

    public void validate(Student s) {
       // code
    }
}

In the above StudentOps is interface and it contains only 3 methods insert, update, delete. When we create an object without knowing the underlying impl, for example,

StudentOps studentOps = Factory.createFor("JDBC");
StudentOps studentOps = Factory.createFor("Kafka");

We cannot access validate() method, which could be hidden for those who are not aware of the JdbcStudentOpsImpl class.

Considering that there are 2 implementations, one for storing the data into a database via JDBC, another is to push it to Kafka.

The implementations may be provided at runtime and the actual programmer's job is to not worry about the implementation as to where the data will be stored, but to just invoke the corresponding operations based on runtime parameters like JDBC, Kafka etc.

JdbcStudentOpsImpl, KafkaStudentOpsImpl can be there in different jars (libraries) which should be included in the classpath at runtime, for example.

In such a case, the programmer who calls Factory.createFor() might not know about those classes. So, he does not know what other methods are there in those classes apart from those in the StudentOps. He is just given an interface to access and he has to look after only those methods defined in the interface.

So, according to the example, insert(), update(), delete() are the important details where as validate() is not.

JavaTechnical
  • 8,846
  • 8
  • 61
  • 97