Java - Overriding
Rules for Method Overriding: subclass in a different package
A _______________ can only override the non-final methods declared public or protected.
Rules for Method Overriding: subclass within the same package
A _________________ as the instance's superclass can override any superclass method that is not declared private or final.
Rules for Method Overriding: static
A method declared ______ cannot be overridden but can be re-declared.
Rules for Method Overriding: final
A method declared ___________ cannot be overridden.
Rules for Method Overriding: uncheck exceptions
An overriding method can throw any ____________, regardless of whether the overridden method throws exceptions or not. However, the overriding method should not throw checked exceptions that are new or broader than the ones declared by the overridden method. The overriding method can throw narrower or fewer exceptions than the overridden method.
Rules for Method Overriding: inherited
If a method cannot be _____ , then it cannot be overridden.
Rules for Method Overriding: argument list
The _______ should be exactly the same as that of the overridden method.
Rules for Method Overriding: return type
The _________ should be the same or a subtype of the return type declared in the original overridden method in the superclass.
Rules for Method Overriding: access level
The ___________ cannot be more restrictive than the overridden method's access level. For example: If the superclass method is declared public then the overridding method in the sub class cannot be either private or protected.
super Keyword
When invoking a superclass version of an overridden method the super keyword is used.
Rules for Method Overriding: Constructors
___________ cannot be overridden.
Rules for Method Overriding: Instance methods
____________ can be overridden only if they are inherited by the subclass.
benefit of overriding
ability to define a behavior that's specific to the subclass type, which means a subclass can implement a parent class method based on its requirement.
overriding : example
class Animal { public void move() { System.out.println("Animals can move"); } } class Dog extends Animal { public void move() { System.out.println("Dogs can walk and run"); } } public class TestDog { public static void main(String args[]) { Animal a = new Animal(); // Animal reference and object Animal b = new Dog(); // Animal reference but Dog object a.move(); // runs the method in Animal class b.move(); // runs the method in Dog class } } output : Animals can move Dogs can walk and run
super Keyword Example
class Animal { public void move() { System.out.println("Animals can move"); } } class Dog extends Animal { public void move() { super.move(); // invokes the super class method System.out.println("Dogs can walk and run"); } } public class TestDog { public static void main(String args[]) { Animal b = new Dog(); // Animal reference but Dog object b.move(); // runs the method in Dog class } } output: Animals can move Dogs can walk and run
overriding
to override the functionality of an existing method.