This project is a single-file Java program designed to illustrate the core concepts of Object-Oriented Programming (OOP) using a relatable context: a Family structure consisting of humans and pets.
The code is contained entirely within a single source file, utilizing package-private top-level classes alongside a single public class Main which houses the application's entry point.
┌──────────────────────────────────────────────────────────────┐
│ ABSTRACT CLASS │
│ class Mammal │
├──────────────────────────────────────────────────────────────┤
│ ⚙️ PROPERTY: boolean breathesAir = true │
│ 🛠️ ABSTRACTION: abstract void makeNoise() │
└──────────────────────────────┬───────────────────────────────┘
│
┌──────────────┴──────────────┐
│ INHERITS FROM (extends) │
▼ ▼
┌──────────────────────────────┐ ┌───────────────────────┐
│ CHILD CLASS │ │ CHILD CLASS │
│ class Human │ │ class Dog │
├──────────────────────────────┤ ├───────────────────────┤
│ 🛡️ ENCAPSULATION: │ │ │
│ - private int age │ │ │
│ - private String name │ │ │
│ │ │ │
│ 🔓 ACCESS CHANNELS: │ │ │
│ + public String getName() │ │ │
│ + public void setAge() │ │ │
├──────────────────────────────┤ ├───────────────────────┤
│ 🎭 POLYMORPHISM: │ │ 🎭 POLYMORPHISM: │
│ void makeNoise() { │ │ void makeNoise() { │
│ print("Hello!"); │ │ print("Woof!"); │
│ } │ │ } │
└──────────────┬───────────────┘ └───────────┬───────────┘
│ │
│ INSTANTIATED OBJECTS │
▼ (via 'new' keyword) ▼
┌─────────────────┐ ┌─────────────────┐
│ OBJECT │ │ OBJECT │
│ alex (Human) │ │ (Dog) │
└────────┬────────┘ └────────┬────────┘
│ │
└─────────────────┬─────────────────┘
|
Collected into
|
▼
┌──────────────────────────────────────────────────────────────┐
│ Mammal[] family │
│ [ Index 0: alex ] | [ Index 1: Dog ] │
├──────────────────────────────────────────────────────────────┤
│ 🔄 ENHANCED FOR-EACH LOOP: │
│ for (Mammal member : family) -> member.makeNoise(); │
└──────────────────────────────────────────────────────────────┘
Definition: Hiding internal implementation complexities and showing only essential features.
- In this Code: The
Mammalclass is declared asabstract. You cannot directly create a generic mammal (new Mammal()). It establishes a conceptual blueprint that forces child classes to implement themakeNoise()action, without defining how they should make that sound.
Definition: Allowing a child class to inherit fields and behaviors from a parent class to eliminate redundant code.
- In this Code: Both
HumanandDoguse theextendskeyword (class Human extends Mammal). Consequently, they automatically inherit the parent propertybreathesAir = truewithout having to define it themselves.
Definition: Restricting direct access to an object's internal data fields and exposing them safely through public methods.
- In this Code: In the
Humanclass, variables likeageandnameare marked asprivate. They cannot be altered directly from outside. Instead, external classes must use public Getter and Setter methods (setAge(int newAge)). This allows the class to execute safety checks (e.g., preventing a negative age).
Definition: The ability of different object types to respond uniquely to the same method invocation ("many forms").
- In this Code: An array of the parent type (
Mammal[] family) holds distinct object types—aHumanand aDog. When looping through the array using an Enhanced For-Each Loop, callingmember.makeNoise()dynamically executes the human's greeting or the dog's bark depending on the underlying object type.
Before running this program, ensure you have the Java Development Kit (JDK) installed on your machine. You can verify this by running java -version in your terminal.
You can run this application either instantly through a modern IDE or manually using the command-line interface.
- Open the project root directory in IntelliJ IDEA, Eclipse, or VS Code.
- Navigate to
src/main/java/com/qa/automation/Main.java. - Click the green Run arrow next to the
mainmethod or pressShift + F10(IntelliJ default).
Because this application uses a packaged namespace (package com.qa.automation;), Java requires execution from the root source directory to correctly resolve the package structure at runtime.
-
Compile the Class: Navigate to the source folder and compile the code:
cd src/main/java/com/qa/automation javac Main.java -
Reset to Root Source Directory: Move back up the directory tree to the base
javafolder:cd ../../../../Your terminal path should now end precisely in
...\src\main\java>. -
Execute via Package Pointer: Launch the program using its fully qualified name (do not append
.class):java com.qa.automation.Main