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

  1. Use 'implements' keyword
  2. Interface methods are public abstract by default
  3. Class must implement all interface methods

Related Inheritance exercises

Practice all Inheritance exercises · Run this idea in the Java compiler