Java Programming Day 10

π 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 multipleCar
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
andPlant
inherit fromOrganism
.Dog
andCat
inherit fromAnimal
.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
Concept | Description | Use Case |
Array of Objects | Array that holds object instances | Managing multiple cars/students |
Static Members | Belongs to class, not instance | Utility methods, counters |
Inheritance | Class inherits from another class | Code reuse, hierarchy design |
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