Some good questions about Java

Tram Ho

1. Is it possible to declare static in an interface?

Yes, the method in the interface that is declared as static, the method belongs to the interface and can be accessed through that interface.

    interface Interface {
        static void myMethod ( ) {
            System . out . println ( "static" ) ;
        }
    }
    public class Test implements Interface {
        public static void main ( String [ ] args ) {
            Test o = new Test ( ) ;
            Interface . myMethod ( ) ;
        }
    }

The result is : "static".

In addition, we can override the static method for the class that implements that method.

    interface Interface {
        static void myMethod ( ) {
            System . out . println ( "static" ) ;
        }
    }
    public class Test implements Interface {
        public void myMethod ( ) {
            System . out . println ( "static override" ) ;
        }
        public static void main ( String [ ] args ) {
            Test o = new Test ( ) ;
            o . myMethod ( ) ;
            Interface . myMethod ( ) ;
        }
    }

The result is: "static override" "static".

2. Is it possible to declare final in an interface?

Yes, the final keyword specifies that a variable cannot be reassigned after it is initialized. Therefore, classes that implement that interface cannot reassign the value of that variable.

Note: When declaring a variable in the interface, the value must be assigned and the variable becomes final , public and static . Therefore, this variable belongs to the Interface, not the instances of the class that implements it.

3. Is it possible to define a method body in an interface?

In pure java we are often taught in school that interface is different from abstract class in that it only defines the method, not the method body. But with Java 8, we can define method body in interface with default keyword;

Example:

    interface Interface {
        default void display ( ) {
            System . out . println ( "This is default method of Interface" ) ;
        }
    }

Note:

We can use this default method as abstract method or we can override this method in our class inplements Interface.

4. Is it possible to implement 2 Interfaces with the same two methods?

The answer is yes. But, with one condition is to override the above method.

Example:

    interface Interface1 {
        default void display ( ) {
            System . out . println ( "This is display method of Interface1" ) ;
        }
    }
    interface Interface2 {
        default void display ( ) {
            System . out . println ( "This is display method of Interface2" ) ;
        }
    }
    public class Test implements Interface1 , Interface2 {

        public void display ( ) {
            System . out . println ( "overrided" ) ;
        }

        public static void main ( String [ ] args ) {
            Test o = new Test ( ) ;
            o . display ( ) ;
        }
    }

Thus, the result when running main() will be "overrided".

So what if I don't want to rewrite a new function but I want to call a predefined method in Interface1 or Interface2 ? –> Then you just need to override that method and in there you call the method you need and you're done:

    public class Test implements Interface1 , Interface2 {

        public void display ( ) {
            Interface1 . super . display ( ) ;
            //Or
            // Interface2.super.display();
        }
    }

5. In Java, how many interfaces can be implemented?

That time when I interviewed the fresher, I heard it like: "What's wrong? Who asked what's so weird?". And I don't know how to respond. Then I said there's no limit. Because it has a limit, people have mentioned it while studying.

Actually, yes, everyone. According to orecle , each class or interface has a limit on a certain number of byte lines: 65535(2^16-1) entries by the 16-bit constant_pool_count field of the ClassFile structure . Thus, the maximum number of interfaces that can be implemented depends on the number of lines of byte codes to be compiled or the complexity of the class.

(The knowledge I shared above, mostly I learned it myself, so I can't avoid many mistakes. So if there are any mistakes, hope everyone or comment to let me know, thank you. !).

Share the news now

Source : Viblo