Avatar ·

Implements and extends in Java: when to use and what's the difference?

Hi everyone! I started learning Java and can't understand the difference between the keywords extends and implements. They both seem to be used for inheritance, but when to use which? \n\nI wrote a small piece of code, but it doesn't compile and I feel like I'm confusing the concepts:\n\n```java\nclass Animal {\n void eat() {\n System.out.println("Eating");\n }\n}\n\ninterface Pet {\n void play();\n}\n\n// Here I'm not sure\nclass Dog extends Animal implements Pet {\n // trying to override the method from the interface\n void play() {\n System.out.println("Playing with a ball");\n }\n}\n\npublic class Main {\n public static void main(String[] args) {\n Dog dog = new Dog();\n dog.eat();\n dog.play();\n }\n}\n```\n\nError: method play() in Dog does not override the method