Hello, dear Jetto Forum members!
Today, I will demonstrate the fundamental principles of OOP, including objects and inheritance, using a coffee shop simulation. I believe that writing code on top of theoretical knowledge will reinforce your understanding of OOP better. Simply having theoretical knowledge is not enough to learn OOP. Just as you need to dive into the water to learn swimming, you need to code to learn OOP.
If you’re ready, let’s start coding!
First, since coffee and tea are both beverages, let’s create a class for beverages:
class Beverage {
String type;
Beverage(String type) {
this.type = type;
}
@Override
void prepare() {
System.out.println(type + " is being prepared...");
}
}
Daha Çok Göster
Our Beverage class is ready. The fields and methods here represent the common properties and behaviors for all beverages.
class Coffee extends Beverage {
String beanType;
// constructor
Coffee(String type, String beanType){
super(type);
this.beanType = beanType;
}
@Override
void prepare() {
System.out.println("Preparing " + type + " with " + beanType + " beans...");
}
}
Daha Çok Göster
The Coffee class inherits from the Beverage class, using the type field. It has its own property, beanType, and overrides the prepare() method.
The super(type) keyword is used to inherit the type property from the Beverage class.
Finally, let’s create the Tea class:
class Tea extends Beverage {
String variety;
Tea(String type, String variety) {
super(type);
this.variety = variety;
}
@Override void prepare() {
System.out.println("Brewing " + variety + " tea... " + type + " is being prepared...");
}
}
Daha Çok Göster
The Tea class inherits from the Beverage class, using the type field. It has its own property, variety, and overrides the prepare() method.
The super(type) keyword is used to inherit the type property from the Beverage class.
Lastly, let’s use these features in the Main class through the main method:
public class Main {
public static void main(String[] args) {
Coffee americano = new Coffee("Americano", "Arabica");
Tea blackTea = new Tea("Black Tea", "Ceylon");
americano.prepare();
blackTea.prepare();
}
}
Americano (Coffee) and Black Tea (Tea) objects are created. Then, we call the prepare methods through these objects.
The program output will be as follows:
Here, I used the following OOP principles:
- Objects: The americano and blackTea objects represent real-world coffee and tea.
- Inheritance: The Coffee and Tea classes inherit common properties and behaviors from the Beverage class.
- Override: The prepare() method is implemented differently for each subclass (polymorphism).
This Coffee Shop simulation program is a simple example that will help you understand OOP. Later, you can add different types of beverages, manage their contents, prices, and even customer orders. With OOP, your code will be more organized, understandable, and extensible.
In the next article, we will continue exploring other important OOP concepts and similar programming exercises. Feel free to leave your questions or comments below.
Happy Coding!