SlideShare a Scribd company logo
Nested and Inner
classes , Object as
Parameter & Recursion
Nested classes
• A class declared inside a class is known as
nested class.
• a class within a class
• group classes in one place
• It can access all the members of outer class
including private members.
• It is used to develop more readable and
maintainable code
Syntax of Nested class
class Outer_class_Name{
...
class Nested_class_Name{
...
}
...
}
Outer_Class_name.Nested_class_Name
obj=oc.new Nested_class_Name();
class OuterClass {
int x = 10;
class InnerClass {
int y = 5;
}
}
public class MyMainClass {
public static void main(String[] args) {
OuterClass myOuter = new OuterClass();
OuterClass.InnerClass myInner = myOuter.new InnerClass();
System.out.println(myInner.y + myOuter.x);
}
}
Object as Parameter
• A method can take an objects as a parameter.
• pass-by-reference
class Add
{ int a; int b;
Add(int x,int y)// parametrized constructor
{ a=x;
b=y;
}
void sum(Add A1) // object 'A1' passed as parameter in function 'sum'
{ int sum1=A1.a+A1.b;
System.out.println("Sum of a and b :"+sum1);
}
}
public class classExAdd
{ public static void main(String arg[])
{
Add A=new Add(5,8);
/* Calls the parametrized constructor
with set of parameters*/
A.sum(A);
}
}
Output : Sum of a and b :13
class Test1
{
int a,b,c;
}
class Test
{
void get(Test1 obj,int a1, int b1 )
{ obj.a=a1;
obj.b=b1;
obj.c=obj.a+obj.b;
System.out.println(obj.c);
}
}
class Testmain
{
public static void main(String ss[])
{
Test1 tt1=new Test1();
Test tt=new Test();
tt.get(tt1,120,30);
}
}
Object as Parameter
passing arguments to methods
Pass by Value:
class Operation{
int data=50;
void change(int data){
data=data+100;//changes will be in the local variable only
}
public static void main(String args[]){
Operation op=new Operation();
System.out.println("before change "+op.data);
op.change(500);
System.out.println("after change "+op.data);
}
}
Output: before change 50
after change 50
Call by Value means calling a method with a
parameter as value
class Operation2{
int data=50;
void change(Operation2 op){
op.data=op.data+100;//changes will be in the instance variable
}
public static void main(String args[]){
Operation2 op=new Operation2();
System.out.println("before change "+op.data);
op.change(op);//passing object
System.out.println("after change "+op.data);
}
}
Output:
before change 50
after change 150
pass-by-reference
class Point {
public int x, y;
}
public class Tester {
public static void updatePoint(Point point) {
point.x = 100;
point.y = 100;
}
public static void main(String[] args) {
Point point = new Point();
System.out.println("X: " +point.x + ", Y: " + point.y);
updatePoint(point);
System.out.println("X: " +point.x + ", Y: " + point.y);
}
}
Output:
X: 0, Y: 0
X: 100, Y: 100
Returning Objects from Methods
class ObjectPassDemo
{ int a, b;
ObjectPassDemo(int i, int j)
{ a = i; b = j; }
boolean equalTo(ObjectPassDemo o)
{ return (o.a == a && o.b == b); }
}
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
A method can return any type of data,
including class types
• System.out.println("ob1 == ob2: " + ob1.equalTo(ob2));
• System.out.println("ob1 == ob3: " + ob1.equalTo(ob3));
Recursion
• Recursion is the technique of making a
function call itself.
Syntax:
returntype methodname()
{
//code to be executed
methodname(); //calling same method
}
nested_Object as Parameter  & Recursion_Later_commamd.pptx
Factorial of a Number Using Recursion
public class RecursionExample
{
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));
}
}
Output:
Factorial of 5 is: 120
Classification of methods
1)Based on nature of creation – a. user defined
b. predefined
2)Based on return type – a. return a value
b. Return void
3)Based on method call – a. nonrecursive
b. recursive
Command-Line Arguments
• It is an argument that is passed at the time of
running the java program
• The arguments passed from the console can
be received in the java program and it can be
used as an input.
class CommandLine {
public static void main(String args[]) {
for(int i=0; i<args.length; i++)
System.out.println("args[" + i + "]: " + args[i]);
}
}
• Try executing this program, as shown here:
java CommandLine this is a test 100 -1
Output
args[0]: this
args[1]: is
args[2]: a
args[3]: test
args[4]: 100
args[5]: -1

More Related Content

PPTX
UNIT_-II_2021R.pptx
RDeepa9
 
PPTX
Chap2 class,objects contd
raksharao
 
PDF
Method overloading, recursion, passing and returning objects from method, new...
JAINAM KAPADIYA
 
PPTX
MODULE_3_Methods and Classes Overloading.pptx
VeerannaKotagi1
 
PPTX
Inheritance
Mavoori Soshmitha
 
PDF
Lezione03
robynho86
 
PDF
Lezione03
robynho86
 
PPTX
Hemajava
SangeethaSasi1
 
UNIT_-II_2021R.pptx
RDeepa9
 
Chap2 class,objects contd
raksharao
 
Method overloading, recursion, passing and returning objects from method, new...
JAINAM KAPADIYA
 
MODULE_3_Methods and Classes Overloading.pptx
VeerannaKotagi1
 
Inheritance
Mavoori Soshmitha
 
Lezione03
robynho86
 
Lezione03
robynho86
 
Hemajava
SangeethaSasi1
 

Similar to nested_Object as Parameter & Recursion_Later_commamd.pptx (20)

PPTX
Classes, objects in JAVA
Abhilash Nair
 
PPTX
L03 Software Design
Ólafur Andri Ragnarsson
 
PDF
Second chapter-java
Ahmad sohail Kakar
 
PPTX
Classes, Objects and Method - Object Oriented Programming with Java
Radhika Talaviya
 
PPTX
javaimplementation
FaRaz Ahmad
 
PDF
CIS 1403 lab 3 functions and methods in Java
Hamad Odhabi
 
DOCX
Second chapter-java
Ahmad sohail Kakar
 
PPT
Chapter 4 - Classes in Java
Khirulnizam Abd Rahman
 
PPT
Eo gaddis java_chapter_06_5e
Gina Bullock
 
PPT
Eo gaddis java_chapter_06_Classes and Objects
Gina Bullock
 
PPT
Eo gaddis java_chapter_06_5e
Gina Bullock
 
PPTX
U2 JAVA.pptx
madan r
 
PPT
Class & Objects in JAVA.ppt
RohitPaul71
 
PPTX
Chapter 8 java
Ahmad sohail Kakar
 
PDF
CS3391 -OOP -UNIT – II NOTES FINAL.pdf
AALIM MUHAMMED SALEGH COLLEGE OF ENGINEERING
 
PPTX
Chap-2 Classes & Methods.pptx
chetanpatilcp783
 
PPT
Lesson16
cybernaut
 
PDF
Classes and objects
Kamal Acharya
 
PDF
C sharp chap5
Mukesh Tekwani
 
PPT
Java căn bản - Chapter7
Vince Vo
 
Classes, objects in JAVA
Abhilash Nair
 
L03 Software Design
Ólafur Andri Ragnarsson
 
Second chapter-java
Ahmad sohail Kakar
 
Classes, Objects and Method - Object Oriented Programming with Java
Radhika Talaviya
 
javaimplementation
FaRaz Ahmad
 
CIS 1403 lab 3 functions and methods in Java
Hamad Odhabi
 
Second chapter-java
Ahmad sohail Kakar
 
Chapter 4 - Classes in Java
Khirulnizam Abd Rahman
 
Eo gaddis java_chapter_06_5e
Gina Bullock
 
Eo gaddis java_chapter_06_Classes and Objects
Gina Bullock
 
Eo gaddis java_chapter_06_5e
Gina Bullock
 
U2 JAVA.pptx
madan r
 
Class & Objects in JAVA.ppt
RohitPaul71
 
Chapter 8 java
Ahmad sohail Kakar
 
CS3391 -OOP -UNIT – II NOTES FINAL.pdf
AALIM MUHAMMED SALEGH COLLEGE OF ENGINEERING
 
Chap-2 Classes & Methods.pptx
chetanpatilcp783
 
Lesson16
cybernaut
 
Classes and objects
Kamal Acharya
 
