Java Programming Day 10

HimanshiHimanshi
5 min read

🌟 Java Day 10 – Learning Log: Arrays of Objects, Static Methods, and Inheritance


πŸ“Œ Introduction

Day 10 was all about revising important foundational Java concepts: Arrays of Objects, Static Methods and Variables, and Inheritance. These concepts are pillars for building complex programs in Java by organizing data, sharing common behavior across objects, and promoting code reuse. Today’s practice deepened my understanding through real-world examples such as cars, friends, and living organisms.


🧠 Concept 1: Arrays of Objects

πŸ”Ž What is an Array of Objects?

  • Just like primitive arrays (int[], double[]), Java allows arrays of objects (Car[], Student[]).

  • This allows grouping multiple objects of the same type together for easy management.

  • Especially useful for collections like inventories, student lists, or any object grouping.

βœ… Program: Managing Cars with Array of Objects

javaCopyEdit// Car.java
public class Car {
    String name;
    String color;

    Car(String name, String color) {
        this.name = name;
        this.color = color;
    }

    void drive() {
        System.out.println(name + " with color " + color + " is driving.");
    }
}

// Main.java
public class Main {
    public static void main(String[] args) {
        // Creating an array of Car objects
        Car[] cars = {
            new Car("Mustang", "Red"),
            new Car("Corvette", "Blue"),
            new Car("Charger", "Yellow")
        };

        // Changing each car's color to black
        for (Car car : cars) {
            car.color = "Black";
        }

        // Printing updated car information
        for (Car car : cars) {
            car.drive();
        }
    }
}

πŸ“ Detailed Notes:

  • Car[] cars declares an array capable of holding Car objects.

  • Each element of the array references an instance of Car.

  • Modifying car.color inside the array updates the actual object’s field β€” showing the array holds references, not copies.

  • Enhanced for loops (for (Car car : cars)) make iteration concise and readable.

  • Arrays of objects facilitate bulk operations on grouped data and ease maintenance.


🧠 Concept 2: Static Methods and Variables

πŸ”Ž What is static in Java?

  • static members belong to the class, not to any specific instance (object).

  • Shared across all instances.

  • Useful for counters, utility methods, or constants.

βœ… Program: Counting Friends Using Static Variables

javaCopyEdit// Friend.java
public class Friend {
    static int numOfFriends;  // Shared by all Friend objects
    String name;

    Friend(String name) {
        this.name = name;
        numOfFriends++;  // Increment whenever a new Friend is created
    }

    static void showFriends() {
        System.out.println("You have " + numOfFriends + " total friends.");
    }
}

// Main.java
public class Main {
    public static void main(String[] args) {
        Friend friend1 = new Friend("Spongebob");
        Friend friend2 = new Friend("Patrick");
        Friend friend3 = new Friend("Sandy");
        Friend friend4 = new Friend("Plankton");

        System.out.println("Friend 1: " + friend1.name);
        System.out.println("Friend 2: " + friend2.name);
        System.out.println("Friend 3: " + friend3.name);
        System.out.println("Friend 4: " + friend4.name);

        // Accessing static method without object
        Friend.showFriends();
    }
}

πŸ“ Detailed Notes:

  • static int numOfFriends keeps a count common to all Friend objects.

  • Each time a new Friend is instantiated, the counter increments β€” reflecting total friends created.

  • static void showFriends() can be called via the class name directly without an object.

  • Demonstrates the shared state concept β€” all instances see the same static variable value.

  • Useful for managing global states, utility functions, or constant data shared across all instances.


🧠 Concept 3: Inheritance in Java

πŸ”Ž What is Inheritance?

  • Allows a new class (child) to inherit fields and methods from an existing class (parent).

  • Promotes code reuse and hierarchical relationships among classes.

  • Supports method overriding for specialized behavior.

βœ… Program: Multi-Level Inheritance Example

javaCopyEdit// Organism.java
public class Organism {
    boolean isAlive;

    Organism() {
        isAlive = true;  // Default all organisms as alive
    }
}

