0% found this document useful (0 votes)
788 views14 pages

Constructors in Java

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
788 views14 pages

Constructors in Java

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 14

WARANGAL

INSTITUTE OF
TECHNOLOGY &
SCIENCE
CONSTRUCTORS IN
JAVA
Presented by:
B.Srinidhi
CONTENTS

• What is Constructor
• Rules for creating Constructor
• Types of Java Constructor
• Constructor Overloading
What is Constructor

• Constructor in java is a special type of method that is used to


initialize the object.
• Java Constructor is invoked at the time of object creation.
• It can be used to set initial values for object attributes.
Rules for creating java Constructor

• Constructor name must be same as its


class name.
• Constructor must have no explicit return
type.
• Constructors can be overloaded.
• Constructor cannot be static.
Types of Constructor

•Default Constructor
•Parameterized Constructor
Default Constructor

• A Constructor that have no parameter is known as default Constructor


• Syntax of default Constructor:
<class_name>{}
Example on Default Constructor
Class A
{
A()
{
System.out.println(“This is a default Constructor”);
}
Void display()
{
System.out.println(“this is a method”);
}
Class default_Cons
{
Public static void main(String args[])
{
A obj=new A();
Obj.display();
}
}
Parameterized Constructor
• A Constructor that have parameters is known as Parameterized
Constructor.
• Parameterized Constructor is used to provide different values to the
distinct objects.
• Syntax:
Class class name
{
Class name(par_list)
{
…….
…….
}
}
Program on Parameterized Constructor
Class employee1
{
string name
int id;
Employee1(string name, int id);
{
this.name=name;
this.id=id;
}
Void display()
{
System.out.println(“employee name is: name”+”employee id
is:”+id);
}
}
Class employee1
{
Public static void main(String args[])
{
employee e1=new employee1(“karan”,1000);
employee e2=new employee2(“Aryan”,1002);
}
}
Constructor Overloading

• A class has multiple Constructors having


the same name but different in
parameters.
• When an object is created, a Constructor
is invoked based on the number and
types of arguments passed.
Program on Constructor Overloading
Public class person
{
String name;
int age;
Public person(String name)
{
this.name=name;
}
Public person(string name, int age)
{
this(name);
this.age=age;
}
Public void displaydetails()
{
System.out.println(“name:”+ name);
System.out.println(“age:”+ age);
}
Public static void main(String args[])
{
Person person1=new person(“karan”,28);
Person person2=new person(“Aryan”,30);
Person1.displaydetails();
Person2.displaydetails();
}
• THANK YOU

You might also like