SlideShare a Scribd company logo
Constructor and Method in
Constructor
Constructor is a special member function whose main
operation is to allocate the required resources such as
memory and initializes the object of its class.
Property of Constructor
It has same name as class name.
It has no return type.
It is automatically requested for help when the object
is created.
It is declared in the public section.
It cannot be overridden.
Its default argument is zero.
Sub class constructor can call super class constructor.
Types of constructor
Default constructor
Simple constructor
Parameterized constructor
Default constructor
There is no constructor in a class, java provides default
constructor.
Its argument is zero.
Simple constructor
class student
{
int rollno;
String name;
// this is constructor of student class
student()
{
rollno=11;
name=”HARSH”;
}
}
class demo
{
public static void main(String args[])
{
student s1 = new student();
System.out.println(“Roll number is = ”+s1.rollno);
System.out.println(“Name is = ”+s1.name);
}
}
Output
Roll number is = 11
Name = HARSH
Parameterized constructor
class student
{
int rollno;
String name;
student(int rno, String nm)
{
rollno=rno;
name=nm;
}
}
class demo
{
public static void main(String args[])
{
student s1 = new student(11,"HARSH");
System.out.println("Roll number is = "+s1.rollno);
System.out.println("Name is = "+s1.name);
}
}
Output
Roll number is = 11
Name is = HARSH
Method
Method is small part of the program that has some meaning and it has
some task for the program.
Methods are used to access class data.
Types of Method
Simple Method
Parameterized Method
Method Returning Method
Simple method
class box
{
double width;
double height;
double depth;
// parameterized constructor of box
box(double w, double h, double d)
{
width=w;
height=h;
depth=d;
}
// simple method to calculate volume
void calvol()
{
System.out.println("Volume of a box is = "+(width*height*depth));
}
}
class demo
{
public static void main(String args[])
{
box b1 = new box(10,10,10);
// method is called
b1.calvol();
}
}
Output
Volume of box is = 1000
Parameterized Method
class box
{
double width;
double height;
double depth;
// Parameterized Method to set dimention
void setdim(double w, double h, double d)
{
width=w;
height=h;
depth=d;
}
// simple method to calculate volume
void calvol()
{
System.out.println("Volume of a box is = "+(width*height*depth));
}
}
class demo
{
public static void main(String args[])
{
box b1 = new box();
b1.setdim(10,10,10);
b1.calvol();
}
}
Output
Volume of box is = 1000
Method returning value
class box
{
double width;
double height;
double depth;
// parameterized constructor of box
box(double w, double h, double d)
{
width=w;
height=h;
depth=d;
}
// method returning a value to main()
double calvol()
{
return(width*height*depth);
}
}
class demo
{
public static void main(String args[])
{
box b1 = new box(10,10,10);
double vol = b1.calvol();
System.out.println("Volume of box is = "+vol);
}
}
Output
Volume of box is = 1000
Prepared by
Harsh Jani
jani99harsh@gmail.com
[CCET]

More Related Content

What's hot

constructor and destructor
constructor and destructorconstructor and destructor
constructor and destructor
VENNILAV6
 
Java OOP Programming language (Part 3) - Class and Object
Java OOP Programming language (Part 3) - Class and ObjectJava OOP Programming language (Part 3) - Class and Object
Java OOP Programming language (Part 3) - Class and Object
OUM SAOKOSAL
 
Constructor and Destructor
Constructor and DestructorConstructor and Destructor
Constructor and DestructorKamal Acharya
 
C++ Constructor destructor
C++ Constructor destructorC++ Constructor destructor
C++ Constructor destructor
Da Mystic Sadi
 
Classes and objects1
Classes and objects1Classes and objects1
Classes and objects1
Vineeta Garg
 
Constructor & Destructor
Constructor & DestructorConstructor & Destructor
Constructor & Destructor
KV(AFS) Utarlai, Barmer (Rajasthan)
 
Constructor in java
Constructor in javaConstructor in java
Constructor in java
Pavith Gunasekara
 
C++ classes tutorials
C++ classes tutorialsC++ classes tutorials
C++ classes tutorialsFALLEE31188
 
คลาสและการเขียนโปรแกรมเชิงวัตถุเบื้องต้น
คลาสและการเขียนโปรแกรมเชิงวัตถุเบื้องต้นคลาสและการเขียนโปรแกรมเชิงวัตถุเบื้องต้น
คลาสและการเขียนโปรแกรมเชิงวัตถุเบื้องต้นFinian Nian
 
Functions, classes & objects in c++
Functions, classes & objects in c++Functions, classes & objects in c++
Functions, classes & objects in c++
ThamizhselviKrishnam
 
C++ classes tutorials
C++ classes tutorialsC++ classes tutorials
C++ classes tutorials
Mayank Jain
 
Constructor and destructor
Constructor  and  destructor Constructor  and  destructor
Constructor and destructor
Shubham Vishwambhar
 
Lecture 4.2 c++(comlete reference book)
Lecture 4.2 c++(comlete reference book)Lecture 4.2 c++(comlete reference book)
Lecture 4.2 c++(comlete reference book)
Abu Saleh
 
Constructor overloading & method overloading
Constructor overloading & method overloadingConstructor overloading & method overloading
Constructor overloading & method overloading
garishma bhatia
 
Java Foundations: Objects and Classes
Java Foundations: Objects and ClassesJava Foundations: Objects and Classes
Java Foundations: Objects and Classes
Svetlin Nakov
 
Constructors and Destructors
Constructors and DestructorsConstructors and Destructors
Constructors and Destructors
Dr Sukhpal Singh Gill
 

What's hot (20)

constructor and destructor
constructor and destructorconstructor and destructor
constructor and destructor
 
Java OOP Programming language (Part 3) - Class and Object
Java OOP Programming language (Part 3) - Class and ObjectJava OOP Programming language (Part 3) - Class and Object
Java OOP Programming language (Part 3) - Class and Object
 
Constructor and Destructor
Constructor and DestructorConstructor and Destructor
Constructor and Destructor
 
C++ Constructor destructor
C++ Constructor destructorC++ Constructor destructor
C++ Constructor destructor
 
Op ps
Op psOp ps
Op ps
 
Classes and objects1
Classes and objects1Classes and objects1
Classes and objects1
 
Constructors & destructors
Constructors & destructorsConstructors & destructors
Constructors & destructors
 
Constructor & Destructor
Constructor & DestructorConstructor & Destructor
Constructor & Destructor
 
Constructor in java
Constructor in javaConstructor in java
Constructor in java
 
C++ classes tutorials
C++ classes tutorialsC++ classes tutorials
C++ classes tutorials
 
คลาสและการเขียนโปรแกรมเชิงวัตถุเบื้องต้น
คลาสและการเขียนโปรแกรมเชิงวัตถุเบื้องต้นคลาสและการเขียนโปรแกรมเชิงวัตถุเบื้องต้น
คลาสและการเขียนโปรแกรมเชิงวัตถุเบื้องต้น
 
Functions, classes & objects in c++
Functions, classes & objects in c++Functions, classes & objects in c++
Functions, classes & objects in c++
 
Constructor
ConstructorConstructor
Constructor
 
C++ classes tutorials
C++ classes tutorialsC++ classes tutorials
C++ classes tutorials
 
Constructor and destructor
Constructor  and  destructor Constructor  and  destructor
Constructor and destructor
 
Lecture 4.2 c++(comlete reference book)
Lecture 4.2 c++(comlete reference book)Lecture 4.2 c++(comlete reference book)
Lecture 4.2 c++(comlete reference book)
 
Constructor overloading & method overloading
Constructor overloading & method overloadingConstructor overloading & method overloading
Constructor overloading & method overloading
 
Constructor
ConstructorConstructor
Constructor
 
Java Foundations: Objects and Classes
Java Foundations: Objects and ClassesJava Foundations: Objects and Classes
Java Foundations: Objects and Classes
 
Constructors and Destructors
Constructors and DestructorsConstructors and Destructors
Constructors and Destructors
 

Viewers also liked

Contributer Personality Development
Contributer Personality DevelopmentContributer Personality Development
Contributer Personality Development
Jani Harsh
 
Koshina-Polina-et ismail
Koshina-Polina-et ismailKoshina-Polina-et ismail
Koshina-Polina-et ismailIB Net Carbon
 
TRABAJOS EN LOS PASILLOS
TRABAJOS EN LOS PASILLOSTRABAJOS EN LOS PASILLOS
TRABAJOS EN LOS PASILLOS
Nuria Gil
 
Gender Terminology Definitions 2014
Gender Terminology Definitions 2014Gender Terminology Definitions 2014
Gender Terminology Definitions 2014Jessica E. Wilson
 
Keyword of java
Keyword of javaKeyword of java
Keyword of java
Jani Harsh
 
Keywords of java
Keywords of javaKeywords of java
Keywords of java
Jani Harsh
 

Viewers also liked (8)

TECertificate366697--Dickerson
TECertificate366697--DickersonTECertificate366697--Dickerson
TECertificate366697--Dickerson
 
Contributer Personality Development
Contributer Personality DevelopmentContributer Personality Development
Contributer Personality Development
 
Mahmoud Youssef
Mahmoud YoussefMahmoud Youssef
Mahmoud Youssef
 
Koshina-Polina-et ismail
Koshina-Polina-et ismailKoshina-Polina-et ismail
Koshina-Polina-et ismail
 
TRABAJOS EN LOS PASILLOS
TRABAJOS EN LOS PASILLOSTRABAJOS EN LOS PASILLOS
TRABAJOS EN LOS PASILLOS
 
Gender Terminology Definitions 2014
Gender Terminology Definitions 2014Gender Terminology Definitions 2014
Gender Terminology Definitions 2014
 
Keyword of java
Keyword of javaKeyword of java
Keyword of java
 
Keywords of java
Keywords of javaKeywords of java
Keywords of java
 

Similar to Constructor&method

OOP Lab Report.docx
OOP Lab Report.docxOOP Lab Report.docx
OOP Lab Report.docx
ArafatSahinAfridi
 
class object.pptx
class object.pptxclass object.pptx
class object.pptx
Killmekhilati
 
Inheritance.pptx
Inheritance.pptxInheritance.pptx
Inheritance.pptx
RutujaTandalwade
 
constructors.pptx
constructors.pptxconstructors.pptx
constructors.pptx
Epsiba1
 
Unit-3 Practice Programs-5.docx
Unit-3 Practice Programs-5.docxUnit-3 Practice Programs-5.docx
Unit-3 Practice Programs-5.docx
R.K.College of engg & Tech
 
OOPs & Inheritance Notes
OOPs & Inheritance NotesOOPs & Inheritance Notes
OOPs & Inheritance Notes
Shalabh Chaudhary
 
Hello. Im currently working on the last section to my assignment a.pdf
Hello. Im currently working on the last section to my assignment a.pdfHello. Im currently working on the last section to my assignment a.pdf
Hello. Im currently working on the last section to my assignment a.pdf
irshadkumar3
 
classes & objects.ppt
classes & objects.pptclasses & objects.ppt
classes & objects.ppt
BArulmozhi
 
OOPS Basics With Example
OOPS Basics With ExampleOOPS Basics With Example
OOPS Basics With Example
Thooyavan Venkatachalam
 
INHERITANCE, POINTERS, VIRTUAL FUNCTIONS, POLYMORPHISM.pptx
INHERITANCE, POINTERS, VIRTUAL FUNCTIONS, POLYMORPHISM.pptxINHERITANCE, POINTERS, VIRTUAL FUNCTIONS, POLYMORPHISM.pptx
INHERITANCE, POINTERS, VIRTUAL FUNCTIONS, POLYMORPHISM.pptx
DeepasCSE
 
