UNDERSTANDING JAVA
CONSTRUCTORS
BY: ZANDRO APOSTOL
WHAT IS A CONSTRUCTOR?
• A constructor in Java is similar to a method that is invoked when an
object of the class is created.
• In Java, a constructor is a special kind of method. It is a member
function of a class with the same name as the class, and it is
responsible for initializing the object of the class.
• They set initial values, support inheritance, and ensure objects are
ready for use.
HERE ARE SOME KEY CHARACTERISTICS
OF CONSTRUCTORS:
1. Class Name: A constructor has the same name as the class it belongs to.
2. No Return Type: Unlike regular methods, constructors don't have a return
type, not even ‘void’. They implicity return an instance of the class.
3. Initialization: The primary purpose of a constructor is to initialize the state
of an object. It is automatically called when an object is created using the ‘new’
keyword.
4. Default Constructor: If a class doesn't explicitly define any constructors,
Java provides a default constructor with no parameters.
5. Overloading: Constructors, like regular methods, can be overloaded by
defining multiple constructors with different parameter lists.
SYNTAX
public – This can be access modifier, which
determines the visibility of the constructor. It can be
‘public’, ‘private’,’protected’.
• Example:
Class with constructor
USING THE CONSTRUCTOR:
You can create an instance of the class and invoke the constructor like this:
PARAMETERIZED CONSTRUCTOR:
You can also create
constructors that accept
parameters. This allows
you to initialize the object
with specific values at the
time of creation. Here's
an example:
Now, when you create a car object, you can pass values to initialize it:
EXAMPLE 1: JAVA CONSTRUCTOR
Name of the
class to be
construct
The
constructor
name, must
same to the
class name
Creating a
new object
TYPES OF CONSTRUCTOR
In Java, constructors can be divided into 3 types:
1. No-Arg Constructor
2. Parameterized Constructor
3. Default Constructor
•1. Java No-Arg Constructors
• Similar to methods, a Java constructor may or may not have any
parameters (arguments).
• If a constructor does not accept any parameters, it is known as a no-
argument constructor. For example,
•Example 2: Java private no-arg constructor
-Example of private no-arg
Constructor
2. JAVA PARAMETERIZED
CONSTRUCTOR
• A Java constructor can also accept one or more parameters. Such
constructors are known as parameterized constructors (constructor
with parameters).
-Example of parameterized
Constructor
3. JAVA DEFAULT CONSTRUCTOR
• If we do not create any constructor, the Java compiler automatically
create a no-arg constructor during the execution of the program. This
constructor is called default constructor.
-Example of default
Constructor
CONSTRUCTORS OVERLOADING IN
JAVA
• Similar to Java method overloading, we can also create two or more
constructors with different parameters. This is called constructors
overloading.
-Example of overloading
Constructor
THANK YOU!

Java ConstructorsPPT.pptx

  • 1.
  • 2.
    WHAT IS ACONSTRUCTOR? • A constructor in Java is similar to a method that is invoked when an object of the class is created. • In Java, a constructor is a special kind of method. It is a member function of a class with the same name as the class, and it is responsible for initializing the object of the class. • They set initial values, support inheritance, and ensure objects are ready for use.
  • 3.
    HERE ARE SOMEKEY CHARACTERISTICS OF CONSTRUCTORS: 1. Class Name: A constructor has the same name as the class it belongs to. 2. No Return Type: Unlike regular methods, constructors don't have a return type, not even ‘void’. They implicity return an instance of the class. 3. Initialization: The primary purpose of a constructor is to initialize the state of an object. It is automatically called when an object is created using the ‘new’ keyword. 4. Default Constructor: If a class doesn't explicitly define any constructors, Java provides a default constructor with no parameters. 5. Overloading: Constructors, like regular methods, can be overloaded by defining multiple constructors with different parameter lists.
  • 4.
    SYNTAX public – Thiscan be access modifier, which determines the visibility of the constructor. It can be ‘public’, ‘private’,’protected’.
  • 5.
  • 6.
    USING THE CONSTRUCTOR: Youcan create an instance of the class and invoke the constructor like this:
  • 7.
    PARAMETERIZED CONSTRUCTOR: You canalso create constructors that accept parameters. This allows you to initialize the object with specific values at the time of creation. Here's an example:
  • 8.
    Now, when youcreate a car object, you can pass values to initialize it:
  • 9.
    EXAMPLE 1: JAVACONSTRUCTOR Name of the class to be construct The constructor name, must same to the class name Creating a new object
  • 11.
    TYPES OF CONSTRUCTOR InJava, constructors can be divided into 3 types: 1. No-Arg Constructor 2. Parameterized Constructor 3. Default Constructor
  • 12.
    •1. Java No-ArgConstructors • Similar to methods, a Java constructor may or may not have any parameters (arguments). • If a constructor does not accept any parameters, it is known as a no- argument constructor. For example,
  • 13.
    •Example 2: Javaprivate no-arg constructor -Example of private no-arg Constructor
  • 14.
    2. JAVA PARAMETERIZED CONSTRUCTOR •A Java constructor can also accept one or more parameters. Such constructors are known as parameterized constructors (constructor with parameters). -Example of parameterized Constructor
  • 15.
    3. JAVA DEFAULTCONSTRUCTOR • If we do not create any constructor, the Java compiler automatically create a no-arg constructor during the execution of the program. This constructor is called default constructor. -Example of default Constructor
  • 16.
    CONSTRUCTORS OVERLOADING IN JAVA •Similar to Java method overloading, we can also create two or more constructors with different parameters. This is called constructors overloading.
  • 17.
  • 18.