Java Programming Day 10

π 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 holdingCar
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 fieldisAlive
.Animal
inherits fromOrganism
, adding behavior likeeat()
.Dog
andCat
inherit fromAnimal
, adding unique attributes (lives
) and methods (speak()
).Plant
inherits directly fromOrganism
, 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
Concept | Description | Real-world Example |
Arrays of Objects | Array holding references to objects | Garage of Cars |
Static Methods/Variables | Class-level shared members | Counting total friends |
Inheritance | Hierarchical code reuse | Organism β Animal β Dog/Cat |
sign |
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