Overriding Explanation

Overriding methods

Overriding a method means that its entire functionality is being replaced. Overriding is something done in a child class to a method defined in a parent class. To override a method a new method is defined in the child class with exactly the same signature as the one in the parent class. This has the effect of shadowing the method in the parent class and the functionality is no longer directly accessible.

Java provides an example of overriding in the case of the equals method that every class inherits from the grandaddy parent Object. The inherited version of equals simply compares where in memory the instance of the class references. This is often not what is wanted, particularly in the case of a String. For a string you would generally want to do a character by character comparison to see if the two strings are the same. To allow for this the version of equals that comes with String is an overridden version that performs this character by character comparison.

Invoking base class constructors

A constructor is a special method that is automatically run every time an instance of a class is created. Java knows that a method is a constructor because it has the same name as the class itself and no return value. A constructor may take parameters like any other method and you may need to pass different parameters according to how you want the class initialised. Thus if you take the example of the Button class from the AWT package its constructor is overloaded to give it two versions. One is
Button()
Button(String label)
Thus you can create a button with no label and give it one later on, or use the more common version and assign the label at creation time.
Constructors are not inherited however, so if you want to get at some useful constructor from an ancestor class it is not available by default. Thus the following code will not compile
class Base{

public  Base(){}
public  Base(int i){}
}

public class MyOver extends Base{
public static void main(String argvp[]){
        MyOver m = new MyOver(10);//Will NOT compile
        }
}
The magic keyword you need to get at a constructor in an ancestor is super. This keyword can be used as if it were a method and passed the appropriate parameters to match up with the version of the parental constructor you require. In this modified example of the previous code the keyword super is used to call the single integer version of the constructor in the base class and the code compiles without complaint.
class Base{
public Base(){}
public Base(int i){}
}

public class MyOver extends Base{
public static void main(String arg[]){
                MyOver m = new MyOver(10);
                }
        MyOver(int i){
                super(i);
        }
}
       

Invoking constructors with this()

In the same way that you can call a base class constructor using super() you can call another constructor in the current class by using this as if it were a method. Thus in the previous example you could define another constructor as follows
MyOver(String s, int i){
                this(i);
                }

    


Either this or super can be called as the first line from within a constructor, but not both

As you might gues this will call the other constructor in the current class that takes a single integer parameter. If you usesuper() or this() in a constructor it must be the first method call. As only one or the other can be the first method call, you can not use both super() and this() in a constructor
Thus the following will cause a compile time error.
MyOver(String s, int i){
                this(i);
                super();//Causes a compile time error
 

        }
Based on the knowledge that constructors are not inherited, it must be obvious that overriding is irrelevant. If you have a class called Base and you create a child that extends it, for the extending class to be overriding the constructor it must have the same name. This would cause a compile time error. Here is an example of this nonsense hierarchy.
class Base{}
class Base extends Base{} //Compile time error!

       

Constructors and the class hierarchy

Constructors are always called downward from the top of the hierarchy. You are very likely to get some questions on the exam that involve a class hierarchy with various calls to this and super and you have to pick what will be the output. Look out for questions where you have a complex hierarchy that is made irrelevant by a constructor that has a call to both this and superand thus results in a compile time error.


Constructors are called from the base (ancestor) of the hierarchy downwards.

Take the following example
class Mammal{

        Mammal(){
               System.out.println("Creating Mammal");
        }                
}



public class Human extends Mammal{
public static void main(String argv[]){
        Human h = new Human();
        }
        Human(){
        System.out.println("Creating Human");
        }
}
When this code runs the string "Creating Mammal" is output first due to the implicit call to the no-args constructor at the base of the hierarchy.

Comments

Popular posts from this blog

Android Objective type Question and Answers

Android Questions and Answers for written exams

Core Java -----Question and Answers