The power of interfaces in Java

 

INTERFACE

The interface keyword can be called as a keyword that can generate a class in java. An interface is like a blueprint for a behavior. It is used to specify a set of methods that a class must implement. Multiple inheritance cannot be done from extends (classes from classes), but multiple inheritance can be done from implements (classes from interfaces). Interfaces are provides a way to achieve abstraction and multiple inheritance in Java.

Difference between Class and Interface


Difference between Extends and Implements

Example:-


You can't create an object for an interface, also you can't create a constructor.

But you can create object by upcasting, the side where the object is generated must be a class.

Since an interface cannot be run, there is no use in having a method.  However, since methods can be overridden, meaningless methods can be created in the interface.

After Java 8, you can keep the normal method, so the method does not have to be meaningless.

You can't keep normal variable in an interface.


You can keep the variable by modifying it with two modifiers called static and final.

But after java 8 you can keep normal variable.

Advantages of Interface📝

👉Helps in achieving abstraction by hiding the complex implementation from the user and focusing only on what the object can do.

👉Allows for the implementation of multiple functionalities, leading to better code organization and flexibility.

👉Allows for writing flexible and reusable code, as methods can accept objects of various types that share a common interface.

👉Allows for smoother versioning and evolution of software systems, as long as the contract remains consistent.

Reference-:

https://www.geeksforgeeks.org

Tutorials List - Javatpoint

W3Schools Online Web Tutorials


Article by-: Rasanjali Herath

E-Mail-:rasanjaliherath899@gmail.com













Comments

Popular posts from this blog

Unveiling the Trinity of Standards, Objects, and Classes

A Guide to Object Generating, Data Types, Parameters and Arguments

Getting Started with Design Patterns - Part 1