Constructor (No-Arg, Parameterized, Default)

Estimated reading: 3 minutes 127 Views

What is a Constructor?

  • It’s a special method in a Java class that’s automatically invoked when a new object of that class is created.
  • Its primary purpose is to initialize the object’s attributes (fields or variables) to appropriate initial values.
  • It ensures objects are created in a valid and consistent state.

Rules for Creating Constructors:

  1. Same Name as Class: The constructor’s name must be exactly the same as the class name.
  2. No Explicit Return Type: Constructors don’t have a return type, not even void.
  3. Cannot Have Modifiers: Constructors cannot be declared as abstract, static, final, or synchronized.

Constructor Types:

  1. No-Arg Constructor: a constructor that does not accept any arguments.
    public class Product {
        private String name;
        private String description;
        private float price;
        private int quantity;
        private float discount;
    
        public Product() {
            this.name = "No name";
            this.description = "No description";
            this.price = 0.0f;
            this.quantity = 0;
            this.discount = 0;
        }
    }
  2. Parameterized Constructor: a constructor that accepts arguments.
    public Product(String n, String d, float p, int q, float dis) {
            this.name = n;
            this.description = d;
            this.price = p;
            this.quantity = q;
            this.discount = dis;
        }
  3. Default Constructor: A constructor that is automatically created by Java compiler if it is not explicitly defined.

Constructor Chaining

Constructor Chaining: When a constructor calls another constructor of the same class then it is called constructor chaining.

Code:

Product class:

public class Product {
    private String name;
    private String description;
    private float price;
    private int quantity;
    private float discount;
    private String color;
    
    public Product() {
        this.name = "No name";
        this.description = "No description";
        this.price = 0.0f;
        this.quantity = 0;
        this.discount = 0;
    }
    
    public Product(String n, String d, float p, int q, float dis) {
        this.name = n;
        this.description = d;
        this.price = p;
        this.quantity = q;
        this.discount = dis;
        System.out.println("constructor: 5");
    }
    
    public Product(String n, String d, float p, int q, float dis, String c) {
        this (n,d,p,q,dis);
        this.color = c;
        System.out.println("constructor: 6");
    }
    
    public void display() {
        System.out.println("Name = " + name);
        System.out.println("description = " + description);
        System.out.println("Price = " + price);
        System.out.println("Quantity = " + quantity);
        System.out.println("Discount = " + Discount);
    }
}

Attributes:

  • name: String representing the product name.
  • description: String describing the product.
  • price: float storing the product price.
  • quantity: int representing the product quantity.
  • discount: float representing the discount percentage.
  • color: String (optional) for the product color (added in the third constructor).

Constructors:

  • Product() (Default Constructor): Initializes all attributes with default values (“No name”, “No description”, etc.).
  • Product(String n, String d, float p, int q, float dis): Parameterized constructor taking arguments for name, description, price, quantity, and discount. Prints “constructor: 5” upon invocation.
  • Product(String n, String d, float p, int q, float dis, String c): Parameterized constructor taking arguments for all attributes, including color. Internally calls the second constructor (“constructor: 5”) and then sets the color. Prints “constructor: 6” upon invocation.
  • display() method: Prints the product information in a user-friendly format.

Main Class:

public class Main
{
    public static void main(String args[]) {
        Product p1 = new Product("Camera", "Auto focus", 99, 10, 5, "red");
        Product p2 = new Product();
        p.display();

    }
}

Creates two Product objects:

  • p1 using the third constructor, providing values for all attributes.
  • p2 using the default constructor (no arguments provided).

Attempts to call the display() method on the incorrectly referenced variable p (should be p2) to display the default values.

Share this

Constructor (No-Arg, Parameterized, Default)

Or copy link

CONTENTS
English