Complete the code to declare a class that cannot be extended.
public [1] class Vehicle {}
Using final before a class means it cannot be inherited by other classes.
Complete the code to prevent a method from being overridden in subclasses.
public class Animal { public [1] void sound() { System.out.println("Animal sound"); } }
The final keyword before a method means subclasses cannot override it.
Fix the error in the code by choosing the correct keyword to allow inheritance.
[1] class Shape {} public class Circle extends Shape {}
The class must be public (or default) and not final to allow inheritance.
Fill both blanks to declare an abstract class with an abstract method.
public [1] class Device { public [2] void start(); }
An abstract class can have abstract methods without body.
Fill all three blanks to override a method and prevent further overriding.
public class Parent { public void greet() { System.out.println("Hello from Parent"); } } public class Child extends Parent { @Override public [1] void greet() { System.out.println("Hello from Child"); } } public class GrandChild extends Child { @Override public void greet() { [2].greet(); System.out.println("Hello from GrandChild"); } } // To prevent GrandChild from overriding greet(), add [3] keyword in Child class method.
Marking the Child's greet() method as final prevents GrandChild from overriding it. The super keyword calls the parent's method.