0% found this document useful (0 votes)
13 views24 pages

657011d6e42d8 PPT

Uploaded by

gufranah15
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views24 pages

657011d6e42d8 PPT

Uploaded by

gufranah15
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 24

Object Oriented Programming

(ACSD01)

Mr. Sriikanth S
Assistant Professor
EEE
Function Overloading

Overloading: Function overloading, constructor overloading, operator overloading - rules


for overloading operators, overloading unary and binary operators, friend functions.
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

public class OverloadingExample {

// Method with two int parameters


public int add(int a, int b) {
return a + b;
}

// Method with three int parameters


public int add(int a, int b, int c) {
return a + b + c;
}

// Method with two double parameters


public double add(double a, double b) {
return a + b;
}
Function Overloading

public static void main(String[] args) {


OverloadingExample example = new OverloadingExample();

// Call the first method


int sum1 = example.add(1, 2);
System.out.println("Sum 1: " + sum1);

// Call the second method


int sum2 = example.add(1, 2, 3);
System.out.println("Sum 2: " + sum2);

// Call the third method


double sum3 = example.add(1.5, 2.5);
System.out.println("Sum 3: " + sum3);
}
}
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

providing various options for initializing their state.

 Here's an example of constructor overloading in Java:


Constructor Overloading

 In this example, the class MyClass has three constructors:


 The first constructor takes no parameters and initializes myInt to 0 and myString to
"Default".
 The second constructor takes an integer parameter and initializes myInt with the
provided value, setting myString to "Parameterized".
 The third constructor takes both an integer and a string parameter and initializes myInt
and myString accordingly.
 In the main method, three objects (obj1, obj2, and obj3) are created using different
constructors, demonstrating constructor overloading.
Operator Overloading

 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

public class MyNumber {


private int value;

public MyNumber(int value) {


this.value = value;
}
public MyNumber add(MyNumber other) {
int result = this.value + other.value;
return new MyNumber(result);
}

// 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

public int getValue() {


return value;
}

public static void main(String[] args) {


MyNumber num1 = new MyNumber(5);
MyNumber num2 = new MyNumber(10);

MyNumber sum1 = num1.add(num2);


System.out.println("Sum 1: " + sum1.getValue()); // Output: 15

MyNumber sum2 = num1.add(7);


System.out.println("Sum 2: " + sum2.getValue()); // Output: 12
}
}
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

Java doesn't allow to create custom operators or overload existing ones.


However, it can achieve a form of operator overloading through method overloading, as I mentioned in the previous
response. Here are some general rules and tips for simulating operator overloading in Java using method overloading:
Define Methods with the Same Name:
Create methods with the same name but different parameter lists to simulate different versions of the operator.
Match Parameter Types and Number:
Overloaded methods should have different parameter types or a different number of parameters. This allows the
compiler to differentiate between them.
Maintain Consistent Naming:
Choose method names that make sense in the context of the operation you are trying to simulate. For example, if
overloading for addition, you might have methods named add, sum, etc.
Return Type Considerations:
The return type of the overloaded methods can be the same or different. You might want to return a new object
representing the result of the operation.
Overloading unary and binary 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;
}

// Unary operator overloading


public MyNumber increment() {
return new MyNumber(this.value + 1);
}

public MyNumber negate() {


return new MyNumber(-this.value);
}
// Binary operator overloading
public MyNumber add(MyNumber other) {
return new MyNumber(this.value + other.value);
Overloading unary and binary operators

public static void main(String[] args) {


MyNumber num1 = new MyNumber(5);
MyNumber num2 = new MyNumber(10);

// Unary operator usage


MyNumber incremented = num1.increment();
System.out.println("Incremented: " + incremented.getValue()); // Output: 6

MyNumber negated = num2.negate();


System.out.println("Negated: " + negated.getValue()); // Output: -10
// Binary operator usage
MyNumber sum = num1.add(num2);
System.out.println("Sum: " + sum.getValue()); // Output: 15

MyNumber difference = num1.subtract(num2);


System.out.println("Difference: " + difference.getValue()); // Output: -5
}
}
Overloading unary and binary operators

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

public class MyNumber {


private int value;

public MyNumber(int value) {


this.value = value;
}

// Unary operator overloading


public MyNumber increment() {
return new MyNumber(this.value + 1);
}

public MyNumber negate() {


return new MyNumber(-this.value);
}
// Binary operator overloading
public MyNumber add(MyNumber other) {
return new MyNumber(this.value + other.value);
}
Overloading unary and binary operators

public MyNumber subtract(MyNumber other) {


return new MyNumber(this.value - other.value);
}

public int getValue() {


return value;
}

public static void main(String[] args) {


MyNumber num1 = new MyNumber(5);
MyNumber num2 = new MyNumber(10);

// Unary operator usage


MyNumber incremented = num1.increment();
System.out.println("Incremented: " + incremented.getValue()); // Output: 6

MyNumber negated = num2.negate();


System.out.println("Negated: " + negated.getValue()); // Output: -10
Overloading unary and binary operators

// Binary operator usage


MyNumber sum = num1.add(num2);
System.out.println("Sum: " + sum.getValue()); // Output: 15

MyNumber difference = num1.subtract(num2);


System.out.println("Difference: " + difference.getValue()); // Output: -5
}
}
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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy