Overriding :: When a method in a sub class has same name, same number of arguments and same type signature as a method in its super class, then the method is known as overridden method. Method overriding is also referred to as runtime polymorphism. The key benefit of overriding is the abitility to define method that's specific to a particular subclass type.
- The access level cannot be more restrictive than the overridden method’s access level. For example: if the super class 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.
- A subclass within the same package as the instance’s superclass can override any superclass method that is not declared private or final.
- Constructors cannot be overridden.
Example of override method:
When we commnet Base baseSub=new Sub(); and copy below code exceute it will show classcastexception.Sub subBase=(Sub) new Base();
subBase.read();
O/p:
Private and final methods cannot be overridden.
Can we override staticmethod? think about it before scrolling down.
When you defines a static method with same signature as a static method in base class, it is known as method hiding.
The following table summarizes what happens when you define a method with the same signature as a method in a super-class.
SUPERCLASS INSTANCE METHOD | SUPERCLASS STATIC METHOD | |
---|---|---|
SUBCLASS INSTANCE METHOD | Overrides | Generates a compile-time error |
SUBCLASS STATIC METHOD | Generates a compile-time error | Hides |
in above base class you can add the static keyword infront of void read method()And trying ot create instance method name(read) in sub it will displays following error.
in above Sub class you can add the static keyword infront of void read method()And trying ot create instance method name(read) in Base class it will displays following error.
Method overriding with Exception handling ::1)If the superclass method does not declare an exception, subclass overridden method cannot declare the checked exception but it can declare unchecked exception.
Base class:
public class Base {
Base(){
System.out.println("from base class constructor");
}
public void read() {
System.out.println("read method from base class");
}
}
Subclass:
public class Sub extends Base {
Sub(){
System.out.println("from Sub class constructor");
}
/*public void read() throws ClassCastException {
System.out.println("read method from Sub class");
}
*/
//it will show Exception IOException is not compatible with throws clause in Base.read()
public void read() throws IOException {
System.out.println("read method from Sub class");
}
public static void main(String args[]) {
Base base=new Base();
base.read();
Sub sub=new Sub();
sub.read();
Base baseSub=new Sub();
//Sub subBase=(Sub) new Base();
baseSub.read();
}
}
you can uncomment above commented lines and commented currently showing exception method.
public class Base {
public void read() throws IOException {
System.out.println("read method from base class");
}
}
public class Sub extends Base {
//it does not throw any exception
public void read() throws EOFException {
System.out.println("read method from Sub class");
}
}
public class Sub1 extends Base {
//it does not throw any exception
public void read() { System.out.println("read method from Sub class"); }
}
public class Sub2 extends Base {
//it does throw compilation error as per above rule
public void read() throws Exception { System.out.println("read method from Sub class"); }
}
No comments:
Post a Comment