Redefining an instance
method in a class inherited from the super-class, is called method overriding.
Example
Let's consider the following
declarations of class A and class B:
class A { public void print() { System.out.println("In Class A"); } } class B extends A { public void print() { System.out.println("In Class B"); } }
Class B is a subclass of class A. Class B inherits the print() method from its superclass and redefines it. The print() method in class B overrides the print() method of class A.
Use of Java Method Overriding
o Provides
specific implementation of a method that is already provided by the super
class.
o Used
for runtime polymorphism. It means which overridden method is to be
invoked will be determined by reference type and not the instance type.
Method Override Rules
o Overriding
method must have same name as in the parent class.
o The
argument list must exactly match that of the overridden method.
o Return
type must be the same as, or subtype of the return type declared in overridden
method in Super class.
o Instance
methods can be overridden only if they are inherited by the subclass.
o Final method cannot be
overridden.
o Static methods cannot be
overridden.
o Constructors
cannot be overridden.
o A
subclass must use the ‘super’ keyword in order to invoke an overridden method
in the superclass. A subclass cannot override fields of the superclass, but it
can hide them.
class A { public void print(String name) { System.out.println("In Class " + name); } } class B extends A { public void print(String name) { // Call print() method of A class super.print(“A”); System.out.println("In Class " + name); } } public class Main { public static void main(String[] args) { B obj = new B(); obj.print(“B”); } }
Output
In
Class A
In
Class B
Access Level
o The access level of
the overriding method must be at least the same or more relaxed than that of
the overridden method.
The following table lists allowed
Access Levels for an Overriding Method
Overridden
Method Access Level
|
Allowed
Overriding Method Access Level
|
public
|
public
|
protected
|
public,
protected
|
package-level
|
public,
protected, package-level
|
Exception Handling with Overridden methods
o
If the superclass method does not declare an exception, subclass overridden
method cannot declare the checked exception but it can declare unchecked
exception.
o
If the superclass method declares an exception, subclass overridden method can
declare narrower exception or no exception but cannot declare broader
exception.
Example:
o
If the superclass method does not declare an exception, subclass overridden
method cannot declare the checked exception.
class A { public void print() { System.out.println("In Class A"); } } class B extends A { public void print() throws IOException { System.out.println("In Class B"); } } public static void main(String args[]) { A obj = new B(); obj.print(); }
Output
Compile
Time Error
o
If the superclass method does not declare an exception, subclass overridden
method cannot declare the checked exception but can declare unchecked
exception.
class A { public void print() { System.out.println("In Class A"); } } class B extends A { public void print() throws IndexOutOfBoundsException { System.out.println("In Class B"); } } public static void main(String args[]) { A obj = new B(); obj.print(); }
Output
In
Class B
o
If the superclass method declares an exception, subclass overridden method can
declare narrower exception or no exception but cannot declare broader
exception.
class A { public void print()throws IndexOutOfBoundsException { System.out.println("In Class A"); } } class B extends A { public void print() throws Exception { System.out.println("In Class B"); } } public static void main(String args[]) { A obj = new B(); obj.print(); }
Output
Compile
Time Error
class A { public void print()throws Exception { System.out.println("In Class A"); } } class B extends A { public void print() throws IndexOutOfBoundsException { System.out.println("In Class B"); } } public static void main(String args[]) { A obj = new B(); obj.print(); }
Output
In
Class B
0 comments:
Post a Comment