0
0
Javaprogramming~5 mins

Implementing interfaces in Java - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is an interface in Java?
An interface in Java is a blueprint that defines methods without implementations. Classes can implement interfaces to promise they provide those methods.
Click to reveal answer
beginner
How do you declare that a class implements an interface?
Use the keyword <code>implements</code> after the class name, followed by the interface name. For example: <code>class Dog implements Animal</code>.
Click to reveal answer
intermediate
Can a class implement multiple interfaces in Java?
Yes, a class can implement multiple interfaces by listing them separated by commas. For example: <code>class Robot implements Walkable, Talkable</code>.
Click to reveal answer
intermediate
What happens if a class does not implement all methods of an interface?
The class must be declared abstract, or else the compiler will give an error because it did not fulfill the interface contract.
Click to reveal answer
advanced
Why use interfaces instead of abstract classes?
Interfaces allow multiple inheritance of type, letting a class promise to provide many behaviors. Abstract classes are for shared code and single inheritance.
Click to reveal answer
Which keyword is used to make a class follow an interface in Java?
Ainherits
Bextends
Cimplements
Duses
If a class implements an interface, what must it do?
ANothing, methods are optional
BImplement all methods declared in the interface
CDeclare the class as abstract
DOnly implement some methods
Can a Java class implement more than one interface?
AYes, by listing interfaces separated by commas
BNo, only one interface is allowed
CYes, but only if interfaces extend each other
DNo, Java does not support interfaces
What is the main purpose of interfaces?
ATo store data fields
BTo create objects
CTo provide method implementations
DTo define a contract of methods a class must provide
If a class does not implement all interface methods, what must it be declared as?
Aabstract
Bfinal
Cstatic
Dpublic
Explain how to implement an interface in a Java class and what rules must be followed.
Think about the syntax and what the compiler expects.
You got /3 concepts.
    Describe the difference between interfaces and abstract classes in Java.
    Consider how Java supports multiple behaviors.
    You got /3 concepts.