Write a Java program to demonstrate method overloading with overloaded methods that use different parameter types, including custom objects and their properties
public class OverloadingWithCustomObjectsDemo { public static void main(String[] args) { MethodOverloadingDemo printer = new MethodOverloadingDemo(); Student student = new Student("Leo", 25); printer.printStudentInfo(student); printer.printStudentInfo("Bob"); } } class Student { private String name; private int age; public Student(String name, int age) { this.name = name; this.age = age; } public String getName() // Getters for name and age { return name; } public int getAge() { return age; } } class MethodOverloadingDemo { void printStudentInfo(Student student) // Method to print student's name and age { System.out.println("Student Info :"); System.out.println("Name : " + student.getName()); System.out.println("Age : " + student.getAge()); } void printStudentInfo(String name) // Method to print student's name only { System.out.println("Student Name : " + name); } }
Student Info : Name : Leo Age : 25 Student Name : Bob
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions