CLASSES
Examples of Method Declarations
8.4.8
(0, 0)
(2.7182798, 3.14159)
(2, 3)
(2, 3)
The first line of output illustrates the fact that an instance of
RealPoint
 actu 
ally contains the two integer fields declared in class
Point
; it is just that their
names are hidden from code that occurs within the declaration of class
RealPoint
 (and those of any subclasses it might have). When a reference to an
instance of class
RealPoint
 in a variable of type
Point
 is used to access the field
x
, the integer field
x
 declared in class
Point
 is accessed. The fact that its value is
zero indicates that the method invocation
p.move(1,  1)
 did not invoke the
method
move
 of class
Point
; instead, it invoked the overriding method
move
 of
class
RealPoint
.
The second line of output shows that the field access
rp.x
 refers to the field
x
declared in class
RealPoint
. This field is of type
float
, and this second line of
output accordingly displays floating point values. Incidentally, this also illustrates
the fact that the method name
show
 is overloaded; the types of the arguments in
the method invocation dictate which of the two definitions will be invoked.
The last two lines of output show that the method invocations
p.getX()
 and
rp.getX()
 each invoke the
getX
 method declared in class
RealPoint
. Indeed,
there is no way to invoke the
getX
 method of class
Point
 for an instance of class
RealPoint
 from outside the body of
RealPoint
, no matter what the type of the
variable we may use to hold the reference to the object. Thus, we see that fields
and methods behave differently: hiding is different from overriding.
8.4.8.5
Example: Invocation of Hidden Class Methods
A hidden class (
static
) method can be invoked by using a reference whose type
is the class that actually contains the declaration of the method. In this respect,
hiding of static methods is different from overriding of instance methods. The
example:
class Super {
static String greeting() { return "Goodnight"; }
String name() { return "Richard"; }
}
class Sub extends Super {
static String greeting() { return "Hello"; }
String name() { return "Dick"; }
}
171






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