Java tutorial for Beginners and Entry Level
Java tutorial for Beginners and Entry LevelJava tutorial for Beginners and Entry Level
Java tutorial for Beginners and Entry Level
Ramrao Desai
 
class c++
class c++class c++
class c++
vinay chauhan
 
OOPs & C++ UNIT 3
OOPs & C++ UNIT 3OOPs & C++ UNIT 3
OOPs & C++ UNIT 3
SURBHI SAROHA
 
Class and object in C++ By Pawan Thakur
Class and object in C++ By Pawan ThakurClass and object in C++ By Pawan Thakur
Class and object in C++ By Pawan Thakur
Govt. P.G. College Dharamshala
 
Java oops PPT
Java oops PPTJava oops PPT
Java oops PPT
kishu0005
 
Class and object
Class and objectClass and object
Class and object
prabhat kumar
 
Class introduction in java
Class introduction in javaClass introduction in java
Class introduction in java
yugandhar vadlamudi
 
Object Oriented Programming with Java
Object Oriented Programming with JavaObject Oriented Programming with Java
Object Oriented Programming with JavaJussi Pohjolainen
 

Similar to Constructor&method (20)

OOP Lab Report.docx
OOP Lab Report.docxOOP Lab Report.docx
OOP Lab Report.docx
 
class object.pptx
class object.pptxclass object.pptx
class object.pptx
 
Inheritance.pptx
Inheritance.pptxInheritance.pptx
Inheritance.pptx
 
constructors.pptx
constructors.pptxconstructors.pptx
constructors.pptx
 
Unit-3 Practice Programs-5.docx
Unit-3 Practice Programs-5.docxUnit-3 Practice Programs-5.docx
Unit-3 Practice Programs-5.docx
 
OOPs & Inheritance Notes
OOPs & Inheritance NotesOOPs & Inheritance Notes
OOPs & Inheritance Notes
 
Hello. Im currently working on the last section to my assignment a.pdf
Hello. Im currently working on the last section to my assignment a.pdfHello. Im currently working on the last section to my assignment a.pdf
Hello. Im currently working on the last section to my assignment a.pdf
 
classes & objects.ppt
classes & objects.pptclasses & objects.ppt
classes & objects.ppt
 
OOPS Basics With Example
OOPS Basics With ExampleOOPS Basics With Example
OOPS Basics With Example
 
INHERITANCE, POINTERS, VIRTUAL FUNCTIONS, POLYMORPHISM.pptx
INHERITANCE, POINTERS, VIRTUAL FUNCTIONS, POLYMORPHISM.pptxINHERITANCE, POINTERS, VIRTUAL FUNCTIONS, POLYMORPHISM.pptx
INHERITANCE, POINTERS, VIRTUAL FUNCTIONS, POLYMORPHISM.pptx
 
Java tutorial for Beginners and Entry Level
Java tutorial for Beginners and Entry LevelJava tutorial for Beginners and Entry Level
Java tutorial for Beginners and Entry Level
 
Inheritance
InheritanceInheritance
Inheritance
 
class c++
class c++class c++
class c++
 
OOPs & C++ UNIT 3
OOPs & C++ UNIT 3OOPs & C++ UNIT 3
OOPs & C++ UNIT 3
 
Class and object in C++ By Pawan Thakur
Class and object in C++ By Pawan ThakurClass and object in C++ By Pawan Thakur
Class and object in C++ By Pawan Thakur
 
Java oops PPT
Java oops PPTJava oops PPT
Java oops PPT
 
Class and object
Class and objectClass and object
Class and object
 
Class introduction in java
Class introduction in javaClass introduction in java
Class introduction in java
 
Java Basic day-2
Java Basic day-2Java Basic day-2
Java Basic day-2
 
