SlideShare a Scribd company logo
11
Most read
14
Most read
15
Most read
Method overloading, recursion , passing
and returning objects from method, new
operator
Prepared By : -
Jainam Kapadiya
Contents of the Presentation
 Method and Method overloading
 How to overload method in JAVA?
 Method overloading Examples of program.
New operator
Recursion
 Passing and returning objects from methods.
Method overloading, recursion, passing and returning objects from method, new operator
Method overloading, recursion, passing and returning objects from method, new operator
Method overloading, recursion, passing and returning objects from method, new operator
Method overloading, recursion, passing and returning objects from method, new operator
Method overloading, recursion, passing and returning objects from method, new operator
 The new operator allocates memory dynamically for an
object. Here is the general form to use new operator in Java.
New Operator
class-var = new classname();
 Here, class-var is the variable of the class type being
created. The classname is the name of the class that is being
instantiated.
 The class name followed by the parentheses determines
the constructor for the class. A constructor defines what
occurs whenever an object of a class is created.
 Constructors are an crucial part of all the classes and have
many significant attributes.
Method overloading, recursion, passing and returning objects from method, new operator
public class Recursion
{
static int factorial(int n)
{
if (n == 1)
return 1;
else
return(n * factorial(n-1));
}
public static void main(String[] args)
{
System.out.println("Factorial of 5 is: "+factorial(5));
}
}
O/P:-
Factorial of 5 is: 120
RECURSION PROGRAM
Method overloading, recursion, passing and returning objects from method, new operator
Example:-
// Java program to demonstrate objects
// passing to methods.
class ObjectPassDemo
{
int a, b;
ObjectPassDemo(int i, int j)
{
a = i;
b = j;
}
// return true if o is equal to the invoking
// object notice an object is passed as an
// argument to method
boolean equalTo(ObjectPassDemo o)
{
return (o.a == a && o.b == b);
}
}
// Driver class
public class Test
{
public static void main(String args[])
{
ObjectPassDemo ob1 = new ObjectPassDemo(100, 22);
ObjectPassDemo ob2 = new ObjectPassDemo(100, 22);
ObjectPassDemo ob3 = new ObjectPassDemo(-1, -1);
System.out.println("ob1 == ob2: " + ob1.equalTo(ob2));
System.out.println("ob1 == ob3: " + ob1.equalTo(ob3));
}
}
Output:
ob1 == ob2: true
ob1 == ob3: false
Method overloading, recursion, passing and returning objects from method, new operator
// This method returns an object
Test incrByTen()
{
Test temp =
new Test(a+10);
return temp;
}
}
// Driver class
class returningobject
{
public static void main(String args[])
{
Test ob1 = new Test(2);
Test ob2;
ob2 = ob1.incrByTen();
System.out.println("ob1.a: " + ob1.a);
System.out.println("ob2.a: " + ob2.a);
}
}
Output:
ob1.a: 2 ob2.a: 12

More Related Content

What's hot (20)

PPTX
Java Data Types
Spotle.ai
 
PDF
Method Overloading In Java
CharthaGaglani
 
PPTX
Applets in java
Wani Zahoor
 
PPTX
Methods in java
chauhankapil
 
PPTX
Arrays in java
Arzath Areeff
 
PPTX
Interface in java
PhD Research Scholar
 
PPTX
C# classes objects
Dr.Neeraj Kumar Pandey
 
PDF
Php array
Nikul Shah
 
PPTX
Data structures and algorithms
Julie Iskander
 
PPTX
Lecture_7-Encapsulation in Java.pptx
ShahinAhmed49
 
PPTX
queue & its applications
somendra kumar
 
PPT
Class and object in C++
rprajat007
 
PPT
Files Vs DataBase
Dr. C.V. Suresh Babu
 
PPTX
class and objects
Payel Guria
 
PPTX
Vectors in Java
Abhilash Nair
 
PPTX
Inheritance in java
RahulAnanda1
 
PPT
Introduction to method overloading & method overriding in java hdm
Harshal Misalkar
 
PPTX
I/O Streams
Ravi Chythanya
 
PPTX
Chapter 07 inheritance
Praveen M Jigajinni
 
Java Data Types
Spotle.ai
 
Method Overloading In Java
CharthaGaglani
 
Applets in java
Wani Zahoor
 
Methods in java
chauhankapil
 
Arrays in java
Arzath Areeff
 
Interface in java
PhD Research Scholar
 
C# classes objects
Dr.Neeraj Kumar Pandey
 
Php array
Nikul Shah
 
Data structures and algorithms
Julie Iskander
 
Lecture_7-Encapsulation in Java.pptx
ShahinAhmed49
 