// Animal.java
public class Animal extends Organism {
    void eat() {
        System.out.println("The animal is eating.");
    }
}

// Dog.java
public class Dog extends Animal {
    int lives = 1;

    void speak() {
        System.out.println("The dog goes *bark*");
    }
}

// Cat.java
public class Cat extends Animal {
    int lives = 9;

    void speak() {
        System.out.println("The cat goes *meow*");
    }
}

// Plant.java
public class Plant extends Organism {
    void photosynthesis() {
        System.out.println("The plant absorbs sunlight.");
    }
}

// Main.java
public class Main {
    public static void main(String[] args) {
        Dog dog = new Dog();
        Cat cat = new Cat();
        Plant plant = new Plant();

        // Inherited field from Organism
        System.out.println("Dog alive? " + dog.isAlive);
        System.out.println("Cat alive? " + cat.isAlive);
        System.out.println("Plant alive? " + plant.isAlive);

        // Inherited methods from Animal
        dog.eat();
        cat.eat();

        // Unique method in Plant
        plant.photosynthesis();

        // Unique attributes and methods in Dog and Cat
        System.out.println("Dog lives: " + dog.lives);
        System.out.println("Cat lives: " + cat.lives);

        dog.speak();
        cat.speak();
    }
}

πŸ“ Detailed Notes:

  • Organism is the base class with a field isAlive.

  • Animal inherits from Organism, adding behavior like eat().

  • Dog and Cat inherit from Animal, adding unique attributes (lives) and methods (speak()).

  • Plant inherits directly from Organism, showing inheritance isn’t limited to animals.

  • Demonstrates multi-level inheritance and specialization.

  • Shows how shared properties/methods are passed down and extended.


🌟 Key Takeaways:

  • Arrays of Objects: Java arrays can hold references to objects. Using arrays helps manage collections of related objects and perform batch operations.

  • Static members: static variables and methods belong to the class, not individual objects. Shared state and utility functions are implemented using static members.

  • Inheritance: Enables subclasses to reuse code from parent classes and add their own specialized behavior. Multi-level inheritance supports complex hierarchies.

  • Use real-world analogies (cars, friends, animals) to understand these concepts intuitively.


βœ… Final Reflection – What I Learned on Java Day 10

Today was a productive revision day! I strengthened my grasp of arrays containing objects, how static variables maintain shared data across instances, and how inheritance forms the backbone of OOP in Java. Practical examples made the theory easy to follow, and now I feel more confident using these concepts in real-world projects.

Also, I began exploring Kaggle for machine learning β€” a platform perfect for datasets, competitions, and hands-on practice, aligning with my AI/ML goals.


πŸ’‘ Summary Table

ConceptDescriptionReal-world Example
Arrays of ObjectsArray holding references to objectsGarage of Cars
Static Methods/VariablesClass-level shared membersCounting total friends
InheritanceHierarchical code reuseOrganism β†’ Animal β†’ Dog/Cat

sign

1
Subscribe to my newsletter

Read articles from Himanshi directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Himanshi
Himanshi

Hi! I'm a curious and self-driven programmer currently pursuing my BCA πŸŽ“ and diving deep into the world of Java β˜• from Day 1. I already have a foundation in programming, and now I'm expanding my skills one concept, one blog, and one project at a time. I’m learning Java through Bro Code’s YouTube tutorials, experimenting with code, and documenting everything I understand β€” from basic syntax to real-world applications β€” to help others who are just starting out too. I believe in learning in public, progress over perfection, and growing with community support. You’ll find beginner-friendly Java breakdowns, hands-on code snippets, and insights from my daily coding grind right here. πŸ’‘ Interests: Java, Web Dev, Frontend Experiments, AI-curiosity, Writing & Sharing Knowledge πŸ› οΈ Tools: Java β€’ HTML/CSS β€’ JavaScript β€’ Python (basics) β€’ SQL 🎯 Goal: To become a confident Full Stack Developer & AI Explorer πŸ“ Based in India | Blogging my dev journey #JavaJourney #100DaysOfCode #CodeNewbie #LearnWithMe