Object Oriented Programming with Java
Object Oriented Programming with JavaObject Oriented Programming with Java
Object Oriented Programming with Java
 

Recently uploaded

Top Features to Include in Your Winzo Clone App for Business Growth (4).pptx
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptxTop Features to Include in Your Winzo Clone App for Business Growth (4).pptx
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptx
rickgrimesss22
 
Large Language Models and the End of Programming
Large Language Models and the End of ProgrammingLarge Language Models and the End of Programming
Large Language Models and the End of Programming
Matt Welsh
 
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoamOpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
takuyayamamoto1800
 
How to Position Your Globus Data Portal for Success Ten Good Practices
How to Position Your Globus Data Portal for Success Ten Good PracticesHow to Position Your Globus Data Portal for Success Ten Good Practices
How to Position Your Globus Data Portal for Success Ten Good Practices
Globus
 
top nidhi software solution freedownload
top nidhi software solution freedownloadtop nidhi software solution freedownload
top nidhi software solution freedownload
vrstrong314
 
Into the Box 2024 - Keynote Day 2 Slides.pdf
Into the Box 2024 - Keynote Day 2 Slides.pdfInto the Box 2024 - Keynote Day 2 Slides.pdf
Into the Box 2024 - Keynote Day 2 Slides.pdf
Ortus Solutions, Corp
 
GlobusWorld 2024 Opening Keynote session
GlobusWorld 2024 Opening Keynote sessionGlobusWorld 2024 Opening Keynote session
GlobusWorld 2024 Opening Keynote session
Globus
 
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing SuiteAI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
Google
 
Graphic Design Crash Course for beginners
Graphic Design Crash Course for beginnersGraphic Design Crash Course for beginners
Graphic Design Crash Course for beginners
e20449
 
May Marketo Masterclass, London MUG May 22 2024.pdf
May Marketo Masterclass, London MUG May 22 2024.pdfMay Marketo Masterclass, London MUG May 22 2024.pdf
May Marketo Masterclass, London MUG May 22 2024.pdf
Adele Miller
 
Corporate Management | Session 3 of 3 | Tendenci AMS
Corporate Management | Session 3 of 3 | Tendenci AMSCorporate Management | Session 3 of 3 | Tendenci AMS
Corporate Management | Session 3 of 3 | Tendenci AMS
Tendenci - The Open Source AMS (Association Management Software)
 
Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604
Fermin Galan
 
2024 RoOUG Security model for the cloud.pptx
2024 RoOUG Security model for the cloud.pptx2024 RoOUG Security model for the cloud.pptx
2024 RoOUG Security model for the cloud.pptx
Georgi Kodinov
 
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data AnalysisProviding Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
Globus
 
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
informapgpstrackings
 
Enhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdfEnhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdf
Globus
 
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, BetterWebinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
XfilesPro
 
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
Juraj Vysvader
 
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Mind IT Systems
 
Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus
 

Recently uploaded (20)

Top Features to Include in Your Winzo Clone App for Business Growth (4).pptx
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptxTop Features to Include in Your Winzo Clone App for Business Growth (4).pptx
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptx
 
Large Language Models and the End of Programming
Large Language Models and the End of ProgrammingLarge Language Models and the End of Programming
Large Language Models and the End of Programming
 
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoamOpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
 
How to Position Your Globus Data Portal for Success Ten Good Practices
How to Position Your Globus Data Portal for Success Ten Good PracticesHow to Position Your Globus Data Portal for Success Ten Good Practices
How to Position Your Globus Data Portal for Success Ten Good Practices
 
top nidhi software solution freedownload
top nidhi software solution freedownloadtop nidhi software solution freedownload
top nidhi software solution freedownload
 
Into the Box 2024 - Keynote Day 2 Slides.pdf
Into the Box 2024 - Keynote Day 2 Slides.pdfInto the Box 2024 - Keynote Day 2 Slides.pdf
Into the Box 2024 - Keynote Day 2 Slides.pdf
 