queue & its applications
somendra kumar
 
Class and object in C++
rprajat007
 
Files Vs DataBase
Dr. C.V. Suresh Babu
 
class and objects
Payel Guria
 
Vectors in Java
Abhilash Nair
 
Inheritance in java
RahulAnanda1
 
Introduction to method overloading & method overriding in java hdm
Harshal Misalkar
 
I/O Streams
Ravi Chythanya
 
Chapter 07 inheritance
Praveen M Jigajinni
 

Similar to Method overloading, recursion, passing and returning objects from method, new operator (20)

PPTX
Inheritance
Mavoori Soshmitha
 
PPTX
nested_Object as Parameter & Recursion_Later_commamd.pptx
Kongu Engineering College, Perundurai, Erode
 
PPTX
MODULE_3_Methods and Classes Overloading.pptx
VeerannaKotagi1
 
PPT
FUNDAMETAL ALG.ppt
Menaka Sivakumar
 
PDF
Lezione03
robynho86
 
PDF
Lezione03
robynho86
 
PPT
Cis068 08
FALLEE31188
 
PPTX
Core Java and Data Structure using C++.pptx
rawatsatish0327
 
PPT
Recursion.ppt
TalhaHussain58
 
PPTX
Chap2 class,objects contd
raksharao
 
PDF
Week11
hccit
 
PPTX
Data structure and algorithm presentation.pptx
sarimshakeel2003
 
PPTX
Internet and Web Technology (CLASS-16) [Basic Elements of Java Program] | NIC...
Ayes Chinmay
 
PDF
Methods in Java
Jussi Pohjolainen
 
PDF
Java methods or Subroutines or Functions
Kuppusamy P
 
PPTX
U2 JAVA.pptx
madan r
 
DOCX
Wap to implement bitwise operators
Harleen Sodhi
 
PPT
Cso gaddis java_chapter15
mlrbrown
 
PPT
3-Recursion.ppt
TrnHuy921814
 
Inheritance
Mavoori Soshmitha
 
nested_Object as Parameter & Recursion_Later_commamd.pptx
Kongu Engineering College, Perundurai, Erode
 
MODULE_3_Methods and Classes Overloading.pptx
VeerannaKotagi1
 
FUNDAMETAL ALG.ppt
Menaka Sivakumar
 
Lezione03
robynho86
 
Lezione03
robynho86
 
Cis068 08
FALLEE31188
 
Core Java and Data Structure using C++.pptx
rawatsatish0327
 
Recursion.ppt
TalhaHussain58
 
Chap2 class,objects contd
raksharao
 
Week11
hccit
 
Data structure and algorithm presentation.pptx
sarimshakeel2003
 
Internet and Web Technology (CLASS-16) [Basic Elements of Java Program] | NIC...
Ayes Chinmay
 
Methods in Java
Jussi Pohjolainen
 
Java methods or Subroutines or Functions
Kuppusamy P
 
U2 JAVA.pptx
madan r
 
Wap to implement bitwise operators
Harleen Sodhi
 
Cso gaddis java_chapter15
mlrbrown
 
3-Recursion.ppt
TrnHuy921814
 
Ad

More from JAINAM KAPADIYA (8)

PDF
CLASSIFICATION OF DEBUGGERS
JAINAM KAPADIYA
 
PDF
Clementine tool
JAINAM KAPADIYA
 
PDF
Zed Attack Proxy (ZAP)
JAINAM KAPADIYA
 
PPTX
Software Engineering Layered Technology Software Process Framework
JAINAM KAPADIYA
 
PDF
Android Application Components
JAINAM KAPADIYA
 
PDF
Symmetric Cipher Model, Substitution techniques, Transposition techniques, St...
JAINAM KAPADIYA
 
PDF
Computer graphics practical(jainam)
JAINAM KAPADIYA
 
PPTX
Corruption in india
JAINAM KAPADIYA
 
CLASSIFICATION OF DEBUGGERS
JAINAM KAPADIYA
 
Clementine tool
JAINAM KAPADIYA
 
Zed Attack Proxy (ZAP)
JAINAM KAPADIYA
 
Software Engineering Layered Technology Software Process Framework
JAINAM KAPADIYA
 
Android Application Components
JAINAM KAPADIYA
 
Symmetric Cipher Model, Substitution techniques, Transposition techniques, St...
JAINAM KAPADIYA
 
Computer graphics practical(jainam)
JAINAM KAPADIYA
 
Corruption in india
JAINAM KAPADIYA
 
Ad

Recently uploaded (20)

PDF
CIFDAQ Market Wrap for the week of 4th July 2025
CIFDAQ
 
