Methods inherited from the superclass can be overridden, which involves changing their implementation. The original source code in the base class is ignored, and new code replaces it. Abstract methods must be overridden.

Answered on

Methods inherited from the superclass can indeed be overridden in a subclass. To override a method, the subclass writes a new method with the same signature (name, parameters, and return type) as the method in the superclass. The purpose of overriding is to provide a specific implementation in the subclass that is different from the existing implementation in the superclass.

When a method is overridden, any calls to that method on an instance of the subclass will execute the new method code, rather than the code in the superclass. However, the overridden method in the superclass is not modified or ignored; it just isn't called when dealing with instances of the subclass. For example:

```java class Superclass { void display() { System.out.println("Display method in Superclass"); } }

class Subclass extends Superclass { @Override void display() { System.out.println("Display method in Subclass"); } }

public class Test { public static void main(String args[]) { Superclass obj1 = new Superclass(); obj1.display(); // Prints "Display method in Superclass"

Subclass obj2 = new Subclass(); obj2.display(); // Prints "Display method in Subclass" } } ```

In the example above, the `display` method in `Subclass` overrides the `display` method in `Superclass`. When the `display` method is called on an instance of `Subclass`, the overridden method in `Subclass` is executed.

For abstract methods, these are methods declared in an abstract class without an implementation. Any non-abstract class that inherits from an abstract class must override and provide an implementation for all the abstract methods, or else it must be declared abstract as well.

Related Questions