C sharp chap5
Mukesh Tekwani
 
Java căn bản - Chapter7
Vince Vo
 
Ad

More from Kongu Engineering College, Perundurai, Erode (20)

PPTX
Event Handling -_GET _ POSTimplementation.pptx
Kongu Engineering College, Perundurai, Erode
 
PPTX
Introduction to Generative AI refers to a subset of artificial intelligence
Kongu Engineering College, Perundurai, Erode
 
PPTX
Introduction to Microsoft Power BI is a business analytics service
Kongu Engineering College, Perundurai, Erode
 
PPTX
Connect to NoSQL Database (MongoDB) using Node JS & Connect Node.js with NoSQ...
Kongu Engineering College, Perundurai, Erode
 
PPTX
concept of server-side JavaScript / JS Framework: NODEJS
Kongu Engineering College, Perundurai, Erode
 
PPTX
Node.js web-based Example :Run a local server in order to start using node.js...
Kongu Engineering College, Perundurai, Erode
 
PPT
Concepts of Satellite Communication and types and its applications
Kongu Engineering College, Perundurai, Erode
 
PPT
Concepts of Mobile Communication Wireless LANs, Bluetooth , HiperLAN
Kongu Engineering College, Perundurai, Erode
 
PPTX
Web Technology Introduction framework.pptx
Kongu Engineering College, Perundurai, Erode
 
PPTX
Computer Network - Unicast Routing Distance vector Link state vector
Kongu Engineering College, Perundurai, Erode
 
PPT
Android SQLite database oriented application development
Kongu Engineering College, Perundurai, Erode
 
PPT
Android Application Development Programming
Kongu Engineering College, Perundurai, Erode
 
PPTX
Introduction to Spring & Spring BootFramework
Kongu Engineering College, Perundurai, Erode
 
PPTX
A REST API (also called a RESTful API or RESTful web API) is an application p...
Kongu Engineering College, Perundurai, Erode
 
PPTX
SOA and Monolith Architecture - Micro Services.pptx
Kongu Engineering College, Perundurai, Erode
 
PPTX
Connect to NoSQL Database using Node JS.pptx
Kongu Engineering College, Perundurai, Erode
 
PPTX
Bootstarp installation.pptx
Kongu Engineering College, Perundurai, Erode
 
Event Handling -_GET _ POSTimplementation.pptx
Kongu Engineering College, Perundurai, Erode
 
Introduction to Generative AI refers to a subset of artificial intelligence
Kongu Engineering College, Perundurai, Erode
 
Introduction to Microsoft Power BI is a business analytics service
Kongu Engineering College, Perundurai, Erode
 
Connect to NoSQL Database (MongoDB) using Node JS & Connect Node.js with NoSQ...
Kongu Engineering College, Perundurai, Erode
 
concept of server-side JavaScript / JS Framework: NODEJS
Kongu Engineering College, Perundurai, Erode
 
Node.js web-based Example :Run a local server in order to start using node.js...
Kongu Engineering College, Perundurai, Erode
 
Concepts of Satellite Communication and types and its applications
Kongu Engineering College, Perundurai, Erode
 
Concepts of Mobile Communication Wireless LANs, Bluetooth , HiperLAN
Kongu Engineering College, Perundurai, Erode
 
Web Technology Introduction framework.pptx
Kongu Engineering College, Perundurai, Erode
 
Computer Network - Unicast Routing Distance vector Link state vector
Kongu Engineering College, Perundurai, Erode
 
Android SQLite database oriented application development
Kongu Engineering College, Perundurai, Erode
 
Android Application Development Programming
Kongu Engineering College, Perundurai, Erode
 
Introduction to Spring & Spring BootFramework
Kongu Engineering College, Perundurai, Erode
 
A REST API (also called a RESTful API or RESTful web API) is an application p...
Kongu Engineering College, Perundurai, Erode
 
SOA and Monolith Architecture - Micro Services.pptx
Kongu Engineering College, Perundurai, Erode
 
Connect to NoSQL Database using Node JS.pptx
Kongu Engineering College, Perundurai, Erode
 
Ad

Recently uploaded (20)

PPTX
An introduction to Prepositions for beginners.pptx
drsiddhantnagine
 