PPTX
MuleSoft MCP Support (Model Context Protocol) and Use Case Demo
shyamraj55
 
PPTX
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
PPTX
AUTOMATION AND ROBOTICS IN PHARMA INDUSTRY.pptx
sameeraaabegumm
 
PPTX
"Autonomy of LLM Agents: Current State and Future Prospects", Oles` Petriv
Fwdays
 
PDF
How do you fast track Agentic automation use cases discovery?
DianaGray10
 
PDF
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
PDF
LOOPS in C Programming Language - Technology
RishabhDwivedi43
 
PDF
Transcript: Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
PDF
Mastering Financial Management in Direct Selling
Epixel MLM Software
 
PDF
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
PDF
Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
PDF
Automating Feature Enrichment and Station Creation in Natural Gas Utility Net...
Safe Software
 
PPTX
Future Tech Innovations 2025 – A TechLists Insight
TechLists
 
PPTX
The Project Compass - GDG on Campus MSIT
dscmsitkol
 
PPTX
Mastering ODC + Okta Configuration - Chennai OSUG
HathiMaryA
 
PDF
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
PDF
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
PDF
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
PDF
CIFDAQ Market Insights for July 7th 2025
CIFDAQ
 
CIFDAQ Market Wrap for the week of 4th July 2025
CIFDAQ
 
MuleSoft MCP Support (Model Context Protocol) and Use Case Demo
shyamraj55
 
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
AUTOMATION AND ROBOTICS IN PHARMA INDUSTRY.pptx
sameeraaabegumm
 
"Autonomy of LLM Agents: Current State and Future Prospects", Oles` Petriv
Fwdays
 
How do you fast track Agentic automation use cases discovery?
DianaGray10
 
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
LOOPS in C Programming Language - Technology
RishabhDwivedi43
 
Transcript: Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
Mastering Financial Management in Direct Selling
Epixel MLM Software
 
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
Automating Feature Enrichment and Station Creation in Natural Gas Utility Net...
Safe Software
 
Future Tech Innovations 2025 – A TechLists Insight
TechLists
 
The Project Compass - GDG on Campus MSIT
dscmsitkol
 
Mastering ODC + Okta Configuration - Chennai OSUG
HathiMaryA
 
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
CIFDAQ Market Insights for July 7th 2025
CIFDAQ
 

Method overloading, recursion, passing and returning objects from method, new operator

  • 1. Method overloading, recursion , passing and returning objects from method, new operator Prepared By : - Jainam Kapadiya
  • 2. Contents of the Presentation  Method and Method overloading  How to overload method in JAVA?  Method overloading Examples of program. New operator Recursion  Passing and returning objects from methods.
  • 8.  The new operator allocates memory dynamically for an object. Here is the general form to use new operator in Java. New Operator class-var = new classname();  Here, class-var is the variable of the class type being created. The classname is the name of the class that is being instantiated.  The class name followed by the parentheses determines the constructor for the class. A constructor defines what occurs whenever an object of a class is created.  Constructors are an crucial part of all the classes and have many significant attributes.
  • 10. public class Recursion { static int factorial(int n) { if (n == 1) return 1; else return(n * factorial(n-1)); } public static void main(String[] args) { System.out.println("Factorial of 5 is: "+factorial(5)); } } O/P:- Factorial of 5 is: 120 RECURSION PROGRAM
  • 12. Example:- // Java program to demonstrate objects // passing to methods. class ObjectPassDemo { int a, b; ObjectPassDemo(int i, int j) { a = i; b = j; } // return true if o is equal to the invoking // object notice an object is passed as an // argument to method boolean equalTo(ObjectPassDemo o) { return (o.a == a && o.b == b); } }
  • 13. // Driver class public class Test { public static void main(String args[]) { ObjectPassDemo ob1 = new ObjectPassDemo(100, 22); ObjectPassDemo ob2 = new ObjectPassDemo(100, 22); ObjectPassDemo ob3 = new ObjectPassDemo(-1, -1); System.out.println("ob1 == ob2: " + ob1.equalTo(ob2)); System.out.println("ob1 == ob3: " + ob1.equalTo(ob3)); } } Output: ob1 == ob2: true ob1 == ob3: false
  • 15. // This method returns an object Test incrByTen() { Test temp = new Test(a+10); return temp; } } // Driver class class returningobject { public static void main(String args[]) { Test ob1 = new Test(2); Test ob2; ob2 = ob1.incrByTen(); System.out.println("ob1.a: " + ob1.a); System.out.println("ob2.a: " + ob2.a); } } Output: ob1.a: 2 ob2.a: 12