DEV Community

MANIKANDAN
MANIKANDAN

Posted on

constructors in Java: no-arg constructor, and parameterised constructor.

Using No-Argument Constructor and Parameterised Constructor

 public class Student {
    int RollNumber;
    String Name;
    String Department;
    int Mark1;
    int Mark2;
    int Mark3;`

   ` Student(int Mark1,int Mark2,int Mark3){
        this.Mark1=Mark1;
        this.Mark2=Mark2;
        this.Mark3=Mark3;
    }
    // No Argument Constructor
    Student(){

    }
    Student(int Mark1,int Mark2 ){
     this.Mark1=Mark1;
     this.Mark2=Mark2;

    }

    int getAverage(){
        int Total=(Mark1+Mark2+Mark3)/3;
        return Total;
    }

    void getMark(){
        System.out.println("mark1 "+Mark1);
        System.out.println("mark2 "+Mark2);

    }

    public static void main(String[] arg) {

        //Parameter constructor
        Student mamoj = new Student(50,30,85);

        //default constructor
         System.out.println("Average of     mamoj"+mamoj.getAverage())     ;
        Student lokesh = new Student();
        System.out.println("Average of lokesh   "+lokesh.getAverage());

        Student Rajan  = new Student(20,30,40);
        System.out.println(Rajan.getAverage());

    // constructor overloading
        Student arun =new Student(20,20);
        arun.getMark();


    }
}


Enter fullscreen mode Exit fullscreen mode

In the above program, we have seen how to implement a no-argument constructor and a parameterised constructor

From that, I have learned why an instant variable return
some default value, there comes a default constructor
The compiler creates a constructor for the class with
default value when we do not initialise any value to the instance variable

In a constructor, we can create many constructors with the same name but with different parameters. Now we can recall our
Last topic polymorphism, in that we have seen method overloading, what is method overloading, same method name but different parameters, here also, we can use the same constructor name with different parameters(constructor overloading ), no constructor is overriding in the constructor

What is the difference between a parameter and an argument

Parameter
In programming, a parameter is a variable in a function or method declaration. It serves as a placeholder for data that will be provided when the function or method is called. Parameters define the type and number of values that a function or method can accept.

They define the types and order of values that a function can accept. Parameters are used to receive the arguments passed to a function when it is called.

Example:
add(x, y): # Here, x and y are parameters
return x + y

Arguments
Arguments, also known as actual arguments, are the values supplied to a function when it is called. These values serve as inputs to the function during its execution.

They are used to supply the values to the parameters of a function. The number of arguments must match the number of parameters in the function definition.

Example:
add(10,20) //10 and 20 are the arguments

Top comments (0)