object oriented programming CONSTRUCTORS.pptx

MattFlordeliza1 10 views 9 slides Jul 13, 2024
Slide 1
Slide 1 of 9
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9

About This Presentation

use for modules in object oriented programming (constructors)


Slide Content

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 (); } }