Core JAVA
Java Runtime Environment (JRE)
Java Virtual Machine (JVM)
Java overview
Java basics
Java Objects and classes
Java Constructors
Java basic datatypes
Java variable types
Java modifiers/Access Modifiers In Java
Java Basic Operators
Java Loops and Controls
Java conditions
Java numbers and characters
Java strings
Java arrays
Java date time
Java methods
Java file and IO operations
Java exceptions
Inner class
Java OOPs Concepts
Java Inheritance
Java Polymorphism
Java Abstraction
Java Encapsulation
Java Interface
Cohesion and Coupling
Association, Aggregation and Composition
Java Collections
Java ArrayList
Java LinkedList
Set and HashSet
LinkedHashSet and TreeSet
Queue and PriorityQueue
Deque and PriorityQueue
Java Map Interface
Java HashMap
Internal Working Of Java HashMap
Java Mutithread
Methods of Thread In Java
Join , run & Start Method in Threads
Difference b/w start & run Methods in Threads
Java Concurrency Package & its Features
CountDownLatch, CyclicBarrier, Semaphore and Mutex in Thread
Java Interface and Implements
An interface is similar to an abstract class or a class which will have declaration of methods to be implemented, some default and static methods and static final variables. In this article we will see details of java interface implements keyword.
Java interfaces may have no methods or variables at all.
We use the interface keyword in java while declaring an interface. Other classes may implement interface and provide definitions of abstract or declared methods without body.
Important points related to interface:
- Interface may have static final variables and abstract methods.
- Interface may have default and static methods.
- Interface is similar to any class and will generate bytecode class (.class) file after compilation.
- Before Java 8, interfaces never had any concrete or implemented method inside.
- Interface is used to define base structure and key methods skeleton which can be implemented by any child class using implement or extend feature of java.
- Interface provides a high level of abstraction.
Interface Declaration and Example
public interface { //static final variables //skeleton methods’ declaration to be implemented, static and default methods }
In below interface Car, skeletonM1() method is skeleton without body and will be implemented by another child class, defaultM1() and defaultM2() are default methods, staticM1() and staticM2() are static methods.
Example:
public interface Car { String CAR_NAME = "Tesla"; String MODEL = "M2"; void skeletonM1(); //To be implimented default void defaultM1() { //default method 1 System.out.println("I am default method M1"); } default void defaultM2() { //default method 2 System.out.println("I am default method M2"); } static void staticM1() { //static method System.out.println("I am static method M1"); } static void staticM2() { //static method System.out.println("I am static method M2"); } }
Java Interface Implementation Using Implements
We can implement any interface using reserved keyword “implements’ keyword, a class which will implement interface will be like a sub or child class for interface.
In below example we can see how Car interface is implemented by ElectricCar child class.
Example:
public interface Car { String CAR_NAME = "Tesla"; String MODEL = "M2"; void startCar(); //To be implemented void showPowerSource(); //To be implemented default void printCarName() { //default method 1 System.out.println("Car name is: "+CAR_NAME); } static void staticM1() { //static method System.out.println("Car model is: "+MODEL); } } class ElectricCar implements Car { @Override public void startCar() { System.out.println("Car started."); } @Override public void showPowerSource() { System.out.println("Car's power source is electricity stored in battery"); } public static void main(String[] args) { Car electricCar = new ElectricCar(); electricCar.startCar(); electricCar.showPowerSource(); electricCar.printCarName(); Car.staticM1(); //Can be invoked by interface only } }
Output:
Car started.
Car’s power source is electricity stored in battery
Car name is: Tesla
Car model is: M2
Java Interface extending Another Interfaces
An interface in java may extend one or more interfaces as well.
See below example where MyInterface3 is only extending one interface MyInterface1, but MyInterface4 is extending multiple interfaces. Class which will implement interface will be forced to implement all methods of interfaces along with methods of extended interfaces.
Example:
interface MyInterface1 { void method1(); //To be implemented } interface MyInterface2 { void method1(); //To be implemented } interface MyInterface3 extends MyInterface1 { void method1(); //To be implemented } interface MyInterface4 extends MyInterface1, MyInterface2 { void method1(); //To be implemented }
Tagging Interface
An interface which has no methods or variables only empty declaration is called tagging interface, it is used to create a common standard in coding and may be asked to be implemented by most of related classes.
Example:
interface MyTaggingInterface {}
References
Article from oracle.com:Â InterfaceÂ