Interfaces in Java | What's an interface and how will you go about implementing an interface?

What's an interface and how will you go about implementing an interface?

Interfaces in Java | What's an interface and how will you go about implementing an interface?

(B) What's an interface and how will you go about implementing an interface?

An interface is a named collection of method definitions, without implementations. Interface defines s tocol of behavior for a class. So if "Class!" is adhering to an interface and "Class2" wants to communicate with "Class!" then it should follow the same interface which "Class!" is following". In short it defines a contract to the external consumer of the class i.e. if you want use me then you need to follow these interfaces.

So let's run through a sample in order to understand the interface concept.

In the below sample we want to develop a class which will make us run through software development phases in a proper order. So first let's decide an interface which will have all the necessary vocabulary so that we do not miss anything. So below is the interface with six phases of software life cycle. Preparing this interface is a one time exercise. Now any time we want to do a project regarding software development life cycle we just have

to implement this interface. So we can say interfaces define vocabulary and contract.

Now let's make a class which will implement this interface and provide us with necessary functionalities. This class does not do anything special but will just display the name of the phase. Interfaces are implemented using "implements" keyword. You can see the best thing about using interfaces is that the shell is predefined in this case it's the "ISoftwareLifeCycle"); 

Programmer who is coding "clsSoftware" class does not have to think about what different phases exists just implement the "ISoftwareLifeCycle" interface. In big projects where you have a huge programmer software but rather group coding it's much possible that every one will not follow same naming conventions and method names. 

So the best way is to define an interface and let the programmers implement the interface and write the implementation. Due to this approach every programmer is implementing the same interface it brings in consistency of method and naming conventions through out the project.

Interfaces in Java | What's an interface and how will you go about implementing an interface?

Post a Comment

Thanks for Comment

Previous Post Next Post