Interface Flashcards

1
Q

What is Interface?

A

Interfaces group similar functionality and capabilities of different classes together. They model “act as” relationship

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Do all methods declared in Interface need to be defined?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Should signature in classes implementing interfaces be the same?

A

Signature(name and number/type of arguments) should be the same in class-defined methods.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is an override?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Why should we use @override?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How many interfaces can a class implement?

A

A class can implement multiple interfaces. Implementing class must define every single method in each of its interfaces.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What all you can have in the Interface body?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly