Interface of Java

 You can define(declare + initialize) variable like

interface_name variable_name = new class_name()

In such cases, the variable can only use method overriding the interfaces methods. Also, the language does not recognized it as the class; the language recognizes it as interface. Still, it does have member variables of the class and there are some libraries that enables the programmer to tell what class it is constructed(i.e. Reflection)


Interface can be used as parameter of functions or return types of functions.

When interface is a parameter, the class that implements the interface should be given to the function.

When interface is a return type, it can be used for polymorphism.


Also, variable of java(in case its not primitive type like int, float), it is rather closer to pointer of C++; it refers to the value, not contains value itself.

The important point is that, when the class implements the interface, all of its public methods should be the interface's methods. If not, client might not be able to call the function, because what clients are given is the interface, not the class.

Car car = new CarImpl(); 

The above function can only use the interface 'Car's methods, so if CarImpl has public functions that are not the 'Car's methods, the client might not be able to reach it though its public.

댓글

이 블로그의 인기 게시물

Data Analytics Overview(OLTP vs OLAP)

Leetcode