CLASSES
Examples of Method Declarations
8.4.8
the class
RealPoint
 hides the declarations of the
int
 instance variables
x
 and
y
of class
Point
 with its own
float
 instance variables
x
 and
y
, and overrides the
method
move
 of class
Point
 with its own
move
 method. It also overloads the name
move
 with another method with a different signature ( 8.4.2).
In this example, the members of the class
RealPoint
 include the instance
variable
color
 inherited from the class
Point
, the
float
 instance variables
x
 and
y
 declared in
RealPoint
, and the two
move
 methods declared in
RealPoint
.
Which of these overloaded
move
 methods of class
RealPoint
 will be chosen
for any particular method invocation will be determined at compile time by the
overloading resolution procedure described in  15.11.
8.4.8.3
Example: Incorrect Overriding
This example is an extended variation of that in the preceding section:
class Point {
int x = 0, y = 0, color;
void move(int dx, int dy) { x += dx; y += dy; }
int getX() { return x; }
int getY() { return y; }
}
class RealPoint extends Point {
float x = 0.0f, y = 0.0f;
void move(int dx, int dy) { move((float)dx, (float)dy); }
void move(float dx, float dy) { x += dx; y += dy; }
float getX() { return x; }
float getY() { return y; }
}
Here the class
Point
 provides methods
getX
 and
getY
 that return the values of its
fields
x
 and
y
; the class
RealPoint
 then overrides these methods by declaring
methods with the same signature. The result is two errors at compile time, one for
each method, because the return types do not match; the methods in class
Point
return values of type
int
, but the wanna be overriding methods in class
RealPoint
 return values of type
float
.
8.4.8.4
Example: Overriding versus Hiding
This example corrects the errors of the example in the preceding section:
169






footer




 

 

 

 

 Home | About Us | Network | Services | Support | FAQ | Control Panel | Order Online | Sitemap | Contact

java hosting

 

Our partners: PHP: Hypertext Preprocessor Best Web Hosting Java Web Hosting Inexpensive Web Hosting  Jsp Web Hosting

Cheapest Web Hosting Jsp Hosting Cheap Hosting

Visionwebhosting.net Business web hosting division of Web Design Plus. All rights reserved