I am trying to understand the concept of an interface in java so far what I understand is that an interface is java is.
That an interface as below constructs a formal contract between to parties, party A the developer of the interface and party B, the user of the class developed by party B. That any class created that implements the interface must provide the behavior provided by the interface created by Party A.
An object instance created from a class that implements the class will be able to have functionality / behaviour / sub rounties run on that are contained by the interface?
And that an interface acts also as a blue print for the class?
And that an interface should be coded to and not coded from?
package house;
public interface Infratructure {
public int numberBedrooms();
public int numberBathrooms();
public boolean attic();
public int atticSize();
public boolean lounge();
public int loungeSize();
public boolean kitchen();
public int kitchenSize();
public boolean grarage();
public int garageSize();
public boolean basement();
public int basementSize();
}
Really the interface is a formal contract between two components (software), you don’t define an interface in base to requirements of programers, instead you must define it in base a software requirements.
Yes the interface define the behavior. So you must have only the public behavior of a group of classes this enforces the concept of encapsulation.
The interface doesn’t contain (implement) any behavior, functionality, sub routines. It just declares it. Letting to the classes the work of implementation.
Yes, if you design your system in base a interfaces you will get more granularity and low cohesion of component.