Java Class Design
Java Class Design is a fundamental aspect of object-oriented programming (OOP) that involves structuring classes to model real-world entities and behaviors. Understanding how to design classes effectively is crucial for creating maintainable, scalable, and reusable code. This webpage will delve into key concepts related to Java Class Design, providing detailed explanations and examples to enhance your understanding.
1. Encapsulation
Encapsulation is the practice of bundling data (attributes) and methods (behaviors) that operate on the data into a single unit, known as a class. It also involves restricting direct access to some of the object's components, which helps in controlling the state of the object and preventing unintended interference.
Example:
public class Car {
private String model;
private int year;
public Car(String model, int year) {
this.model = model;
this.year = year;
}
public String getModel() {
return model;
}
public int getYear() {
return year;
}
public void setModel(String model) {
this.model = model;
}
public void setYear(int year) {
this.year = year;
}
}
In this example, the Car class encapsulates the model and year attributes, providing public methods to access and modify them. This ensures that the internal state of the Car object is controlled and protected.
2. Inheritance
Inheritance allows a class to inherit properties and behaviors from another class, known as the superclass or parent class. This promotes code reuse and the creation of a hierarchical relationship between classes. The class that inherits is called the subclass or child class.
Example:
public class Vehicle {
protected String type;
public Vehicle(String type) {
this.type = type;
}
public void start() {
System.out.println("Vehicle started");
}
}
public class Car extends Vehicle {
public Car() {
super("Car");
}
@Override
public void start() {
System.out.println("Car started");
}
}
Here, the Car class inherits from the Vehicle class, inheriting the type attribute and the start method. The Car class can override the start method to provide its own implementation.
3. Polymorphism
Polymorphism allows objects of different classes to be treated as objects of a common superclass. It enables methods to be written that can work with objects of multiple types, providing flexibility and extensibility in code design.
Example:
public class Main {
public static void main(String[] args) {
Vehicle vehicle = new Car();
vehicle.start(); // Output: Car started
}
}
In this example, a Car object is assigned to a Vehicle reference. When the start method is called, the overridden method in the Car class is executed, demonstrating polymorphism.
4. Abstraction
Abstraction involves hiding the complex reality while exposing only the essential features. In Java, this can be achieved using abstract classes and interfaces. Abstract classes cannot be instantiated and are meant to be extended, while interfaces define a contract that implementing classes must adhere to.
Example:
public abstract class Shape {
protected String color;
public Shape(String color) {
this.color = color;
}
public abstract double getArea();
}
public class Circle extends Shape {
private double radius;
public Circle(String color, double radius) {
super(color);
this.radius = radius;
}
@Override
public double getArea() {
return Math.PI * radius * radius;
}
}
In this example, the Shape class is abstract, defining an abstract method getArea. The Circle class extends Shape and provides an implementation for getArea, demonstrating abstraction.
5. Composition
Composition is a design principle where a class is composed of one or more objects of other classes, rather than inheriting from them. This allows for more flexible and modular code, as the composed objects can be easily replaced or modified without affecting the main class.
Example:
public class Engine {
public void start() {
System.out.println("Engine started");
}
}
public class Car {
private Engine engine;
public Car() {
this.engine = new Engine();
}
public void startCar() {
engine.start();
System.out.println("Car started");
}
}
In this example, the Car class is composed of an Engine object. The Car class uses the Engine object to perform its functionality, demonstrating composition.
By mastering these concepts, you will be well-equipped to design robust and efficient Java classes, aligning with the principles of OOP and preparing you for the Oracle Certified Professional Java SE 8 Programmer certification.