Extend Classes - Method Overriding
Method Overriding
Subclasses can provide their own implementation of methods inherited from the superclass. This is called method overriding. To override a method, the subclass defines a method with the same name, return type, and parameters as the method in the superclass. Here's an example:
Animal.java
Main.java
In this example, the Dog class overrides the eat method from the Animal class. When we call eat on an instance of Dog, it executes the Dog's version of the method. You also might have noticed the @Override annotation above the eat method in Dog. This annotation is not required, but it will help find mistakes. If you put @Override above a method that doesn't actually override a method in the superclass (for example, if you misspell the method name), you'll get a compilation error.
ByteBrawl
0