SlideShare a Scribd company logo
CONSTRUCTORS
Introduction
 In Java, a constructor is a special type of
method that is used to initialize objects of a
class. Constructors have the same name as
the class they belong to and are automatically
called when an object of the class is created.
They allow you to set initial values or perform
setup tasks for the object. Constructors play a
crucial role in the object-oriented programming
paradigm and are essential for creating and
initializing objects.
Other definitions of constructors
 Initialization: Constructors are used to initialize the newly created object. They set initial
values for the object's attributes (instance variables).
 Same Name as Class: A constructor has the same name as the class it belongs to. This
establishes the association between the constructor and the class it initializes.
 No Return Type: Unlike regular methods, constructors do not have a return type, not even
void. This distinguishes them from other methods.
 Automatic Invocation: Constructors are automatically called when an object of the class
is created using the new keyword.
 Default Constructor: If a class does not explicitly define any constructors, Java provides
a default constructor. This constructor initializes the object with default values (e.g.,
numeric variables to 0, object references to null).
 Parameterized Constructors: Java allows constructors to accept parameters. These
parameters can be used to initialize the object with specific values passed during object
creation.
 Constructor Overloading: Like methods, constructors can be overloaded, meaning a
class can have multiple constructors with different parameter lists. This allows for flexibility
in object initialization.
 Constructor Chaining: Constructors can call other constructors in the same class using
this() keyword (for calling another constructor in the same class) or super() keyword (for
calling a constructor in the superclass).
public class MyClass {
// Constructor definition
public MyClass() {
// Constructor body - initialization
tasks
}
// Rest of the class members and
methods
}
Default Constructors
public class Person {
private String name;
private int age;
// Default constructor (implicitly provided by
Java)
public Person() {
// No additional initialization needed
}
// Rest of the class members and methods
}
Parameterized constructors
public class Car {
private String make;
private String model;
private int year;
// Parameterized constructor
public Car(String make, String model, int year) {
this.make = make;
this.model = model;
this.year = year;
}
// Rest of the class members and methods
}
Constructors overloading
public class Rectangle {
private int length;
private int width;
// Constructor with no parameters (default)
public Rectangle() {
this.length = 0;
this.width = 0;
}
// Parameterized constructor with one
parameter
public Rectangle(int side) {
this.length = side;
this.width = side;
}
// Parameterized constructor
with two parameters
public Rectangle(int length, int
width) {
this.length = length;
this.width = width;
}
// Rest of the class members
and methods
}
Constructor chaining
public class Student {
private String name;
private int age;
private String department;
// Full parameterized
constructor
public Student(String name,
int age, String department) {
this.name = name;
this.age = age;
this.department =
department;
}
// Constructor with
partial information
(constructor chaining)
public
Student(String name, int
age) {
this(name, age,
"Undeclared");
}
// Rest of the class
members and methods
}
class Car {
private String make;
private String model;
private int year;
// Parameterized constructor
public Car(String make, String model,
int year) {
this.make = make;
this.model = model;
this.year = year;
}
// Method to display car information
public void displayInfo() {
System.out.println("Make: " + make +
", Model: " + model + ", Year: " +
year);
}
}
public class Main {
public static void main(String[] args) {
// Create instances of Car class
Car car1 = new Car("Toyota",
"Camry", 2022);
Car car2 = new Car("Ford",
"Mustang", 2023);
// Call the displayInfo method for
each car
car1.displayInfo();
car2.displayInfo();
}
}

More Related Content

PDF
Constructors In Java – Unveiling Object Creation
PPTX
Constructor and destructor in oop
PDF
Constructors in Java (2).pdf
PPT
Sonu wiziq
PPTX
A constructor in Java is a special method that is used to initialize objects
PPTX
Java ConstructorsPPT.pptx
PPTX
Constructor in java
PPTX
Semi-1-Constructor Constructor Constructor
Constructors In Java – Unveiling Object Creation
Constructor and destructor in oop
Constructors in Java (2).pdf
Sonu wiziq
A constructor in Java is a special method that is used to initialize objects
Java ConstructorsPPT.pptx
Constructor in java
Semi-1-Constructor Constructor Constructor

Similar to object oriented programming CONSTRUCTORS.pptx (20)

PPTX
UNIT - IIInew.pptx
PDF
Java Programming - 04 object oriented in java
PPTX
Constructors in java
PPTX
C++ Constructor destructor
PPTX
Ch-2ppt.pptx
PDF
Java constructors and types with examples
PPTX
DeclaringConstructir.pptx
PPTX
UNIT 3- Java- Inheritance, Multithreading.pptx
PPTX
C# classes objects
PPTX
DAY 9 Constructor java in java full construction.pptx
PPTX
Constructor and Destructor
PPTX
Constructors in java
PDF
Object Oriented Programming using JAVA Notes
PDF
Advanced CPP Lecture 2- Summer School 2014 - ACA CSE IITK
PPTX
BCA Class and Object (3).pptx
PPTX
Java Constructors
PPTX
Java Constructor
PPTX
Methods and constructors in java
UNIT - IIInew.pptx
Java Programming - 04 object oriented in java
Constructors in java
C++ Constructor destructor
Ch-2ppt.pptx
Java constructors and types with examples
DeclaringConstructir.pptx
UNIT 3- Java- Inheritance, Multithreading.pptx
C# classes objects
DAY 9 Constructor java in java full construction.pptx
Constructor and Destructor
Constructors in java
Object Oriented Programming using JAVA Notes
Advanced CPP Lecture 2- Summer School 2014 - ACA CSE IITK
BCA Class and Object (3).pptx
Java Constructors
Java Constructor
Methods and constructors in java
Ad

More from MattFlordeliza1 (20)

PPTX
PHILHEALTH SLIDE FOR EYE SCREENING WITH FLOWCHART .pptx
PPTX
KATOTOHANAN SLIDE SA ESP GRADE 10 SLIDE.pptx
PPTX
NON - DETERMINISTIC AUTOMATA POWERPOINT.pptx
PPTX
Inheritance in Object-Oriented Programming (OOP) (not).pptx
PPTX
software reuses use for mod in college .pptx
PPTX
ENCAPSULATION module for IT or comsci.pptx
PPTX
Automata2(Chapter1)uses as module in college.pptx
PPTX
Introduction to automata use for discussion and overview.pptx
PPTX
CLASSES AND OBJECT SAMPLE use for discussion.pptx
PPTX
benefits or programming in any language.pptx
PPTX
programming language java applications.pptx
PPTX
CLASSES AND OBJECT SAMPLE USES A MODULE OF STUDENT.pptx
PPTX
web host used tsu lecture reliable for other .pptx
PPTX
bootstrap 4 used for discussion in chcci.pptx
PPTX
JDBC OVERVIEW uses from the subject of EDP.pptx
PPTX
ACtionlistener in java use in discussion.pptx
PPTX
PLF-Lesson tsu lecture time 2 units-2.pptx
PPTX
PLF-Lesson-5 programming in TSU lec.pptx
PPTX
Psuedocode1, algorithm1, Flowchart1.pptx
PPTX
www module 1.pptx
PHILHEALTH SLIDE FOR EYE SCREENING WITH FLOWCHART .pptx
KATOTOHANAN SLIDE SA ESP GRADE 10 SLIDE.pptx
NON - DETERMINISTIC AUTOMATA POWERPOINT.pptx
Inheritance in Object-Oriented Programming (OOP) (not).pptx
software reuses use for mod in college .pptx
ENCAPSULATION module for IT or comsci.pptx
Automata2(Chapter1)uses as module in college.pptx
Introduction to automata use for discussion and overview.pptx
CLASSES AND OBJECT SAMPLE use for discussion.pptx
benefits or programming in any language.pptx
programming language java applications.pptx
CLASSES AND OBJECT SAMPLE USES A MODULE OF STUDENT.pptx
web host used tsu lecture reliable for other .pptx
bootstrap 4 used for discussion in chcci.pptx
JDBC OVERVIEW uses from the subject of EDP.pptx
ACtionlistener in java use in discussion.pptx
PLF-Lesson tsu lecture time 2 units-2.pptx
PLF-Lesson-5 programming in TSU lec.pptx
Psuedocode1, algorithm1, Flowchart1.pptx
www module 1.pptx
Ad

Recently uploaded (20)

PDF
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
PDF
CIFDAQ's Teaching Thursday: Moving Averages Made Simple
PDF
Smarter Business Operations Powered by IoT Remote Monitoring
PDF
Software Development Methodologies in 2025
PPTX
ChatGPT's Deck on The Enduring Legacy of Fax Machines
PDF
Using Anchore and DefectDojo to Stand Up Your DevSecOps Function
PPTX
Comunidade Salesforce São Paulo - Desmistificando o Omnistudio (Vlocity)
PDF
Doc9.....................................
PPTX
ABU RAUP TUGAS TIK kelas 8 hjhgjhgg.pptx
PDF
Top Generative AI Tools for Patent Drafting in 2025.pdf
PPTX
Telecom Fraud Prevention Guide | Hyperlink InfoSystem
PDF
NewMind AI Weekly Chronicles - July'25 - Week IV
PDF
agentic-ai-and-the-future-of-autonomous-systems.pdf
PDF
Reimagining Insurance: Connected Data for Confident Decisions.pdf
PDF
Revolutionize Operations with Intelligent IoT Monitoring and Control
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Cloud-Migration-Best-Practices-A-Practical-Guide-to-AWS-Azure-and-Google-Clou...
PPTX
CroxyProxy Instagram Access id login.pptx
PDF
This slide provides an overview Technology
PDF
Event Presentation Google Cloud Next Extended 2025
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
CIFDAQ's Teaching Thursday: Moving Averages Made Simple
Smarter Business Operations Powered by IoT Remote Monitoring
Software Development Methodologies in 2025
ChatGPT's Deck on The Enduring Legacy of Fax Machines
Using Anchore and DefectDojo to Stand Up Your DevSecOps Function
Comunidade Salesforce São Paulo - Desmistificando o Omnistudio (Vlocity)
Doc9.....................................
ABU RAUP TUGAS TIK kelas 8 hjhgjhgg.pptx
Top Generative AI Tools for Patent Drafting in 2025.pdf
Telecom Fraud Prevention Guide | Hyperlink InfoSystem
NewMind AI Weekly Chronicles - July'25 - Week IV
agentic-ai-and-the-future-of-autonomous-systems.pdf
Reimagining Insurance: Connected Data for Confident Decisions.pdf
Revolutionize Operations with Intelligent IoT Monitoring and Control
NewMind AI Weekly Chronicles - August'25 Week I
Cloud-Migration-Best-Practices-A-Practical-Guide-to-AWS-Azure-and-Google-Clou...
CroxyProxy Instagram Access id login.pptx
This slide provides an overview Technology
Event Presentation Google Cloud Next Extended 2025

object oriented programming CONSTRUCTORS.pptx

  • 2. Introduction  In Java, a constructor is a special type of method that is used to initialize objects of a class. Constructors have the same name as the class they belong to and are automatically called when an object of the class is created. They allow you to set initial values or perform setup tasks for the object. Constructors play a crucial role in the object-oriented programming paradigm and are essential for creating and initializing objects.
  • 3. Other definitions of constructors  Initialization: Constructors are used to initialize the newly created object. They set initial values for the object's attributes (instance variables).  Same Name as Class: A constructor has the same name as the class it belongs to. This establishes the association between the constructor and the class it initializes.  No Return Type: Unlike regular methods, constructors do not have a return type, not even void. This distinguishes them from other methods.  Automatic Invocation: Constructors are automatically called when an object of the class is created using the new keyword.  Default Constructor: If a class does not explicitly define any constructors, Java provides a default constructor. This constructor initializes the object with default values (e.g., numeric variables to 0, object references to null).  Parameterized Constructors: Java allows constructors to accept parameters. These parameters can be used to initialize the object with specific values passed during object creation.  Constructor Overloading: Like methods, constructors can be overloaded, meaning a class can have multiple constructors with different parameter lists. This allows for flexibility in object initialization.  Constructor Chaining: Constructors can call other constructors in the same class using this() keyword (for calling another constructor in the same class) or super() keyword (for calling a constructor in the superclass).
  • 4. public class MyClass { // Constructor definition public MyClass() { // Constructor body - initialization tasks } // Rest of the class members and methods }
  • 5. Default Constructors public class Person { private String name; private int age; // Default constructor (implicitly provided by Java) public Person() { // No additional initialization needed } // Rest of the class members and methods }
  • 6. Parameterized constructors public class Car { private String make; private String model; private int year; // Parameterized constructor public Car(String make, String model, int year) { this.make = make; this.model = model; this.year = year; } // Rest of the class members and methods }
  • 7. Constructors overloading public class Rectangle { private int length; private int width; // Constructor with no parameters (default) public Rectangle() { this.length = 0; this.width = 0; } // Parameterized constructor with one parameter public Rectangle(int side) { this.length = side; this.width = side; } // Parameterized constructor with two parameters public Rectangle(int length, int width) { this.length = length; this.width = width; } // Rest of the class members and methods }
  • 8. Constructor chaining public class Student { private String name; private int age; private String department; // Full parameterized constructor public Student(String name, int age, String department) { this.name = name; this.age = age; this.department = department; } // Constructor with partial information (constructor chaining) public Student(String name, int age) { this(name, age, "Undeclared"); } // Rest of the class members and methods }
  • 9. class Car { private String make; private String model; private int year; // Parameterized constructor public Car(String make, String model, int year) { this.make = make; this.model = model; this.year = year; } // Method to display car information public void displayInfo() { System.out.println("Make: " + make + ", Model: " + model + ", Year: " + year); } } public class Main { public static void main(String[] args) { // Create instances of Car class Car car1 = new Car("Toyota", "Camry", 2022); Car car2 = new Car("Ford", "Mustang", 2023); // Call the displayInfo method for each car car1.displayInfo(); car2.displayInfo(); } }