PDF
1.Natural-Resources-and-Their-Use.ppt pdf /8th class social science Exploring...
Sandeep Swamy
 
PPTX
HISTORY COLLECTION FOR PSYCHIATRIC PATIENTS.pptx
PoojaSen20
 
PPTX
TEF & EA Bsc Nursing 5th sem.....BBBpptx
AneetaSharma15
 
PDF
2.Reshaping-Indias-Political-Map.ppt/pdf/8th class social science Exploring S...
Sandeep Swamy
 
PPTX
Dakar Framework Education For All- 2000(Act)
santoshmohalik1
 
DOCX
SAROCES Action-Plan FOR ARAL PROGRAM IN DEPED
Levenmartlacuna1
 
PDF
5.EXPLORING-FORCES-Detailed-Notes.pdf/8TH CLASS SCIENCE CURIOSITY
Sandeep Swamy
 
DOCX
UPPER GASTRO INTESTINAL DISORDER.docx
BANDITA PATRA
 
PPTX
IMMUNIZATION PROGRAMME pptx
AneetaSharma15
 
PPTX
Software Engineering BSC DS UNIT 1 .pptx
Dr. Pallawi Bulakh
 
PPTX
How to Manage Leads in Odoo 18 CRM - Odoo Slides
Celine George
 
PPTX
Information Texts_Infographic on Forgetting Curve.pptx
Tata Sevilla
 
PPTX
Five Point Someone – Chetan Bhagat | Book Summary & Analysis by Bhupesh Kushwaha
Bhupesh Kushwaha
 
PPTX
NOI Hackathon - Summer Edition - GreenThumber.pptx
MartinaBurlando1
 
PPTX
Understanding operators in c language.pptx
auteharshil95
 
PDF
Phylum Arthropoda: Characteristics and Classification, Entomology Lecture
Miraj Khan
 
PPTX
Tips Management in Odoo 18 POS - Odoo Slides
Celine George
 
PDF
Wings of Fire Book by Dr. A.P.J Abdul Kalam Full PDF
hetalvaishnav93
 
PPTX
Care of patients with elImination deviation.pptx
AneetaSharma15
 
An introduction to Prepositions for beginners.pptx
drsiddhantnagine
 
1.Natural-Resources-and-Their-Use.ppt pdf /8th class social science Exploring...
Sandeep Swamy
 
HISTORY COLLECTION FOR PSYCHIATRIC PATIENTS.pptx
PoojaSen20
 
TEF & EA Bsc Nursing 5th sem.....BBBpptx
AneetaSharma15
 
2.Reshaping-Indias-Political-Map.ppt/pdf/8th class social science Exploring S...
Sandeep Swamy
 
Dakar Framework Education For All- 2000(Act)
santoshmohalik1
 
SAROCES Action-Plan FOR ARAL PROGRAM IN DEPED
Levenmartlacuna1
 
5.EXPLORING-FORCES-Detailed-Notes.pdf/8TH CLASS SCIENCE CURIOSITY
Sandeep Swamy
 
UPPER GASTRO INTESTINAL DISORDER.docx
BANDITA PATRA
 
IMMUNIZATION PROGRAMME pptx
AneetaSharma15
 
Software Engineering BSC DS UNIT 1 .pptx
Dr. Pallawi Bulakh
 
How to Manage Leads in Odoo 18 CRM - Odoo Slides
Celine George
 
Information Texts_Infographic on Forgetting Curve.pptx
Tata Sevilla
 
Five Point Someone – Chetan Bhagat | Book Summary & Analysis by Bhupesh Kushwaha
Bhupesh Kushwaha
 
NOI Hackathon - Summer Edition - GreenThumber.pptx
MartinaBurlando1
 
Understanding operators in c language.pptx
auteharshil95
 
Phylum Arthropoda: Characteristics and Classification, Entomology Lecture
Miraj Khan
 
Tips Management in Odoo 18 POS - Odoo Slides
Celine George
 
Wings of Fire Book by Dr. A.P.J Abdul Kalam Full PDF
hetalvaishnav93
 
Care of patients with elImination deviation.pptx
AneetaSharma15
 

