1
CONSTRUCTOR
Presented by
2022
4th November
OBJECTIVES
3
• What is Constructor?
• Rules of Constructor
• Types of Constructor
• Constructor examples
4
 In one constructor we can use different
Parameters.(String name, int number)
CONSTRUCTOR
In java,a Constructor is a block of codes
similar to the method. which is used to
initialize the object.
It has no return type.
Every class contain at-least one Constructor.
Symbol of Constructor
i.e. MyClass{
MyClass(String name){
 We do not require to call the constructor
manually. It automatically invokes implicitly
during the instantiation..
5
RULES FOR CREATING JAVA
CONSTRUCTOR
• Constructor name must be the same
as its class name.
• A Constructor must have no explicit
return type.
6
Default Constructor Parameterized Constructor
 Default Constructor doesn't have any
parameter.
 Syntax of default constructor:
<class-name>(){
}
 It has a specific number of parameters.
 It is used to provide different values to
distinct objects. You can provide the same
values also.
TYPES OF JAVA CONSTRUCOR
There are two types of constructor in java:
• Default Constructor(no-argument Constructor)
• Parameterized Constructor
7
Constructor Example
Create a Constructor class which show a
vehicle model and name.
public class MainClass2 {
int modelYear;
String modelName;
public MainClass2(int year, String name) {
modelYear = year;
modelName = name;
}
public static void main(String[] args) {
MainClass2 myCar=new MainClass2(2022,"Vigo");
System.out.println(myCar.modelYear+" "+ myCar.modelName);
}
}
output=
2022 Vigo
8
Please ask one question at a time.
Give respect to each other.
QUESTION ANSWER SESSION!
THANK YOU!
Phone
Email

Constructor in java Presentation by Dawood Khan .pptx

  • 1.
  • 2.
  • 3.
    OBJECTIVES 3 • What isConstructor? • Rules of Constructor • Types of Constructor • Constructor examples
  • 4.
    4  In oneconstructor we can use different Parameters.(String name, int number) CONSTRUCTOR In java,a Constructor is a block of codes similar to the method. which is used to initialize the object. It has no return type. Every class contain at-least one Constructor. Symbol of Constructor i.e. MyClass{ MyClass(String name){  We do not require to call the constructor manually. It automatically invokes implicitly during the instantiation..
  • 5.
    5 RULES FOR CREATINGJAVA CONSTRUCTOR • Constructor name must be the same as its class name. • A Constructor must have no explicit return type.
  • 6.
    6 Default Constructor ParameterizedConstructor  Default Constructor doesn't have any parameter.  Syntax of default constructor: <class-name>(){ }  It has a specific number of parameters.  It is used to provide different values to distinct objects. You can provide the same values also. TYPES OF JAVA CONSTRUCOR There are two types of constructor in java: • Default Constructor(no-argument Constructor) • Parameterized Constructor
  • 7.
    7 Constructor Example Create aConstructor class which show a vehicle model and name. public class MainClass2 { int modelYear; String modelName; public MainClass2(int year, String name) { modelYear = year; modelName = name; } public static void main(String[] args) { MainClass2 myCar=new MainClass2(2022,"Vigo"); System.out.println(myCar.modelYear+" "+ myCar.modelName); } } output= 2022 Vigo
  • 8.
    8 Please ask onequestion at a time. Give respect to each other. QUESTION ANSWER SESSION!
  • 9.