Java Reference
In-Depth Information
SubClass . You can give any name to this method. However, in the class SubClass ,
you can also name this method print (the same name used by SuperClass ). This is
called overriding,orredefining, the method of the superclass.
To override a public method of the superclass in the subclass, the corresponding method in
the subclass must have the same name, the same type, and the same formal parameter list.
That is, to override a method of a superclass, in the subclass the method must be defined
using the same signature and the same return type as in its superclass. If the corresponding
method in the superclass and the subclass has the same name but different parameter lists,
then this is method overloading in the subclass, which is also allowed.
Whether you override or overload a method of the superclass in the subclass, you must
know how to specify a call to the method of the superclass that has the same name as that
used by a method of the subclass. We illustrate these concepts with the help of an example.
Consider the definition of the following class:
public class Rectangle
{
private double length;
private double width;
public Rectangle()
{
length = 0;
width = 0;
}
public Rectangle( double l, double w)
{
setDimension(l, w);
1
0
}
public void setDimension( double l, double w)
{
if (l >= 0)
length = l;
else
length = 0;
if (w >= 0)
width = w;
else
width = 0;
}
public double getLength()
{
return length;
}
Search WWH ::




Custom Search