A leap year in Java is one of the best beginner programs to learn because it combines logic, conditional statements, and object-oriented programming in a single example. A leap year has 366 days instead of 365, and the rule looks simple at first, but many students get confused when years like 1700 or 1900 appear.
In this article, you will learn how to create a clean leap year program in Java using inheritance. The code is easy to understand, beginner-friendly, and useful for classroom assignments, viva questions, and practice exercises.
This tutorial explains not only how the program works, but also why the leap year rule works, how inheritance is used, common mistakes to avoid, and where this kind of logic can be used in real-world software.
Concept Explanation
A year is considered a leap year only when it follows these rules:
- The year must be divisible by 4.
- If the year is divisible by 100, it is not a leap year.
- But if the year is divisible by 400, it becomes a leap year again.
That means:
- 2020 is a leap year because it is divisible by 4 and not by 100.
- 1700 is not a leap year because it is divisible by 100 but not by 400.
- 2000 is a leap year because it is divisible by 400.
In this program, we use inheritance by creating a base class to store the year and a child class to check whether that year is a leap year or not. This is a simple way to understand how one class can reuse the properties of another class.
Program Code
Below is the complete Java program. It takes a year as input, uses inheritance, and prints whether the year is a leap year.
import java.util.Scanner;
class Year {
protected int year;
public Year(int year) {
this.year = year;
}
public int getYear() {
return year;
}
}
class LeapYear extends Year {
public LeapYear(int year) {
super(year);
}
public boolean isLeapYear() {
return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}
public void displayResult() {
if (isLeapYear()) {
System.out.println(year + " is a leap year.");
} else {
System.out.println(year + " is not a leap year.");
}
}
}
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a year: ");
int inputYear = sc.nextInt();
LeapYear leapYear = new LeapYear(inputYear);
leapYear.displayResult();
sc.close();
}
}
Sample Output
Here is an example output when the user enters 2024:
Enter a year: 2024
2024 is a leap year.
Another example:
Enter a year: 1700
1700 is not a leap year.
Step-by-Step Explanation
Let us break the program into small parts so that the logic becomes easier to remember.
- Step 1: The user enters a year using the Scanner class.
- Step 2: The year value is stored inside the
Yearclass. - Step 3: The
LeapYearclass extendsYear, so it inherits the year property. - Step 4: The
isLeapYear()method checks the leap year condition. - Step 5: The program prints the final result in a readable sentence.
Inheritance is useful here because the child class does not need to create the year variable again. It simply reuses the data from the parent class. This keeps the code organized and supports object-oriented design.
The leap year condition used in the program is:
year % 4 == 0→ divisible by 4year % 100 != 0→ not divisible by 100year % 400 == 0→ divisible by 400
If either of the first two conditions is true, or the year is divisible by 400, then the year is a leap year.
Time Complexity
The time complexity of this program is O(1) because only a fixed number of arithmetic checks are performed. No loops, recursion, or large data structures are involved.
The space complexity is also O(1) because the program uses only a few variables, and their memory usage does not grow with input size.
Real-World Applications
Leap year logic is used in many places where date calculations matter. Some common examples include:
- Calendar applications
- Scheduling systems
- Billing and subscription date calculations
- Age and duration calculators
- Academic and exam date systems
Even though this is a small program, it teaches an important idea: real software often depends on correct date handling. A small mistake in leap year logic can create problems in time-sensitive applications.
Common Mistakes
Many beginners make small errors while writing a leap year program. The most common mistakes are:
- Checking only divisibility by 4 and ignoring the 100 and 400 rules.
- Using incorrect logical operators.
- Forgetting that 1900 is not a leap year, even though it is divisible by 4.
- Hardcoding values instead of taking input from the user.
- Mixing up inheritance and logic in a way that makes the code harder to read.
Always remember that leap year calculation follows a specific standard rule, so the condition must be written carefully.
Best Practices
To make your Java program cleaner and more useful, follow these best practices:
- Use meaningful class names such as
YearandLeapYear. - Keep the leap year logic inside one method.
- Use
Scannerto make the program interactive. - Print clear output messages for better readability.
- Keep your code short but structured so it is easy to revise during exams.
If you want to expand this program later, you can add features like displaying the full calendar year, validating input, or integrating it into a date utility project.
Frequently Asked Questions
1. What is a leap year in Java?
A leap year is a year that has 366 days. In Java, we can check it using simple arithmetic conditions.
2. Why do we use inheritance in this program?
Inheritance helps the child class reuse the year value from the parent class, which keeps the program organized and reusable.
3. Is 2000 a leap year?
Yes. 2000 is a leap year because it is divisible by 400.
4. Is 1900 a leap year?
No. 1900 is divisible by 100 but not by 400, so it is not a leap year.
5. Can this program be made using if-else only?
Yes, but using inheritance makes it a better object-oriented programming example for Java learners.
Internal Linking Block
Here are some related Java articles from the same topic area that can help you learn more:
- Java Method Overriding with Simple Example
- Abstract Class in Java with Shape Example
- Multiple Inheritance Using Interfaces in Java
- Interface Inheritance in Java Explained
Conclusion
Creating a leap year program in Java is a great way to practice condition checking and inheritance together. The logic is simple, but it teaches an important concept that appears in many real-world date-related applications.
In this tutorial, you learned how to build a leap year program in Java using inheritance, how the rule works, what output to expect, and what mistakes to avoid. You can now use this code in your assignment, project, or practice notebook with confidence.
.png)
Comments
Post a Comment