Constructor in java

 Constructor:

  • A constructor is one kind of feature in oops.
  • Constructor means, class name the same as the function or method name.
  • It is similar to the function so it’s called a special member function and that class name the same as the constructor name.
  • It used to construct and initialize all the data members.
  • Constructor automatically called, when the object is created.
  • It can be defined inside the class and definitions are outside the class definition help of the class name. 
Special characteristics of the constructor:
  • A constructor should be declared in the public section.
  • The name should be equal to the class name followed by the parenthesis.
  • It does not have any return type, not even void.
  • They cannot be inherited through a derived class can call the base class constructor.
  • It cannot be a virtual function.
Types of the constructor:
  • Three different types of constructors:
           1. Default constructor
           2. Parameterized constructor
Default constructor:
  • A constructor with no parameters is known as a default constructor.
Syntax:
  1. clclass class_name

    {

    class_name ()

    {

              //Statements

    }

    Main function()

    {

              //Object creation:

              Class_name object_name=new class_name();

              //Object calling:

              Object_name.method_name();//If constructor of method then no need this line.

    }

    }

Example:
  1. import java.util.Scanner;

    public class Default_constructor {

    int a,b;

    Scanner sc = new Scanner(System.in);

    Default_constructor()//no parameter with constructor

    {

            System.out.println("Enter a and b value: ");

            a = sc.nextInt();

            b = sc.nextInt();

            System.out.println("Addition value is:" + (a + b));

        }

        public static void main(String args[]) {

            Default_constructor d = new Default_constructor();

        }

    }

Output:
Parameterized constructor:
  • A constructor with parameters is known as a parameterized constructor.
Syntax:
  1. clclass class_name

    {

    class_name (parameter_list)

    {

              //Statements

    }

    Main function()

    {

              //Object creation:

              Class_name object_name=new class_name(parameter_list);

              //Object calling:

              Object_name.method_name(parameter_list_of_values);

    }

    }

Example:
  1. public class Parameterized_constructor {

        int roll_num, mark;

        String name;

        Parameterized_constructor(int x, String y, int z) {

            roll_num = x;

            name = y;

            mark = z;

            System.out.println("Roll Number\t Student Name\t Student total mark");

            System.out.println(roll_num + "\t\t " + name + "\t\t " + mark);

        }

     

        public static void main(String args[]) {

            Parameterized_constructor p1 = new Parameterized_constructor(01, "Ahamed", 455);

            Parameterized_constructor p2 = new Parameterized_constructor(02, "Bhanu", 337);

        }

    }

Output:
Share:

No comments:

Post a Comment

Recent Posts

Service Support

Need our help to Learn or Post New Concepts Contact me