GlobusWorld 2024 Opening Keynote session
GlobusWorld 2024 Opening Keynote sessionGlobusWorld 2024 Opening Keynote session
GlobusWorld 2024 Opening Keynote session
 
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing SuiteAI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
 
Graphic Design Crash Course for beginners
Graphic Design Crash Course for beginnersGraphic Design Crash Course for beginners
Graphic Design Crash Course for beginners
 
May Marketo Masterclass, London MUG May 22 2024.pdf
May Marketo Masterclass, London MUG May 22 2024.pdfMay Marketo Masterclass, London MUG May 22 2024.pdf
May Marketo Masterclass, London MUG May 22 2024.pdf
 
Corporate Management | Session 3 of 3 | Tendenci AMS
Corporate Management | Session 3 of 3 | Tendenci AMSCorporate Management | Session 3 of 3 | Tendenci AMS
Corporate Management | Session 3 of 3 | Tendenci AMS
 
Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604
 
2024 RoOUG Security model for the cloud.pptx
2024 RoOUG Security model for the cloud.pptx2024 RoOUG Security model for the cloud.pptx
2024 RoOUG Security model for the cloud.pptx
 
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data AnalysisProviding Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
 
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
 
Enhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdfEnhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdf
 
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, BetterWebinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
 
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
 
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
 
Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024
 

Constructor&method

  • 2. Constructor Constructor is a special member function whose main operation is to allocate the required resources such as memory and initializes the object of its class. Property of Constructor It has same name as class name. It has no return type. It is automatically requested for help when the object is created. It is declared in the public section. It cannot be overridden. Its default argument is zero. Sub class constructor can call super class constructor.
  • 3. Types of constructor Default constructor Simple constructor Parameterized constructor Default constructor There is no constructor in a class, java provides default constructor. Its argument is zero.
  • 4. Simple constructor class student { int rollno; String name; // this is constructor of student class student() { rollno=11; name=”HARSH”; } }
  • 5. class demo { public static void main(String args[]) { student s1 = new student(); System.out.println(“Roll number is = ”+s1.rollno); System.out.println(“Name is = ”+s1.name); } } Output Roll number is = 11 Name = HARSH
  • 6. Parameterized constructor class student { int rollno; String name; student(int rno, String nm) { rollno=rno; name=nm; } }
  • 7. class demo { public static void main(String args[]) { student s1 = new student(11,"HARSH"); System.out.println("Roll number is = "+s1.rollno); System.out.println("Name is = "+s1.name); } } Output Roll number is = 11 Name is = HARSH
  • 8. Method Method is small part of the program that has some meaning and it has some task for the program. Methods are used to access class data. Types of Method Simple Method Parameterized Method Method Returning Method
  • 9. Simple method class box { double width; double height; double depth; // parameterized constructor of box box(double w, double h, double d) { width=w; height=h; depth=d; } // simple method to calculate volume void calvol() { System.out.println("Volume of a box is = "+(width*height*depth)); } }
  • 10. class demo { public static void main(String args[]) { box b1 = new box(10,10,10); // method is called b1.calvol(); } } Output Volume of box is = 1000
  • 11. Parameterized Method class box { double width; double height; double depth; // Parameterized Method to set dimention void setdim(double w, double h, double d) { width=w; height=h; depth=d; } // simple method to calculate volume void calvol() { System.out.println("Volume of a box is = "+(width*height*depth)); } }
  • 12. class demo { public static void main(String args[]) { box b1 = new box(); b1.setdim(10,10,10); b1.calvol(); } } Output Volume of box is = 1000
  • 13. Method returning value class box { double width; double height; double depth; // parameterized constructor of box box(double w, double h, double d) { width=w; height=h; depth=d; } // method returning a value to main() double calvol() { return(width*height*depth); } }
  • 14. class demo { public static void main(String args[]) { box b1 = new box(10,10,10); double vol = b1.calvol(); System.out.println("Volume of box is = "+vol); } } Output Volume of box is = 1000