In a professional automotive system, objects must be self-describing. This project models a Car entity that encapsulates both its specifications (brand and production year) and its presentation logic. By moving the display logic into the Car class itself, we ensure that every vehicle "knows" how to present its details to a potential customer, maintaining high cohesion within the object.
- Model Definition: Created a
Carclass withcarBrandandproductionYearfields. - Behavior Encapsulation: Implemented the
displayDetails()method inside theCarclass. - Universal Initialization: Used a service class to instantiate and populate car data.
- Naming Conventions: Followed PascalCase for classes and camelCase for methods/variables.
- Clean Architecture: Separated the application entry point from data management.
- Java 8+ (Object State and Behavior, SRP)
The displayDetails() method is an instance method. It doesn't need parameters because it has direct access to the carBrand and productionYear fields of the specific object it belongs to. This is a fundamental concept of OOP: data and the methods that work with that data stay together.
Brand: Toyota, production year: 2023
Project Structure:
src/com/yurii/pavlenko/
├── Car.java
├── CarDataInitializer.java
├── CarReporter.java
└── CarShowroomApp.java
Code
package com.yurii.pavlenko;
public class CarShowroomApp {
public static void main(String[] args) {
CarDataInitializer initializer = new CarDataInitializer();
CarReporter reporter = new CarReporter();
Car showroomCar = initializer.createCar("Toyota", 2023);
reporter.printCarDetails(showroomCar);
}
}package com.yurii.pavlenko;
public class CarDataInitializer {
public Car createCar(String brand, int year) {
Car car = new Car();
car.carBrand = brand;
car.productionYear = year;
return car;
}
}This project is licensed under the MIT License.
Copyright (c) 2026 Yurii Pavlenko
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files...
License: MIT