When a method in a subclass has the same name and type signature as a method in its superclass, then the method in the subclass is said to override the method in the superclass. Let’s get into the details of method overriding. When an overridden method is called from within a subclass, it will always refer to the method defined by the subclass. The method defined by the superclass will be hidden.For Example Example 1: class SuperClass { int i, j; SuperClass (int a, int b) { i = a; j = b; } void show() { System.out.println("i=" + i + " j=" + j); } class SubClass extends SuperClass { int k; SubClass (int a, int b, int c) { super(a, b); k = c; } void show() { System.out.println("k=" + k); } } class MainClass { public static void main(String args[]) { SubClass subObj = new SubClass(1, 2, 3); subObj.show(); // this calls show() in SubClass } } Output: K=3 If you wish to access the superclass version of an overridden function, you can do so by using super. For example, in this version of B, the superclass version of show( ) is invoked within the subclass' version. This allows all instance variables to be displayed. class SubClass extends SuperClass { int k; SuperClass(int a, int b, int c) { super(a, b); k = c; } void show() { super.show(); // this calls SuperClass show() System.out.println("k=" + k); } } If we use above class then the output for Example 1 will be i=2 j=2 Method overriding occurs only when the names and the type signatures of the two methods are identical. If they are not, then the two methods are simply overloaded. You can see reasonable no of questions on Method Overriding in the certification exam. Practice through some good SCJP dumps and see the type of questions that appear in the actual exam. Rules for method Overriding ->Overridden method and argument list shall exactly be the same -> Return type shall be the subtype that is being declared in original method that is overridden in superclass, or it shall be the same. ->Restriction is not more in case of access level as compared to access level of the overridden method. E.g, when superclass is considerd to be public, in that case overriding method present in the subclass would not be public or private. But access level might not be much restrictive in comparison with access level of overridden method. ->If instance methods are inherited, by subclass, in that case only they might be overridden. -> Method which has been declared final might not get overridden. -> Method which has been declared static might be redeclared or not overridden -> Methods are not overridden if they are not inherited. -> Sublcass present in similar kind of package as that of instance superclass might be overridden to any method of superclass that has not been declared final or private. -> Subclass present in some other different package might override those methods which are declared protected or private. Advantages: ->Method overriding is used to provide specific implementation of a method that is already provided by its super class ->Method Overriding is used for RunTime polymorphism By this we have covered an important topic on Method Overriding. Hope this helps you in your SCJP Exam. Author of the article suggests that individuals preparing for Java and Project Management should take preparation courses by Whizlabs. The decade old company has served over 2 million users and has a success rate of 99.7 percent.
Related Articles -
SCJP dumps, SCJP Exam,
|