nested_Object as Parameter & Recursion_Later_commamd.pptx

  • 1. Nested and Inner classes , Object as Parameter & Recursion
  • 2. Nested classes • A class declared inside a class is known as nested class. • a class within a class • group classes in one place • It can access all the members of outer class including private members. • It is used to develop more readable and maintainable code
  • 3. Syntax of Nested class class Outer_class_Name{ ... class Nested_class_Name{ ... } ... } Outer_Class_name.Nested_class_Name obj=oc.new Nested_class_Name();
  • 4. class OuterClass { int x = 10; class InnerClass { int y = 5; } } public class MyMainClass { public static void main(String[] args) { OuterClass myOuter = new OuterClass(); OuterClass.InnerClass myInner = myOuter.new InnerClass(); System.out.println(myInner.y + myOuter.x); } }
  • 5. Object as Parameter • A method can take an objects as a parameter. • pass-by-reference
  • 6. class Add { int a; int b; Add(int x,int y)// parametrized constructor { a=x; b=y; } void sum(Add A1) // object 'A1' passed as parameter in function 'sum' { int sum1=A1.a+A1.b; System.out.println("Sum of a and b :"+sum1); } } public class classExAdd { public static void main(String arg[]) { Add A=new Add(5,8); /* Calls the parametrized constructor with set of parameters*/ A.sum(A); } } Output : Sum of a and b :13
  • 7. class Test1 { int a,b,c; } class Test { void get(Test1 obj,int a1, int b1 ) { obj.a=a1; obj.b=b1; obj.c=obj.a+obj.b; System.out.println(obj.c); } } class Testmain { public static void main(String ss[]) { Test1 tt1=new Test1(); Test tt=new Test(); tt.get(tt1,120,30); } } Object as Parameter
  • 8. passing arguments to methods Pass by Value: class Operation{ int data=50; void change(int data){ data=data+100;//changes will be in the local variable only } public static void main(String args[]){ Operation op=new Operation(); System.out.println("before change "+op.data); op.change(500); System.out.println("after change "+op.data); } } Output: before change 50 after change 50 Call by Value means calling a method with a parameter as value
  • 9. class Operation2{ int data=50; void change(Operation2 op){ op.data=op.data+100;//changes will be in the instance variable } public static void main(String args[]){ Operation2 op=new Operation2(); System.out.println("before change "+op.data); op.change(op);//passing object System.out.println("after change "+op.data); } } Output: before change 50 after change 150
  • 10. pass-by-reference class Point { public int x, y; } public class Tester { public static void updatePoint(Point point) { point.x = 100; point.y = 100; } public static void main(String[] args) { Point point = new Point(); System.out.println("X: " +point.x + ", Y: " + point.y); updatePoint(point); System.out.println("X: " +point.x + ", Y: " + point.y); } } Output: X: 0, Y: 0 X: 100, Y: 100
  • 11. Returning Objects from Methods class ObjectPassDemo { int a, b; ObjectPassDemo(int i, int j) { a = i; b = j; } boolean equalTo(ObjectPassDemo o) { return (o.a == a && o.b == b); } } 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 A method can return any type of data, including class types
  • 12. • System.out.println("ob1 == ob2: " + ob1.equalTo(ob2)); • System.out.println("ob1 == ob3: " + ob1.equalTo(ob3));
  • 13. Recursion • Recursion is the technique of making a function call itself. Syntax: returntype methodname() { //code to be executed methodname(); //calling same method }
  • 15. Factorial of a Number Using Recursion public class RecursionExample { 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)); } } Output: Factorial of 5 is: 120
  • 16. Classification of methods 1)Based on nature of creation – a. user defined b. predefined 2)Based on return type – a. return a value b. Return void 3)Based on method call – a. nonrecursive b. recursive
  • 17. Command-Line Arguments • It is an argument that is passed at the time of running the java program • The arguments passed from the console can be received in the java program and it can be used as an input.
  • 18. class CommandLine { public static void main(String args[]) { for(int i=0; i<args.length; i++) System.out.println("args[" + i + "]: " + args[i]); } } • Try executing this program, as shown here: java CommandLine this is a test 100 -1 Output args[0]: this args[1]: is args[2]: a args[3]: test args[4]: 100 args[5]: -1