SlideShare a Scribd company logo
1 of 15
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 destructorVENNILAV6
 
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 ObjectOUM SAOKOSAL
 
Constructor and Destructor
Constructor and DestructorConstructor and Destructor
Constructor and DestructorKamal Acharya
 
C++ Constructor destructor
C++ Constructor destructorC++ Constructor destructor
C++ Constructor destructorDa Mystic Sadi
 
Classes and objects1
Classes and objects1Classes and objects1
Classes and objects1Vineeta Garg
 
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 tutorialsMayank Jain
 
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 overloadinggarishma bhatia
 
Java Foundations: Objects and Classes
Java Foundations: Objects and ClassesJava Foundations: Objects and Classes
Java Foundations: Objects and ClassesSvetlin Nakov
 

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 DevelopmentJani 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 PASILLOSNuria 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 javaJani Harsh
 
Keywords of java
Keywords of javaKeywords of java
Keywords of javaJani 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

constructors.pptx
constructors.pptxconstructors.pptx
constructors.pptxEpsiba1
 
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.pdfirshadkumar3
 
classes & objects.ppt
classes & objects.pptclasses & objects.ppt
classes & objects.pptBArulmozhi
 
INHERITANCE, POINTERS, VIRTUAL FUNCTIONS, POLYMORPHISM.pptx
INHERITANCE, POINTERS, VIRTUAL FUNCTIONS, POLYMORPHISM.pptxINHERITANCE, POINTERS, VIRTUAL FUNCTIONS, POLYMORPHISM.pptx
INHERITANCE, POINTERS, VIRTUAL FUNCTIONS, POLYMORPHISM.pptxDeepasCSE
 
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 LevelRamrao Desai
 
Java oops PPT
Java oops PPTJava oops PPT
Java oops PPTkishu0005
 
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

WSO2Con2024 - Simplified Integration: Unveiling the Latest Features in WSO2 L...
WSO2Con2024 - Simplified Integration: Unveiling the Latest Features in WSO2 L...WSO2Con2024 - Simplified Integration: Unveiling the Latest Features in WSO2 L...
WSO2Con2024 - Simplified Integration: Unveiling the Latest Features in WSO2 L...WSO2
 
Driving Innovation: Scania's API Revolution with WSO2
Driving Innovation: Scania's API Revolution with WSO2Driving Innovation: Scania's API Revolution with WSO2
Driving Innovation: Scania's API Revolution with WSO2WSO2
 
WSO2Con2024 - Unleashing the Financial Potential of 13 Million People
WSO2Con2024 - Unleashing the Financial Potential of 13 Million PeopleWSO2Con2024 - Unleashing the Financial Potential of 13 Million People
WSO2Con2024 - Unleashing the Financial Potential of 13 Million PeopleWSO2
 
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2
 
WSO2CON 2024 - Designing Event-Driven Enterprises: Stories of Transformation
WSO2CON 2024 - Designing Event-Driven Enterprises: Stories of TransformationWSO2CON 2024 - Designing Event-Driven Enterprises: Stories of Transformation
WSO2CON 2024 - Designing Event-Driven Enterprises: Stories of TransformationWSO2
 
%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in sowetomasabamasaba
 
WSO2Con204 - Hard Rock Presentation - Keynote
WSO2Con204 - Hard Rock Presentation - KeynoteWSO2Con204 - Hard Rock Presentation - Keynote
WSO2Con204 - Hard Rock Presentation - KeynoteWSO2
 
Evolving Data Governance for the Real-time Streaming and AI Era
Evolving Data Governance for the Real-time Streaming and AI EraEvolving Data Governance for the Real-time Streaming and AI Era
Evolving Data Governance for the Real-time Streaming and AI Eraconfluent
 
WSO2CON 2024 - Lessons from the Field: Legacy Platforms – It's Time to Let Go...
WSO2CON 2024 - Lessons from the Field: Legacy Platforms – It's Time to Let Go...WSO2CON 2024 - Lessons from the Field: Legacy Platforms – It's Time to Let Go...
WSO2CON 2024 - Lessons from the Field: Legacy Platforms – It's Time to Let Go...WSO2
 
WSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security ProgramWSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security ProgramWSO2
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...Shane Coughlan
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfonteinmasabamasaba
 
WSO2CON 2024 - Building a Digital Government in Uganda
WSO2CON 2024 - Building a Digital Government in UgandaWSO2CON 2024 - Building a Digital Government in Uganda
WSO2CON 2024 - Building a Digital Government in UgandaWSO2
 
WSO2CON 2024 - Software Engineering for Digital Businesses
WSO2CON 2024 - Software Engineering for Digital BusinessesWSO2CON 2024 - Software Engineering for Digital Businesses
WSO2CON 2024 - Software Engineering for Digital BusinessesWSO2
 
WSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With SimplicityWSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With SimplicityWSO2
 
