2️⃣

15.2 Abstract Methods

An Abstract Method is basically a method without a code body. The implementation of an abstract method depends on the child class that extends the abstract class that the abstract method is in. Thus, if the child class that extends the abstract class does not have the implementation of all of the abstract methods in that class, you will get a compilation error.
 
Continuing the example from Abstract Classes, you have an abstract method called noise() in an abstract class called Animal. The child class that extends Animal should have the implementation for all the methods in the abstract class.
 
abstract class Animal { public abstract void noise(); } public class Cow extends Animal { public void noise() { System.out.println("Moo!"); } }
 
The implementation for the abstract method above can be found in Cow. If you wanted to display the sound of a different animal, you would create a new child class that extends Animal and code the implementation for the abstract method in the child class that you just created.
 
You can also use abstract methods in Interfaces. All methods in an interface are abstract by default, so there is no need to explicitly specify that the method is abstract or public. We’ll get into interfaces a little more in the next lesson!
 
⚖️
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.