Types and Declarations
Classes
Basic Class Declaration
public class Person {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
}
Inheritance
public class Employee extends Person {
private String department;
public Employee(String name, int age, String department) {
super(name, age);
this.department = department;
}
public String getDepartment() {
return department;
}
}
Abstract Classes
public abstract class Shape {
protected String color;
public abstract double area();
public String getColor() {
return color;
}
}
Interfaces
Basic Interface
public interface Drawable {
void draw();
default void print() {
System.out.println("Printing...");
}
static void info() {
System.out.println("Drawable interface");
}
}
Implementing Interfaces
public class Circle implements Drawable, Comparable<Circle> {
private double radius;
public void draw() {
System.out.println("Drawing circle");
}
public int compareTo(Circle other) {
return Double.compare(this.radius, other.radius);
}
}
Enums
Basic Enum
public enum Day {
MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY
}
Enum with Fields and Methods
public enum Planet {
MERCURY(3.303e23, 2.4397e6),
VENUS(4.869e24, 6.0518e6),
EARTH(5.976e24, 6.37814e6);
private final double mass;
private final double radius;
Planet(double mass, double radius) {
this.mass = mass;
this.radius = radius;
}
public double getMass() { return mass; }
public double getRadius() { return radius; }
}
Records (Java 14+)
Basic Record
public record Point(int x, int y) {}
Records automatically generate:
- Constructor with all components
- Accessor methods (
x(),y()) equals(),hashCode(),toString()
Record with Custom Methods
public record Rectangle(int width, int height) {
public int area() {
return width * height;
}
public Rectangle {
if (width < 0 || height < 0) {
throw new IllegalArgumentException("Negative dimensions");
}
}
}
Sealed Classes (Java 17+)
public sealed class Shape permits Circle, Rectangle, Triangle {
public abstract double area();
}
public final class Circle extends Shape {
private double radius;
public double area() { return Math.PI * radius * radius; }
}
public non-sealed class Rectangle extends Shape {
private double width, height;
public double area() { return width * height; }
}
Generics
Generic Class
public class Box<T> {
private T content;
public void set(T content) {
this.content = content;
}
public T get() {
return content;
}
}
Bounded Type Parameters
public class NumberBox<T extends Number> {
private T value;
public double doubleValue() {
return value.doubleValue();
}
}
public class ComparableBox<T extends Comparable<T>> {
public int compare(T a, T b) {
return a.compareTo(b);
}
}
Generic Methods
public <T> void printArray(T[] array) {
for (T element : array) {
System.out.println(element);
}
}
public <T extends Comparable<T>> T max(T a, T b) {
return a.compareTo(b) > 0 ? a : b;
}
Wildcards
// Unbounded wildcard
public void printList(List<?> list) { }
// Upper bounded wildcard
public double sum(List<? extends Number> list) { }
// Lower bounded wildcard
public void addIntegers(List<? super Integer> list) { }
Arrays
Array Declaration and Initialization
int[] numbers = new int[5];
int[] primes = {2, 3, 5, 7, 11};
String[] names = new String[]{"Alice", "Bob"};
// Multi-dimensional arrays
int[][] matrix = new int[3][3];
int[][] grid = {{1, 2}, {3, 4}, {5, 6}};
Array Operations
int length = numbers.length;
int first = numbers[0];
numbers[0] = 42;
// Enhanced for loop
for (int n : numbers) {
System.out.println(n);
}
Type Inference with var
var number = 42; // int
var message = "Hello"; // String
var list = new ArrayList<String>(); // ArrayList<String>
var map = new HashMap<String, Integer>();
// Works in for loops
for (var item : list) {
System.out.println(item);
}
Note:
var can only be used for local variables with initializers. It cannot be used for fields, method parameters, or return types.
Nested Classes
Inner Class
public class Outer {
private int value;
public class Inner {
public void show() {
System.out.println(value);
}
}
}
Static Nested Class
public class Outer {
private static int staticValue;
public static class Nested {
public void show() {
System.out.println(staticValue);
}
}
}