Initializing and using Interfaces in Java? -


first of all, learned that:

  • you cannot instantiate interface
  • an interface doesn't implement functions

after seeing following java code:

public class myclasstest {      public static void main(string[] args) {          // string charsequence?          charsequence c = "java";          system.out.println(c);         system.out.println(c.length());     } } 

i confused when learned charsequence interface

  • how can use interface object , initialize it?

  • why charsequence implements length function if interface?

"java" instance of string class, implements charsequence interface, includes implementing length() method. therefore can assign charsequence variable.

a variable type interface can assigned references instances (objects) of classes implement interface.


Comments

Popular posts from this blog

magento2 - Magento 2 admin grid add filter to collection -

Android volley - avoid multiple requests of the same kind to the server? -

Combining PHP Registration and Login into one class with multiple functions in one PHP file -