//file: RealPointTester.java
class Point {
int x = 0, y = 0;
void move(int dx, int dy) { x += dx; y += dy; }
int getX() { return x; }
int getY() { return y; }
int color;
}
class RealPoint extends Point {
float x = 0.0f, y = 0.0f; // Hiding super.x, and super.y
void move(int dx, int dy) { move((float)dx, (float)dy); } // Overriding super.move()
void move(float dx, float dy) { x += dx; y += dy; } // Overloading move()
int getX() { return (int)Math.floor(x); } // Overriding super.getX()
int getY() { return (int)Math.floor(y); } // Overriding super.getY()
}
public class RealPointTester {
public static void main(String[] args) {
RealPoint rp = new RealPoint();
Point p = rp;
rp.move(1.718298f, 4.14159f);
p.move(1, -1);
show(p.x, p.y); // (0,0)
show(rp.x, rp.y); // (2.7182798,3.14159)
show(p.getX(), p.getY()); // (2,3)
show(rp.getX(), rp.getY()); // (2,3)
}
static void show(int x, int y) { // Overloading show()
System.out.println("(" + x + ", " + y + ")");
}
static void show(float x, float y) {
System.out.println("(" + x + ", " + y + ")");
}
}
hidden fields and methods of Superclass can be accessed using the keyword super
Example: Overriding, Hiding, and accessing parent's field and method
Parent
public class Parent {
public static int x = 37;
public static void cling() {
stdOut.println("Called parent's cling method.");
}
public void print() {
stdOut.println("Called parent's print method.");
}
}
Child
public class Child extends Parent {
public double x; // hiding
public Child() {
x = 2.5;
}
public static void cling() { // hiding
stdOut.println("Called child's cling method.");
}
public void print() { // overriding
stdOut.println("Called child's print method.");
}
public void printLots() {
stdOut.println("From within child...");
stdOut.println("Calling: print()");
print();
stdOut.println("Calling: super.print()");
super.print();
stdOut.println("Calling: cling()");
cling();
stdOut.println("Calling: super.cling()");
super.cling();
stdOut.println("Calling: Parent.cling()");
Parent.cling();
stdOut.println("x: " + x);
stdOut.println("super.x: " + super.x);
stdOut.println("Parent.x: " + Parent.x);
}
}
ChildTester.java
import java.io.*;
class Parent {
private static PrintWriter stdOut = new PrintWriter(System.out, true);
public static int x = 37;
public static void cling() {
stdOut.println("Called parent's cling method.");
}
public void print() {
stdOut.println("Called parent's print method.");
}
}
class Child extends Parent {
private static PrintWriter stdOut = new PrintWriter(System.out, true);
public double x; // hiding
public Child() {
x = 2.5;
}
public static void cling() { // hiding
stdOut.println("Called child's cling method.");
}
public void print() { // overriding
stdOut.println("Called child's print method.");
}
public void printLots() {
stdOut.println("From within child...");
stdOut.println("Calling: print()");
print();
stdOut.println("Calling: super.print()");
super.print();
stdOut.println("Calling: cling()");
cling();
stdOut.println("Calling: super.cling()");
super.cling();
stdOut.println("Calling: Parent.cling()");
Parent.cling();
stdOut.println("x: " + x);
stdOut.println("super.x: " + super.x);
stdOut.println("Parent.x: " + Parent.x);
}
}
public class ChildTester {
public static void main(String[] arg) throws IOException {
Child son = new Child();
son.printLots();
}
}