Exploring Object-Oriented Goodness with a Fruit Basket

Lakindu Widuranga Alwis
4 min readMay 31, 2023

--

Hello World! Have you ever wondered how object-oriented programming can be as sweet as a fruit basket? Well, get ready to dive into the delicious world of coding with this simple and original explanation of object-oriented concepts. In this blog post, we’ll explore classes, objects, inheritance, encapsulation, and abstraction — all through the lens of managing a fruit basket. So, let’s get started on this flavorful coding adventure.

Class — The Blueprint of Fruity Goodness

In our fruit basket program, a class is like a blueprint or a recipe that defines the properties and behaviours of our fruits. Just like we have a generic “Fruit” class, we can create specific fruit classes like “Apple,” “Banana,” and “Orange.” The Fruit class contains common attributes such as colour, size, and taste, and methods like “ripe” and “eat.” These attributes and methods can be inherited by the specific fruit classes, allowing us to reuse code and define unique characteristics for each fruit.

public class Fruit {
private String color;
private String size;
private String taste;

public void ripe() {
// Implementation for ripening the fruit
}

public void eat() {
// Implementation for eating the fruit
}
}

Object — Tangible Instances of Fruitiness

Now, imagine objects as actual instances of fruits in our basket. Each object represents a specific fruit — be it a juicy apple, a ripe banana, or a citrusy orange. Objects are created based on the blueprint provided by the class. For example, we can create an object of the Apple class, giving it its specific colour, size, and taste. Objects allow us to work with individual instances, manipulate their data, and call their methods.

public class Main {
public static void main(String[] args) {
Fruit apple = new Fruit();
apple.color = "Red";
apple.size = "Medium";
apple.taste = "Sweet";

// Accessing object properties and methods
System.out.println("Apple Color: " + apple.color);
System.out.println("Apple Size: " + apple.size);
System.out.println("Apple Taste: " + apple.taste);

apple.ripe();
apple.eat();
}
}

Inheritance — Sharing the Juicy Goodness

Inheritance is like passing down traits from a parent to their children. In our fruit basket, we can use inheritance to define a hierarchy of fruit classes. The specific fruit classes, such as Apple, Banana, and Orange, can inherit attributes and methods from the generic Fruit class. For instance, the Apple class can inherit the “colour,” “size,” and “taste” attributes from Fruit. This way, we can avoid duplicating code and establish a relationship where the child classes inherit the properties and behaviours of the parent class.

public class Apple extends Fruit {
public void crunch() {
// Implementation specific to apples
}
}

//banana calss
public class Banana extends Fruit {
public void peel() {
// Implementation specific to bananas
}
}

//Orange class
public class Orange extends Fruit {
public void squeeze() {
// Implementation specific to oranges
}
}

Encapsulation — Keeping Things Fresh and Protected

Encapsulation is like packaging our fruits in a protective layer, keeping them fresh and safe. In our fruit basket program, encapsulation refers to the practice of bundling data and methods within a class and controlling access to them. We can define attributes as private or protected, allowing them to be accessed and modified only through specific methods. This way, we maintain data integrity, hide implementation details, and provide a clean interface for interacting with our fruits.

public class Fruit {
private String color;
private String size;
private String taste;

public void ripe() {
// Implementation for ripening the fruit
}

public void eat() {
// Implementation for eating the fruit
}

// Getter and Setter methods for encapsulated attributes
public String getColor() {
return color;
}

public void setColor(String color) {
this.color = color;
}

public String getSize() {
return size;
}

public void setSize(String size) {
this.size = size;
}

public String getTaste() {
return taste;
}

public void setTaste(String taste) {
this.taste = taste;
}
}

Abstraction — Focusing on the Juicy Essence

public abstract class Fruit {
private String color;
private String size;
private String taste;

// Abstract methods representing essential behaviors
public abstract void ripe();

public abstract void eat();
}

public class Apple extends Fruit {
@Override
public void ripe() {
// Implementation for ripening apples
}

@Override
public void eat() {
// Implementation for eating apples
}
}

Abstraction is like savouring the juicy essence of fruits without worrying about the nitty-gritty details. In our fruit basket program, abstraction involves simplifying complex systems by providing a high-level view. For example, we can have abstract methods in our Fruit class like “ripe” and “eat,” which provide a general idea of what those actions entail, but leave the specific implementation details to the child classes. Abstraction allows us to focus on essential behaviours and hide unnecessary complexities.

Congratulations! You’ve just experienced the fruitful journey of understanding object-oriented concepts through our fruit basket program. We explored the concepts of class, object, inheritance, encapsulation, and abstraction, all while indulging in the world of fruits. Remember, classes, serve as blueprints, objects represent tangible instances, inheritance shares traits, encapsulation protects our data, and abstraction allows us to focus on the essentials. Now, armed with these concepts, you’re ready to embark on your coding adventures and create flavorful applications. Happy coding.

Don’t forget to connect with me: lakinduw.me

--

--

Lakindu Widuranga Alwis
Lakindu Widuranga Alwis

Written by Lakindu Widuranga Alwis

Self-taught Full-Stack Developer, AI Researcher and Business Strategist turned Entrepreneur. CEO of Akwid Labs.