Closed. This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 6 years ago. Improve this question Recently I’ve been asked a question which is, what’s the difference between Golang and Java about interface? I know there
Tag: interface
Is it OK to add default implementations to methods of an interface which represents a listener?
There is a certain library I use in my project. This library has an interface which has about 15 methods. The purpose of this interface is to make it possible to subscribe for some events that are generated in the library. A listener class in application can implement this interface and register itself as a listener in the library to
Logging from default interface methods
Salut to all Java gurus! Since Java8 we can have default implementations in interfaces (yay!). However problem arises when you want to log from default method. I have a feeling that it is not wise to call .getLogger() every time I want to log something in a default method. Yes, one can define static variable in an interface – but
Can an interface method have a body?
I know that an interface is like a 100% pure abstract class. So, it can’t have method implementation in it. But, I saw a strange code. Can anyone explain it? Code Snippet: EDIT: My IDE is Intellij Idea 13.1. The project SDK is java 7 <1.7.0_25>. The IDE is not showing any compiler error. But, When I compile the code
Implementing two interfaces with two default methods of the same signature in Java 8
Suppose I have two interfaces: If I want to implement both of them, what implementation will be used? Answer This is a compile-time error. You cannot have two implementation from two interfaces. However, it is correct, if you implement the getGreeting method in C1: I just want to add that even if the method in I1 is abstract, and default
Is there such a thing as a “local interface” in Java?
Java allows me to define local abstract classes, like in this example: For some reason, when I try to define a “local interface” instead of the local class, like this: Java complains that “The member interface Bar can only be defined inside a top-level class or interface”. Is there a reason for this? Or am I missing a mistake I
Can a normal Class implement multiple interfaces?
I know that multiple inheritances between Interfaces is possible, e.g.: But is it possible to have a regular Class inherit from multiple Interfaces like this: Answer A Java class can only extend one parent class. Multiple inheritance (extends) is not allowed. Interfaces are not classes, however, and a class can implement more than one interface. The parent interfaces are declared
How to use interface to communicate between two activities
I am trying to make listener interface between two activities Act1 and Act2. Act1 will start Act2. If there is some event occurred in Act2, it will inform it to Act1. Problem is that I am starting new activity using Intent, so how Act1 will assign itself as listener to Act2’s interface? Act1.java Act2.java Note: Please don’t suggest me to
An interface with different method parameters
I want to declare an interface be use with several classes this classes have method with different parameters interface: class A: class B: how to impelement of this interface? Answer you could make an operand-object or
How to implement enum with generics?
I have a generic interface like this: This interface has limited instances, hence it would be best to implement them as enum values. The problem is those instances have different type of values, so I tried the following approach but it does not compile: Any idea about this? Answer You can’t. Java doesn’t allow generic types on enum constants. They