Implementing Interface in Java: Explanation & Practice
Create and implement an interface
Problem summary
Create a Drawable interface with draw() method, implemented by Square.
Starter code
// Create Drawable interface with:
// - void draw()
// Create Square implementing Drawable:
// - implement draw() to print "Drawing square"
public class Main {
public static void main(String[] args) {
// Create square and draw it
}
}Expected output and test cases
- Draw square
Drawing square
Hints
- Use 'implements' keyword
- Interface methods are public abstract by default
- Class must implement all interface methods
Related Inheritance exercises
- Practice Using Super Keyword in Java
- Practice Abstract Classes in Java
- Practice Polymorphism Demo in Java
Practice all Inheritance exercises · Run this idea in the Java compiler