When a sub class declares a method which exists in super class, then it is called Method Overriding.
Example:
Example:
class SuperClass { void m1(){ System.out.println("SuperClass -> m1()"); } void m2(){ System.out.println("SuperClass -> m2()"); } } class SubClass extends SuperClass{ void m2(){ System.out.println("SubClass -> m2()"); } void m3(){ System.out.println("SubClass -> m3()"); } } class Lab{ public static void main(String[] args) { SubClass sb = new SubClass(); sb.m1(); sb.m2(); sb.m3(); } }Notes on the above program:
- "SubClass" class has implemented m2() method of SuperClass with the same signature i.e m2() method is overriden in SubClass.
- m1() method of SuperClass is not implemented in SubClass class but m1() method of SuperClass is inherited to SubClass.
- m3() method is the newly added method in "SuperClass" class.
Why Override the method?
- According to OOPS, "Classes are closed for modifications" i.e if you want to modify the functionality of existing class, then you should not disturb existing class.
- It is always better to write a sub class and provide the required implementations in sub class.
- Sub Class can be written for following purposes:
- To add functionality
- To modify existing functionality
- To inherit existing functionality.
Rules to override the method.
- Subclass method name must be same as super class method name.
- Subclass method parameters (type, order and number) must be same as super class method parameters.
- Subclass method return type must be same as super class method return type.
Note: If the super class method return type is class type then while overriding the method in subclass you can use same class type or its subclass as return type (FROM JAVA 5).
Example:
class A{} class B extends A{} class C{} class SuperClass{ A m1(){...}; } class SubClass extends SuperClass{ A m1(){...}; B m1(){...}; //Valid from Java 5 C m1(){...}; //Invalid }
- Subclass method access modifier must be same or higher than super class method access modifier:
Super ClassSub Classpublicpublicprotectedprotected, publicdefaultdefault, protected, publicprivateprivate, default, protected, public
- When super class method is instance method, then you have to override in sub class as instance only.
- When super class method is static method then you have override in sub class as static only.
- When super class method is throwing some method level checked exception then sub class method can/cannot do the the following:
- Sub class method can ignore method level exception
- Subclass method can throw the same exception
- Subclass method can throw the exception which is sub class to super class method exception.
- Subclass method cannot throw the exception which is super class to super class method exception.
- Subclass method cannot throw the exception which is non subclass to super class method exception. i.e cannot throw a new checked exception.
- Subclass method can throw any unchecked exception.
- When super class method is throwing some method level unchecked exception then sub class method can do the following:
- Sub class method can ignore that method level exception.
- Sub class method can throw the same exception.
- Sub class method can throw any other unchecked exception.
- Sub class method cannot throw any checked exception.
Example 1:
class A{ void show(){ System.out.println("A->show()"); } } class B extends A{ void SHOW(){ System.out.println("B->SHOW()"); } } public class Lab1 { public static void main(String[] args) { B bObj = new B(); bObj.show(); bObj.SHOW(); //NOT OVERRIDING, JAVA IS CASE SENSITIVE. } }
Example 2:
public class Lab2 { public static void main(String[] args) { B bObj = new B(); bObj.show(99); bObj.show("Sujay"); } } class A{ void show(int ab){ System.out.println("A->show(int)"); } } class B extends A{ void show(String ab){ System.out.println("B->show(String)"); } } //In this case, Overloading is at play and not Overriding
Example 3:
class Lab3{ public void main(String args[]){ B bObj = new B(); bObj.show();on } } class A{ long show(){ return 0; } } class B extends A{ int show(){ return 0; } } /*CTE Lab3.java:15: error: show() in B cannot override show() in A int show(){ ^ return type int is not compatible with long 1 error */
Example 4:
class Lab4{ public static void main(String args[]){ SubC sb = new SubC(); sb.m1(); } } class SuperC{ A m1(){ return new A(); } } class SubC{ B m1(){ return new B(); } } class A{} class B{} /* C.T.E No relation between A and B */
Example 5:
class Lab7{ public void main(String args[]){ B bObj = new B(); bObj.m1(); } } class A{ final void m1(){ return; } } class B extends A{ void m1(){ return; } } /* final method cannot be overriden final method will be inherited final class cannotbe extended by sub class */
Example 6:
class Lab8{ public void main(String args[]){ B bObj = new B(); bObj.m1(); } } class A{ void m1(){ return; } } class B extends A{ final void m1(){ return; } } /* m1() is overriden in B, but cannot be overriden any further. */
Example 7:
class Lab9{ public void main(String args[]){ B bObj = new B(); bObj.m1(); } } class A{ void m1(){ return; } } class B extends A{ static void m1(){ return; } } /* m1() in A is non-static. Therefore, cannot be overriden as static Lab9.java:15: error: m1() in B cannot override m1() in A static void m1(){ ^ overriding method is static 1 error */
Example 8:
class Lab10{ public void main(String args[]){ B bObj = new B(); bObj.m1(); } } class A{ static void m1(){ return; } } class B extends A{ void m1(){ return; } } /* m1() in A is static, it cannot be overriden as static Lab10.java:15: error: m1() in B cannot override m1() in A void m1(){ ^ overridden method is static 1 error */
Example 9:
class Lab11{ public void main(String args[]){ B bObj = new B(); bObj.m1(); } } class A{ static void m1(){ return; } } class B extends A{ static void m1(){ return; } } /*compile fine*/
Example 10:
class Lab12{ public void main(String args[]){ B.m1(); //cannot call m1() by class name as static } } class A{ void m1(){} static void m2(){} } class B extends A{ void m1(){} static void m2(){} } /* CTE Lab12.java:3: error: non-static method m1() cannot be referenced from a static context B.m1(); //cannot call m1() by class name as static ^ 1 error */
Example 11:
class Lab13{ public void main(String args[]){ new B().m1(); } } class A{ private void m1(){} //private method is not visible in sub class. } class B extends A{ //private void m1(){} throws compile time error void m1(){} //no compile time error now...works fine }
No comments:
Post a Comment