Startertutorials Blog
Tutorials and articles related to programming, computer science, technology and others.
Subscribe to Startertutorials.com's YouTube channel for different tutorial and lecture videos.
Home » Programming » Java » Programs » Basic » Java program for demonstrating methods and method overloading
Suryateja Pericherla Categories: Basic. No Comments on Java program for demonstrating methods and method overloading
0
(0)

Aim

To write a program for demonstrating methods and method overloading.

 

Theory

One of the ways through which Java supports polymorphism is overloading. It can be defined as creating two or more methods in the same class sharing a common name but different number of parameters or different types of parameters.

 

You should remember that overloading doesn’t depend upon the return type of the method. Since method binding is resolved at compile-time based on the number of parameters or type of parameters, overloading is also called as compile-time polymorphism or static binding or early binding.

 

Creating two or more methods in the same class with same name but different number of parameters or different types of parameters is known as method overloading. Following code segment demonstrates method overloading.


Subscribe to our monthly newsletter. Get notified about latest articles, offers and contests.


class Addition
{
	void sum(int a, int b)
	{
		System.out.println("Sum of two integers is: "+(a+b));
	}
	void sum(float a, float b)
	{
		System.out.println("Sum of two floats is: "+(a+b));
	}
}

 

Pseudo code / Steps

  1. Create a class
  2. Create the data members, namely, name, age, and grade.
  3. Create a method named setDetails for setting or assign the values for the data members.
  4. Overload the setDetails
  5. Create a method named getDetails for displaying the values of data members.
  6. Create a main method for creating objects and calling the overloaded method.

 

Program

class Student
{
	String name;
	int age;
	char grade;
	
	void setDetails(String n)
	{
		name = n;
	}
	
	void setDetails(String n, int a)
	{
		name = n;
		age = a;
	}
	
	void setDetails(String n, int a, char g)
	{
		name = n;
		age = a;
		grade = g;
	}
	
	void getDetails()
	{
		System.out.println("Student name is " + name);
		System.out.println("Student age is " + age);
		System.out.println("Student grade is " + grade);
 	}
	
	public static void main(String args[])
	{
		Student s1 = new Student();
		s1.setDetails("Ram");
		s1.getDetails();
		s1.setDetails("Ram", 19);
		s1.getDetails();
		s1.setDetails("Ram", 19, 'A');
		s1.getDetails();
	}
}

 

Input and Output:

Student name is Ram

Student age is 0

Student grade is

Student name is Ram

Student age is 19

Student grade is

Student name is Ram

Student age is 19

Student grade is A

 

Result

The concept of method overloading was understood and demonstrated successfully.

How useful was this post?

Click on a star to rate it!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Leave a Reply

Your email address will not be published. Required fields are marked *

Facebook
Twitter
Pinterest
Youtube
Instagram
Blogarama - Blog Directory