0
0
Javaprogramming~30 mins

OOP principles overview in Java - Mini Project: Build & Apply

Choose your learning style9 modes available
OOP Principles Overview in Java
πŸ“– Scenario: Imagine you are creating a simple program to manage information about cars in a car rental service. You want to organize the data and behavior of cars using basic Object-Oriented Programming (OOP) principles.
🎯 Goal: Build a Java program that demonstrates the four main OOP principles: encapsulation, inheritance, polymorphism, and abstraction by creating classes and using them step-by-step.
πŸ“‹ What You'll Learn
Create a class with private variables and public methods (encapsulation)
Create a subclass that inherits from a superclass (inheritance)
Use method overriding to show polymorphism
Use an abstract class with an abstract method (abstraction)
Print outputs to show how each principle works
πŸ’‘ Why This Matters
🌍 Real World
Organizing code for vehicle management systems in rental or sales companies using OOP makes the code easier to maintain and extend.
πŸ’Ό Career
Understanding OOP principles is essential for Java developers working on real-world applications, as it helps create clean, reusable, and scalable code.
Progress0 / 4 steps
1
Create the Car class with encapsulation
Create a class called Car with a private String variable model and a private int variable year. Add a public constructor that takes model and year as parameters and sets the variables. Add public getter methods getModel() and getYear() to access these variables.
Java
Need a hint?

Use private for variables and public for methods to follow encapsulation.

2
Create a subclass ElectricCar that inherits from Car
Create a class called ElectricCar that extends Car. Add a private int variable batteryCapacity. Create a constructor that takes model, year, and batteryCapacity and calls the superclass constructor for model and year. Add a public method getBatteryCapacity() to return the battery capacity.
Java
Need a hint?

Use extends to inherit and super() to call the parent constructor.

3
Override a method to show polymorphism
In the Car class, add a public method public String start() that returns the String "Car is starting". In the ElectricCar class, override the start() method to return "Electric car is starting silently". This shows polymorphism by changing behavior in the subclass.
Java
Need a hint?

Use the same method name in the subclass and add @Override above it.

4
Create an abstract Vehicle class to show abstraction and print outputs
Create an abstract class called Vehicle with an abstract method public abstract String fuelType(). Make Car extend Vehicle and implement fuelType() to return "Gasoline". Make ElectricCar override fuelType() to return "Electricity". In a Main class with main method, create a Car object and an ElectricCar object. Print the results of calling start() and fuelType() on both objects.
Java
Need a hint?

Remember to declare Vehicle as abstract and implement fuelType() in subclasses. Use System.out.println() to print outputs.