Method Overriding


Method overriding is when a subclass implements a method with the same name , return type, and parameter list as a method it inherits from a superclass. This differs from method overloading where methods of the same name but different parameter lists are defined. Method overriding is used when a subclass wants to define a method with the same parameters as a superclass method but with a different functionality.

An example of a commonly overridden method is the toString() method defined in the Object class. This method returns a String representation of an object. The default implementation as defined in the Object class simply returns the name of the class and its hash code. The toString() method is overridden by other classes in the Java API to provide a more meaningful description of an instance of the class.

You can call a superclass method that has been overridden using the super keyword.

 super.methodName(arguments); 

This is useful if you don't want to reinvent a method but instead want to augment its functionality.

Example: Overriding Methods

To demonstrate method overriding we will rewrite the BlackBody3 class from Chapter 8. The BlackBody3 class has access to the default toString() method it inherited from the Object class. The default version from the Object class returns the class name followed by its hash code, which is not very informative. The BlackBody4 class overrides the toString() method to return the values of the temperature and emissivity variables for invoking the BlackBody4 object.

 public class BlackBody4 {   public static final double SIGMA = 5.6697e-12;   private double temperature, emissivity;   public BlackBody4(double emiss, double t) {     emissivity = emiss;     temperature = t;   }   public double getTemperature() {     return temperature;   }   public double getHeating() {      return SIGMA*emissivity*Math.pow(temperature,4.0);   }   public String toString() {     String str = "Black Body4: temperature=" +              temperature + " emissivity=" + emissivity;     return str;   } } 

Next we will write a driver program that will create a BlackBody4 object and call its toString() method. When an object is concatenated to a String using the + operator, the object's toString() method is automatically called.

 public class OverrideDemo {   public static void main(String args[]) {     BlackBody4 body = new BlackBody4(0.85, 2000.0);     //  Concatenating an object to a String calls the     //  toString() method associated with the object.     System.out.println("object is " + body);   } } 

Output ”

 object is BlackBody4: temperature=2000.0 emissivity=0.85 

If the toString() method was not overridden, the output would be ”

 object is BlackBody4@ea2dfe 


Technical Java. Applications for Science and Engineering
Technical Java: Applications for Science and Engineering
ISBN: 0131018159
EAN: 2147483647
Year: 2003
Pages: 281
Authors: Grant Palmer

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net