| Mobile | RSS

Java Tutorial: Implementing Overriding Methods 

11th Sep, 2009 | No Comment | Posted in Java by Alex Jose

When you extend the functionality of a class, the subclass inherits the data members and methods from the superclass. For example, if the superclass has a show() method, which displays the value of the superclass, the subclass inherits the show() method from the superclass. When you call this method from an object of the subclass, it displays only the data members of the superclass. However, you might also need to display the data members of the subclass. One of the alternatives would be to create a method that calls the show() method of the superclass and also displays the data members of the subclass.

Java has a feature that enables you to redefine the show() method of the superclass. While redefining the method, you can perform the task as per the requirements in the subclass. This feature is known as method overriding.

Method overriding is defined as creating a method in the subclass that has the same return type and signature as a method defined in the superclass. Signature of a method includes the name, number, sequence, and type of arguments of a method. The created method of the subclass hides the method defined in the superclass.

Method overriding implements the concept of polymorphism. It enables you to create objects that respond to the same method as defined in the superclass. The inherited method should have different behavior in the subclass. When an object calls a method, the Java compiler first searches for the method in the class of that object. If the method is not found, then the compiler searches for the method in the class hierarchy until it is found.

You cannot override the static and final methods of a superclass. The Java compiler displays an error message, if you attempt to override a final method. However, a subclass must override the abstract methods of a superclass.

In the library automation example discussed earlier, you can implement method overriding by using the following code:

class Book //Declaration of the Superclass
{
//Declaring the variables.
int price = 50;
int pages = 500;
//Defining the method.
public void show() //declaring the show() method
{
System.out.println(” “);
System.out.println(“\t Books Information”);
System.out.println(“\t Book Price: ” + price);
System.out.println(“\t Number of pages: ” + pages);
System.out.println(” “);
}
}

class SoftwareBook extends Book//Declaration of the Subclass
{
//Declaring the variables.
String softwareName = “Borland C++”;
String softwareVersion = “5.0″;
//overriding the show()method.
public void show()
{
System.out.println(” “);
System.out.println(“\t Software Books Information”);
System.out.println(“\t Software Name: ” + softwareName);
System.out.println(“\t Software Version: ” +
softwareVersion);
System.out.println(” “);
}
public static void main(String args[])
{
SoftwareBook s = new SoftwareBook();
s.show();
}
}

In the preceding code, the SoftwareBook class is the subclass that extends from the Book superclass. The SoftwareBook class defines the data members, softwareName, and softwareVersion that store the name and version of a software book. The SoftwareBook class defines the show() method that overrides the show() method defined in the SoftwareBook superclass.

overriding output

Overriding Methods In Java

Related Posts with Thumbnails