Polymorphism Part-2 (Method Overriding)
Method Overriding:
In object-oriented
programming, method overriding is a language feature that allows a subclass or
child class to provide a specific implementation of a method that is
already provided by one of its superclasses or parent classes. The
benefit of overriding is the ability to define a subclass implementing a parent
class method based on its requirement that has same name, same parameters or
signature, and same return type as the method in the parent class.
An overriding method
can also return a subtype of the type returned by the overridden method. This
subtype is called a covariant return type. When overriding a method, you
might want to use the @Override annotation
that instructs the compiler that you intend to override a method in the
superclass. If, for some reason, the compiler detects that the method does not
exist in one of the superclasses, then it will generate an error. For more
information on @Override, see Annotations.
Rules for Java Method Overriding
- The argument list should be exactly the same as that of the overridden method.
- Method must have same name as in the parent class.
- Method must have same parameter as in the parent class.
- Must be IS-A relationship (inheritance).
- Method can also return a subtype of the type returned by the overridden method.
- The access level 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.
- Instance methods can be overridden only if they are inherited by the subclass.
- A method declared final cannot be overridden.
- A method declared static cannot be overridden but can be re-declared.
- If a method cannot be inherited, then it cannot be overridden.
- A subclass within the same package as the instance's superclass can override any superclass method that is not declared private or final.
- A subclass in a different package can only override the non-final methods declared public or protected.
- An overriding method can throw any uncheck exceptions, 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.
- Constructors cannot be overridden.
Let’s look at an
example:
class Language {
public void write() {
System.out.println("Any language can be written here");
}
}
class Hindi extends Language{
public void write() {
System.out.println("Hindi is a very sweet language ");
}
}
public class Test {
public static void main(String args[]) {
Language
a =
new
Language();
// Language reference and object
Language
b =
new
Hindi();
// Language reference but Hindi object
a.write(); // runs the method in Language class
b.write(); // runs the method in Hindi class
}
}
Therefore, in the
above example, the program will compile properly since Language class has the
method write. Then, at the runtime JVM, runs the method figure out the object
type and would run the method that belongs to that particular object.
Output
Any language can be
written here
Hindi is a very sweet
language
If you still have any
question you can reach me any time through email (jigyasu2010@hotmail.com)
Comments
Post a Comment