657011d6e42d8 PPT
657011d6e42d8 PPT
(ACSD01)
Mr. Sriikanth S
Assistant Professor
EEE
Function Overloading
Function overloading in Java refers to the ability to define multiple methods with the
same name in a class but with different parameters.
The Java compiler uses the number and types of parameters to determine which
version of the method to call during a specific invocation.
Function overloading is a form of polymorphism that allows you to use the same
method name for different behaviors based on the input.
Here's an example of function overloading in Java:
Function Overloading
In this example, the OverloadingExample class has three methods named add, each with a different
number or type of parameters. When you call the add method, the Java compiler determines which
version of the method to invoke based on the arguments provided.
Function overloading simplifies the code and makes it more readable by allowing you to use the same
method name for logically similar operations. Just keep in mind that the method signature, which
includes the method name and the parameter types, must be different for each overloaded method.
Constructor Overloading
Constructor overloading in Java allows to define multiple constructors in a class, each with a different set
of parameters.
Like method overloading, constructor overloading enables you to create objects in different ways by
In Java, operator overloading, as it is known in some other programming languages like C++, is not
directly supported. Java does not allow you to define custom operators or overload existing ones in the
way you can in languages like C++. However, Java does support a form of operator overloading through
the use of method overloading.
In Java, you can create methods with the same name but different parameter lists. By doing this, you can
achieve behavior similar to operator overloading. Here's an example:
Function Overloading
// You can overload the add method to support different types or numbers of parameters
public MyNumber add(int num) {
int result = this.value + num;
return new MyNumber(result);
}
Function Overloading
In this example, the MyNumber class has an add method that can take either another MyNumber or an
int as a parameter. This simulates operator overloading for the addition operation.
Keep in mind that while this provides a way to achieve similar functionality, it is not the same as true
operator overloading. True operator overloading allows you to use operators like +, -, etc., directly with
objects, which is not possible in Java.
Rules for overloading operators
Remember that this is not true operator overloading but a way to achieve similar functionality using method
overloading. import java.util.Scanner;
public class MyNumber {
private int value;
public MyNumber(int value) {
this.value = value;
}
In Java we cannot directly overload unary or binary operators like you can in some other languages like C++.
However, you can simulate operator overloading by creating methods with specific names and using them in
a way that mimics the behavior of operators.
Here's an example to demonstrate how you might simulate unary and binary operator overloading in Java
using methods:
Overloading unary and binary operators
In this example, the methods increment and negate to simulate unary operators and methods add and
subtract to simulate binary operators. While this is not true operator overloading, it provides a way to
achieve similar functionality in Java through method overloading.
Friend Functions
In Java, the concept of friend functions, as seen in some other languages like C++, is not directly supported.
In Java, access control is enforced through modifiers such as public, private, and protected, and there is no direct
equivalent of a "friend" keyword to grant external functions access to private members of a class.
However, there are alternative approaches to achieve similar functionality in Java.
One common approach is to provide public methods in a class that allow controlled access to its private members.
Friend Functions
In this, getPrivateData and setPrivateData are public methods that allow controlled access to the private member
privateData.
While it's not exactly the same as a friend function, it follows the principles of encapsulation and controlled access to
class members.
In this example, getPrivateData and setPrivateData are public methods that allow controlled access to the private
member privateData.
While it's not exactly the same as a friend function, it follows the principles of encapsulation and controlled access to
class members.
Methods in Java
Friend Functions