Java Programming Day 10

HimanshiHimanshi
5 min read

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


🧠 Concept 1: Array of Objects

In Java, just like we can create an array of primitive data types, we can also create an array to hold objects. This is especially useful when you want to manage a group of objects together β€” like a garage of cars, a list of students, etc.


βœ… Code: Car Object 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.");
    }
}
javaCopyEdit// 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")
        };

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

        // Printing updated car info using enhanced for loop
        for (Car car : cars) {
            car.drive();
        }
    }
}

πŸ“ Explanation:

  • Car[] cars creates an array to store multiple Car objects.

  • We use a for-each loop to loop through the array and modify or access properties.

  • This helps in managing a collection of objects more effectively.


🌟 Key Takeaways:

  • Arrays in Java can hold objects, not just primitive data.

  • Use enhanced for loop for easy access and operations.

  • Changes to object fields via array reference affect the actual object.


🧠 Concept 2: Static Methods and Variables

A static keyword is used in Java to define class-level variables or methods. These belong to the class itself and not to a specific object.


βœ… Code: Static Method Example (Friend Counter)

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

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

    static void showFriends() {
        System.out.println("You have " + numOfFriends + " total friends.");
    }
}
javaCopyEdit// Main.java
public class Main {
    public static void main(String[] args) {

        // Creating friend objects
        Friend friend1 = new Friend("Spongebob");
        Friend friend2 = new Friend("Patrick");
        Friend friend3 = new Friend("Sandy");
        Friend friend4 = new Friend("Plankton");

        // Accessing static variable and method
        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);

        // Showing total number of friends
        Friend.showFriends();
    }
}

πŸ“ Explanation:

  • static int numOfFriends: Tracks the number of friend objects created.

  • static void showFriends(): Accessed via class name, not object name.

  • Even though we create multiple Friend objects, they share the static variable.


🌟 Key Takeaways:

  • static variables are shared across all instances of the class.

  • static methods can be called without creating an object.

  • Ideal for counters, utility methods, or constants.


🧠 Concept 3: Inheritance in Java

Inheritance allows one class (child) to inherit the properties and methods of another class (parent). This promotes code reusability and a hierarchical structure.


βœ… Code: Multi-level Inheritance Example

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

    Organism() {
        isAlive = true;
    }
}
javaCopyEdit// Animal.java
public class Animal extends Organism {
    void eat() {
        System.out.println("The animal is eating.");
    }
}
javaCopyEdit// Dog.java
public class Dog extends Animal {
    int lives = 1;

    void speak() {
        System.out.println("The dog goes *bark*");
    }
}
javaCopyEdit// Cat.java
public class Cat extends Animal {
    int lives = 9;

    void speak() {
        System.out.println("The cat goes *meow*");
    }
}
javaCopyEdit// Plant.java
public class Plant extends Organism {
    void photosynthesis() {
        System.out.println("The plant absorbs sunlight.");
    }
}
javaCopyEdit// Main.java
public class Main {
    public static void main(String[] args) {

        Dog dog = new Dog();
        Cat cat = new Cat();
        Plant plant = new Plant();

        // Checking if all are alive (inherited from Organism)
        System.out.println("Dog alive? " + dog.isAlive);
        System.out.println("Cat alive? " + cat.isAlive);
        System.out.println("Plant alive? " + plant.isAlive);

        // Animal behavior
        dog.eat();
        cat.eat();

        // Unique behavior
        plant.photosynthesis();

        // Extra attributes
        System.out.println("Dog lives: " + dog.lives);
        System.out.println("Cat lives: " + cat.lives);

        // Sound behavior
        dog.speak();
        cat.speak();
    }
}

πŸ“ Explanation:

  • Organism is the base class.

  • Animal and Plant inherit from Organism.

  • Dog and Cat inherit from Animal.

  • Each subclass can have its own methods and variables.

  • Demonstrates multi-level inheritance and method overriding.


🌟 Key Takeaways:

  • Java supports single and multi-level inheritance.

  • Subclasses inherit all accessible members of the superclass.

  • Helps achieve clean code, reusability, and logical hierarchy.


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

✨ Today was a productive day! I revised object arrays, understood how static members behave across instances, and explored Java inheritance. The use of real-world examples like friends, cars, and animals made the learning intuitive. I also gained clarity about method access and object behavior within arrays.

I also started exploring Kaggle for MLβ€”a platform I discovered is great for:

  • Competing on datasets

  • Earning certificates

  • Learning ML from notebooks
    This aligns perfectly with my future plans to work in AI/ML.


πŸ’‘ Summary Table

ConceptDescriptionUse Case
Array of ObjectsArray that holds object instancesManaging multiple cars/students
Static MembersBelongs to class, not instanceUtility methods, counters
InheritanceClass inherits from another classCode reuse, hierarchy design
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