3️⃣

15.3 Interfaces

Another way to achieve abstraction is through interfaces! Like explained before, all methods in an interface are abstract meaning they have no body. Note that in the interface itself there cannot be a constructor.
 
interface Animal { public abstract void noise(); } public class Cow implements Animal { public void noise() { System.out.println("Moo!"); } }
We can use the same example as before but with an interface Animal, and implementing this interface in class Cow. The keyword implements is similar to extends but is used in reference to interfaces instead.
 
We can think of interfaces as the blueprints or outline for the class. It determines what a program must be able to do but not how to do it. In this example, the interface Animal determines that all animals must be able to make noise. However the class Cow determines that cows make noise by saying “Moo!”
 
⚖️
Copyright © 2021 Code 4 Tomorrow. All rights reserved. The code in this course is licensed under the MIT License. If you would like to use content from any of our courses, you must obtain our explicit written permission and provide credit. Please contact classes@code4tomorrow.org for inquiries.