Java Programming Day 11

HimanshiHimanshi
5 min read

🌟 Java Day 11 β€” Diving Deep into Object-Oriented Concepts

πŸ“Œ Introduction

Welcome to Day 11 of my Java journey! Today was all about digging into some fundamental but powerful OOP concepts that are at the heart of Java programming: super, .toString() method, and method overriding. These features allow developers to write more flexible, readable, and maintainable code. I also modified and practiced some example programs to strengthen my understanding, which you'll see in detail below with complete explanations and outputs.


🧠 Concepts Covered Today

  1. super keyword

  2. .toString() method

  3. Method Overriding

  4. Real-world examples

  5. Differences between inheritance and overriding

  6. Why overriding is important in polymorphism


πŸ’‘ Topic 1: super Keyword in Java

πŸ”Ž What is super?

  • The super keyword in Java refers to the immediate parent class of a class.

  • It is used to:

    1. Access parent class methods

    2. Access parent class constructors

    3. Access parent class fields

βœ… Modified Program: Using super in Java

javaCopyEdit// Parent class
class Person {
    String firstName;
    String lastName;

    Person(String firstName, String lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
    }

    void showName() {
        System.out.println("Name: " + firstName + " " + lastName);
    }
}

// Subclass
class Student extends Person {
    double cgpa;

    Student(String firstName, String lastName, double cgpa) {
        super(firstName, lastName);  // calling parent constructor
        this.cgpa = cgpa;
    }

    void display() {
        super.showName();  // calling parent method
        System.out.println("CGPA: " + cgpa);
    }
}

// Another subclass
class Employee extends Person {
    String department;

    Employee(String firstName, String lastName, String department) {
        super(firstName, lastName);
        this.department = department;
    }

    void display() {
        super.showName();
        System.out.println("Department: " + department);
    }
}

βœ… Main Class

javaCopyEditpublic class Main {
    public static void main(String[] args) {
        Student student = new Student("Harry", "Porter", 8.7);
        student.display();

        Employee employee = new Employee("Tom", "Riddle", "Finance");
        employee.display();
    }
}

πŸ” Explanation:

  • super(firstName, lastName) calls the constructor of the Person class.

  • super.showName() reuses the method in the parent class.


πŸ’‘ Topic 2: .toString() Method

πŸ”Ž What is .toString()?

  • .toString() is a method of the Object class, which is the superclass of all classes in Java.

  • It returns a string representation of the object.

  • If we don’t override it, it returns a hash code (like Car@3e25a5).

  • When overridden, we can provide meaningful object information.

βœ… Modified Program

javaCopyEditclass Car {
    String brand;
    String model;
    int year;
    String color;

    Car(String brand, String model, int year, String color) {
        this.brand = brand;
        this.model = model;
        this.year = year;
        this.color = color;
    }

    // Overriding toString
    @Override
    public String toString() {
        return "Car [Brand=" + brand + ", Model=" + model + ", Year=" + year + ", Color=" + color + "]";
    }
}

public class Main {
    public static void main(String[] args) {
        Car car1 = new Car("Ford", "Mustang", 2025, "Red");
        Car car2 = new Car("Chevrolet", "Corvette", 2026, "Blue");

        System.out.println("Car 1 details: " + car1);
        System.out.println("Car 2 details: " + car2);
    }
}

πŸ” Explanation:

  • We overrode .toString() to show clear information about the car.

  • Now, printing the object directly shows human-readable details.


πŸ’‘ Topic 3: Method Overriding

πŸ”Ž What is Method Overriding?

  • Occurs when a subclass provides a specific implementation of a method already defined in its superclass.

  • Must have the same method name, return type, and parameters.

  • Helps in runtime polymorphism.

βœ… Modified Program: Method Overriding Example

javaCopyEdit// Parent class
class Animal {
    void move() {
        System.out.println("This animal is running");
    }
}

// Subclasses
class Dog extends Animal {
    // Inherits move() from Animal
}

class Cat extends Animal {
    // Inherits move() from Animal
}

class Fish extends Animal {
    @Override
    void move() {
        System.out.println("This animal is swimming");
    }
}

// Main class
public class Main {
    public static void main(String[] args) {
        Dog dog = new Dog();
        Cat cat = new Cat();
        Fish fish = new Fish();

        System.out.println("Dog's movement:");
        dog.move();  // Output: running

        System.out.println("Cat's movement:");
        cat.move();  // Output: running

        System.out.println("Fish's movement:");
        fish.move();  // Output: swimming
    }
}

πŸ” Explanation:

  • Dog and Cat inherit the move() method from Animal.

  • Fish overrides the method to add its unique behavior.


πŸ“˜ Key Takeaways

ConceptSummary
superUsed to access methods/constructors of the parent class
.toString()Used to represent object data in human-readable form
Method OverridingSubclass changes the parent method’s behavior
InheritanceEnables code reusability
PolymorphismAchieved via method overriding (dynamic binding at runtime)

🧠 Quick Comparison: super vs this

KeywordRefers toUsed in
superImmediate parent classAccess parent methods, constructors
thisCurrent objectAccess current class fields and methods

βœ… Why These Concepts Matter

  • You need super when extending existing classes but still want to reuse the base class logic.

  • .toString() helps in logging, debugging, and displaying object data clearly.

  • Overriding is the foundation of polymorphism, which is essential for flexible and scalable code.


🎯 Conclusion

Java Day 11 was a milestone in understanding the core of OOP principles. Understanding super, .toString(), and method overriding helped me realize how Java makes it easier to manage complexity by allowing code reusability, customization, and polymorphism. Each of these features makes Java robust, maintainable, and powerful for real-world applications.

2
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