Parametrized Constructor & Constructor Overloading in Java

Parameterized constructors
The parameterized constructors are the constructors having a specific number of arguments to be passed. The purpose of a parameterized constructor is to assign user-wanted specific values to the instance variables of different objects.

Example :
      When we create the object like this Student s = new Student ( “Joes” , 15 ) then the new keyword invokes the Parameterized constructor with int and string parameters public Student( String n , String a ) after object creation.

Constructor overloading
       Constructors are special methods named after the class and without a return type, and are used to construct objects. Constructors, like methods, can take input parameters. Constructors are used to initialize objects. Constructor overloading in Java means to more than one constructor in an instance class.

Source Code

//Parameterized Constructor & Constructor Overloading
class Box
    float length,breadth;
    public Box(){  //Default
    public  Box(float x,float y) //Parameterized
    Box(float x)
    float area()
        return  length*breadth;
public class constructor_overloading {
    public static void main(String args[]) {
        Box o =new Box();
        System.out.println("Area : "+o.area() );
        Box o1 =new Box(3,6);
        System.out.println("Area : "+o1.area() );
        Box o2 =new Box(3);
        System.out.println("Area : "+o2.area() );


Area : 10.0
Area : 18.0
Area : 9.0
To download raw file Click Here

Basic Programs