class subclass_name extends base_class_name {
// ...
}
@Override
class Shape {
private int x, y; // Position
public void setPosition(int px, int py) {
x = px;
y = py;
}
@Override public String toString() {
return "Shape(" + x + ", " + y + ")";
}
}
class Circle extends Shape {
private int radius;
public void setRadius(int r) {
radius = r;
}
@Override public String toString() {
return super.toString() + ": Circle(" + radius + ")";
}
}
class Rectangle extends Shape {
private int height, width;
public void setDimensions(int h, int w) {
height = h;
width = w;
}
@Override public String toString() {
return super.toString() + ": Rectangle(" + height + " x " + width + ")";
}
}
class Test1 {
static public void main(String args[])
{
Circle c = new Circle();
Rectangle r = new Rectangle();
r.setPosition(1, 2);
r.setDimensions(50, 50);
c.setPosition(3, 4);
c.setRadius(10);
System.out.println(r);
System.out.println(c);
}
}

class Shape {
private int x, y; // Position
protected int getX() { return x; }
protected int getY() { return y; }
public void setPosition(int px, int py) {
x = px;
y = py;
}
@Override public String toString() {
return "Shape(" + x + ", " + y + ")";
}
}
static public void main(String args[])
{
Rectangle r = new Rectangle();
Shape s;
r.setPosition(1, 2);
r.setDimensions(50, 50);
s = r;
s.setPosition(10, 20);
System.out.println(r);
System.out.println(s);
r = (Rectangle)s;
}
sealed και permits
μπορούμε να
ορίσουμε ποιες κλάσεις επιτρέπεται να επεκτείνουν τη μητρική κλάση.
public sealed class Shape permits Circle, Rectangle {
}
class Circle extends Shape {
}
class Rectangle extends Shape {
}
abstract class Document {
public abstract String identify();
}
abstract class Shape {
private double x, y; // Position
protected double getX() { return x; }
protected double getY() { return y; }
public void setposition(double px, double py) {
x = px;
y = py;
}
public abstract double area();
@Override public String toString() {
return "Shape(x=" + x + ", y=" + y + ", area=" + area() + ")";
}
}
class Circle extends Shape {
private double radius;
public void setradius(double r) {
radius = r;
}
@Override public double area() {
return Math.PI * radius * radius;
}
@Override public String toString() {
return super.toString() + ": Circle(" + radius + ")";
}
}
class Rectangle extends Shape {
private double height, width;
public void setdimensions(double h, double w) {
height = h;
width = w;
}
@Override public double area() {
return height * width;
}
@Override public String toString() {
return super.toString() + ": Rectangle(" + height + " x " + width + ")";
}
}
class Test2 {
static public void main(String args[])
{
Circle c = new Circle();
Rectangle r = new Rectangle();
Shape s[] = new Shape[2];
s[0] = r;
r.setposition(1, 2);
r.setdimensions(50, 50);
s[1] = c;
c.setposition(3, 4);
c.setradius(10);
for (int i = 0; i < s.length; i++)
System.out.println(s[i]);
}
}
Το παραπάνω πρόγραμμα θα τυπώσει:
Shape(x=1.0, y=2.0, area=2500.0): Rectangle(50.0 x 50.0) Shape(x=3.0, y=4.0, area=628.3185307179587): Circle(10.0)
double area = 6 * length * width;
final int CUBE_FACETS = 6;
double area = CUBE_FACETS * length * width;
final ορίζουμε ακόμα ορίσματα που δεν μεταβάλλονται μέσα σε μια μέθοδο.
static double square(final double d) {
return d * d;
}
final ορίζουμε ακόμα πεδία που δεν μεταβάλλονται μέσα στην κλάση.
class FixedPlanarCoordinate {
final int x, y;
final int z = 0;
FixedPlanarCoordinate(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public int getZ() {
return z;
}
}
class PhysicalConstants {
// Speed of light in vacuum (m/s)
public static final double c = 299792458;
// Permeability of vaccum (N/A/A)
public static final double mu0 = 4 * Math.PI * 10e-7;
// Newtonian constant of gravitation (m*m*m/kg/s/s)
public static final double G = 6.67259e-11;
// Planck constant (J*s)
public static final double h = 6.6260755e-34;
// Elementary charge (C)
public static final double e = 1.60217733e-19;
}
final class PasswordChecker {
// Class contents here
}
class Password {
final boolean isPasswordValid() { /* ... */ }
}
Object[] arguments = {
Integer.valueOf(7),
new Date(System.currentTimeMillis()),
"a disturbance in the Force"
};
String result = MessageFormat.format(
"At {1,time} on {1,date}, there was {2} on planet {0,number,integer}.",
arguments);
Εδώ (https://docs.oracle.com/en/java/javase/21/docs/api/index.html) μπορείτε να δείτε την τεκμηρίωση όλων των κλάσεων που προσφέρει η Java.
Κάθε κλάση τεκμηριώνει: