KEMBAR78
object oriented programming CONSTRUCTORS.pptx
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();
}
}

object oriented programming CONSTRUCTORS.pptx

  • 1.
  • 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 ofconstructors  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 classPerson { 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 classCar { 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 classRectangle { 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 classStudent { 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 { privateString 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(); } }