WSO2CON 2024 - IoT Needs CIAM: The Importance of Centralized IAM in a Growing...
WSO2CON 2024 - IoT Needs CIAM: The Importance of Centralized IAM in a Growing...WSO2CON 2024 - IoT Needs CIAM: The Importance of Centralized IAM in a Growing...
WSO2CON 2024 - IoT Needs CIAM: The Importance of Centralized IAM in a Growing...WSO2
 
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2
 
WSO2CON 2024 - Unlocking the Identity: Embracing CIAM 2.0 for a Competitive A...
WSO2CON 2024 - Unlocking the Identity: Embracing CIAM 2.0 for a Competitive A...WSO2CON 2024 - Unlocking the Identity: Embracing CIAM 2.0 for a Competitive A...
WSO2CON 2024 - Unlocking the Identity: Embracing CIAM 2.0 for a Competitive A...WSO2
 
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...WSO2
 
WSO2Con2024 - Low-Code Integration Tooling
WSO2Con2024 - Low-Code Integration ToolingWSO2Con2024 - Low-Code Integration Tooling
WSO2Con2024 - Low-Code Integration ToolingWSO2
 

Recently uploaded (20)

WSO2Con2024 - Simplified Integration: Unveiling the Latest Features in WSO2 L...
WSO2Con2024 - Simplified Integration: Unveiling the Latest Features in WSO2 L...WSO2Con2024 - Simplified Integration: Unveiling the Latest Features in WSO2 L...
WSO2Con2024 - Simplified Integration: Unveiling the Latest Features in WSO2 L...
 
Driving Innovation: Scania's API Revolution with WSO2
Driving Innovation: Scania's API Revolution with WSO2Driving Innovation: Scania's API Revolution with WSO2
Driving Innovation: Scania's API Revolution with WSO2
 
WSO2Con2024 - Unleashing the Financial Potential of 13 Million People
WSO2Con2024 - Unleashing the Financial Potential of 13 Million PeopleWSO2Con2024 - Unleashing the Financial Potential of 13 Million People
WSO2Con2024 - Unleashing the Financial Potential of 13 Million People
 
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
 
WSO2CON 2024 - Designing Event-Driven Enterprises: Stories of Transformation
WSO2CON 2024 - Designing Event-Driven Enterprises: Stories of TransformationWSO2CON 2024 - Designing Event-Driven Enterprises: Stories of Transformation
WSO2CON 2024 - Designing Event-Driven Enterprises: Stories of Transformation
 
%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto
 
WSO2Con204 - Hard Rock Presentation - Keynote
WSO2Con204 - Hard Rock Presentation - KeynoteWSO2Con204 - Hard Rock Presentation - Keynote
WSO2Con204 - Hard Rock Presentation - Keynote
 
Evolving Data Governance for the Real-time Streaming and AI Era
Evolving Data Governance for the Real-time Streaming and AI EraEvolving Data Governance for the Real-time Streaming and AI Era
Evolving Data Governance for the Real-time Streaming and AI Era
 
WSO2CON 2024 - Lessons from the Field: Legacy Platforms – It's Time to Let Go...
WSO2CON 2024 - Lessons from the Field: Legacy Platforms – It's Time to Let Go...WSO2CON 2024 - Lessons from the Field: Legacy Platforms – It's Time to Let Go...
WSO2CON 2024 - Lessons from the Field: Legacy Platforms – It's Time to Let Go...
 
WSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security ProgramWSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security Program
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
 
WSO2CON 2024 - Building a Digital Government in Uganda
WSO2CON 2024 - Building a Digital Government in UgandaWSO2CON 2024 - Building a Digital Government in Uganda
WSO2CON 2024 - Building a Digital Government in Uganda
 
WSO2CON 2024 - Software Engineering for Digital Businesses
WSO2CON 2024 - Software Engineering for Digital BusinessesWSO2CON 2024 - Software Engineering for Digital Businesses
WSO2CON 2024 - Software Engineering for Digital Businesses
 
WSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With SimplicityWSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
 
WSO2CON 2024 - IoT Needs CIAM: The Importance of Centralized IAM in a Growing...
WSO2CON 2024 - IoT Needs CIAM: The Importance of Centralized IAM in a Growing...WSO2CON 2024 - IoT Needs CIAM: The Importance of Centralized IAM in a Growing...
WSO2CON 2024 - IoT Needs CIAM: The Importance of Centralized IAM in a Growing...
 
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
 
WSO2CON 2024 - Unlocking the Identity: Embracing CIAM 2.0 for a Competitive A...
WSO2CON 2024 - Unlocking the Identity: Embracing CIAM 2.0 for a Competitive A...WSO2CON 2024 - Unlocking the Identity: Embracing CIAM 2.0 for a Competitive A...
WSO2CON 2024 - Unlocking the Identity: Embracing CIAM 2.0 for a Competitive A...
 
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
 
WSO2Con2024 - Low-Code Integration Tooling
WSO2Con2024 - Low-Code Integration ToolingWSO2Con2024 - Low-Code Integration Tooling
WSO2Con2024 - Low-Code Integration Tooling
 

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