Interface Flashcards
What is Interface?
Interfaces group similar functionality and capabilities of different classes together. They model “act as” relationship
Do all methods declared in Interface need to be defined?
Yes, Interfaces are contracts that classes agree to. If classes don’t implement all the methods then the compiler will give an error. Interfaces cannot be instantiated.
Should signature in classes implementing interfaces be the same?
Signature(name and number/type of arguments) should be the same in class-defined methods.
What is an override?
Override is an annotation and has no effect on how code behaves. It is just a signal to the compiler and to anyone who is reading the code.
Why should we use @override?
1) If the programmer makes any mistake such as wrong method name, wrong parameter types while overriding, you would get a compile-time error. By using this annotation you instruct the compiler that you are overriding this method. If you don’t use the annotation then the sub-class method would behave as a new method (not the overriding method) in sub class.
How many interfaces can a class implement?
A class can implement multiple interfaces. Implementing class must define every single method in each of its interfaces.
What all you can have in the Interface body?
Abstract methods: Methods that are only declared do not need to explicitly mention as abstract
Default methods
Static methods
Constant declaration
By default every member of an interface is public.