Java Programming Day 11

π 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
super
keyword.toString()
methodMethod Overriding
Real-world examples
Differences between inheritance and overriding
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:
Access parent class methods
Access parent class constructors
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 thePerson
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 fromAnimal
.Fish overrides the method to add its unique behavior.
π Key Takeaways
Concept | Summary |
super | Used to access methods/constructors of the parent class |
.toString() | Used to represent object data in human-readable form |
Method Overriding | Subclass changes the parent methodβs behavior |
Inheritance | Enables code reusability |
Polymorphism | Achieved via method overriding (dynamic binding at runtime) |
π§ Quick Comparison: super
vs this
Keyword | Refers to | Used in |
super | Immediate parent class | Access parent methods, constructors |
this | Current object | Access 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.
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