Overriding refers to a concept where, a method in the sub-class/sub-classes has the same name and same signature, as that of a method in its super class. We have seen the concept of overloading in the earlier sections, overriding unlike overloading allows us to have 2 or more methods with the same name and also the signature.
Let us look at an example to understand the concept of overriding.
class movie {
String name;
int length;
String director;
int temp;
/**
* Default constructor.
*/
movie() {
this.name = "";
this.length = 0;
this.director = "";
}
/**
* @param name
* @param length
* @param director
*/
movie(String name, int length, String director) {
this.name = name;
this.length = length;
this.director = director;
}
int getTemp() {
return temp;
}
void setTemp(int tempVar) {
System.out.println(" Setting, Super class temp = " + tempVar);
this.temp = tempVar;
}
}
class jamesBondMovies extends movie {
String bondActor;
String bondGirl;
String mRoleCharacterName;
int temp;
/**
* Default constructor.
*/
jamesBondMovies() {
super();
this.bondActor = "";
this.bondGirl = "";
this.mRoleCharacterName = "";
}
jamesBondMovies(String name, int length, String director,
String bondActor, String bondGirl,
String mRoleCharacterName) {
super(name, length, director);
System.out.println(" \t Subclass Parameterized constructor: ");
this.bondActor = bondActor;
this.bondGirl = bondGirl;
this.mRoleCharacterName = mRoleCharacterName;
}
/* "super" keyword is used to access the super classes overridden getTemp() method. */
/* Overridden Method */
int getTemp() {
System.out.print("\t Getting (From James Bond), Temp from Super class = " + super.getTemp());
return temp;
}
/* Overridden Method */
void setTemp(int tempVar) {
super.setTemp(tempVar*2);
System.out.println("\t Setting, James Bond Sub Class temp = " + tempVar);
this.temp = tempVar;
}
}
class harryPotterMovies extends movie {
String harryPotterRole;
String dumbeldoreRole;
/**
* Default constructor.
*/
harryPotterMovies() {
super();
this.harryPotterRole = "";
this.dumbeldoreRole = "";
}
harryPotterMovies(String name, int length, String director,
String harryPotterRole, String dumbeldoreRole) {
super(name, length, director);
this.harryPotterRole = harryPotterRole;
this.dumbeldoreRole = dumbeldoreRole;
}
/* "super" keyword is used to access the super classes overridden getTemp() method. */
/* Overridden Method */
int getTemp() {
System.out.print( "\t Getting (From Harry Potter), Temp from Super class = " + super.getTemp());
return temp;
}
/* Overridden Method */
void setTemp(int tempVar) {
super.setTemp(tempVar*2);
System.out.println("\t Setting, Harry potter Sub Class temp = " + tempVar);
this.temp = tempVar;
}
}
public class overridingExample {
public static void main(String[] args) {
movie movieObj1 = new movie();
jamesBondMovies jbondObj1 = new jamesBondMovies();
harryPotterMovies hpotterObj1 = new harryPotterMovies();
/* An object is not created here, it is just a reference.
* An object is created only when a "new" operator is used.
*/
movie tempObj;
System.out.print(" Object : movieObj1 \n\t");
movieObj1.setTemp(444);
System.out.println("\t Getting, super Class temp val " + movieObj1.getTemp());
System.out.print(" Object : jbondObj1 \n\t");
jbondObj1.setTemp(555);
System.out.println("\n\t Getting, James Bond class temp val " + jbondObj1.getTemp());
System.out.print(" Object : hpotterObj1 \n\t");
hpotterObj1.setTemp(666);
System.out.println("\n\t Getting, Harry Potter class temp val " + hpotterObj1.getTemp());
// Run time Polymorphism.
System.out.print("\n P1: ");
tempObj = jbondObj1;
System.out.println("\n\t Super Class temp val " + tempObj.getTemp());
System.out.print(" P2: ");
tempObj = hpotterObj1;
System.out.println("\n\t Super Class temp val " + tempObj.getTemp());
System.out.print(" P3: ");
tempObj = movieObj1;
System.out.println("\t Super Class temp val " + tempObj.getTemp());
}
}
In the above example, getTemp() and setTemp() methods are overridden. "super" keyword is used to access the overridden method of their respective super-class. The same thing can be noticed with the getTemp() methods in sub-classes. The output below (movieObj1, jbondObj1, hpotterObj1) demonstrates the same.
Compile/Run this program and here is the output:
Object : movieObj1 Setting, Super class temp = 444 Getting, super Class temp val 444 Object : jbondObj1 Setting, Super class temp = 1110 Setting, James Bond Sub Class temp = 555 Getting (From James Bond), Temp from Super class = 1110 Getting, James Bond class temp val 555 Object : hpotterObj1 Setting, Super class temp = 1332 Setting, Harry potter Sub Class temp = 666 Getting (From Harry Potter), Temp from Super class = 666 Getting, Harry Potter class temp val 666 P1: Getting (From James Bond), Temp from Super class = 1110 Super Class temp val 555 P2: Getting (From Harry Potter), Temp from Super class = 666 Super Class temp val 666 P3: Super Class temp val 444
We have already seen the concept, where a sub-class object can be assigned to a reference of a super-class. Using the same concept we are going to see another powerful concept of Java called "Run time Polymorphism". Run time polymorphism is a concept where, the decision to call the correct version of methods is determined at the run time based on the object type. Notice the statement in the code "tempObj = jbondObj1;", what version of the method overridden getTemp() method to call is determined based in this statement. Run time polymorphism is demonstrated in the above output (Sections P1, P2, P3).