Java Constructors

Ad Code

Java Constructors


What is Constructors?

constructor in Java is a special method that is used to initialize objects. The constructor is called when an object of a class is created. The purpose of a Java constructor is to initializes the newly created object before it is used.

Example

class myConstructor
{
    myConstructor()
  {
    System.out.println("Hello! I am a Constructor");
   }
  public void display()
   {
    System.out.println("Hello! I am Not a Constructor");
   }
}
class constexmp
{
   public static void main(String arg[])
   {
    myConstructor obj=new myConstructor();
    obj.display();
  }
}


Constructor Parameters

A constructor which has a specific number of parameters is called a parameterized constructor.
Example

class cake
{
  int m1,m2,m3,m4;
  //creating a parameterized constructor
  public cake(int maths,int phy,int chem,int java)
  {
    m1=maths;
    m2=phy;
   m3=chem;
    m4=java;
  }
  public void abc()
  {
    int total=(m1+m2+m3+m4);
    int avg=(m1+m2+m3+m4)/4;
    System.out.println("Total is: "+total);
   System.out.println("Average is: "+avg);
  }
}
public class constructcake
{
  public static void main(String args[])
  {
   cake ck=new cake(50,49,48,47);
   ck.abc();
  }
}


Constructor Overloading in Java

Constructor overloading in Java is a technique of having more than one constructor with different parameter just like a method but without return type.
Example

//Java program to constructors overloading
class Student{
  int rl;
  String name;
  int mark;
  //creating two arg constructor
  Student(int r,String n){
   rl = r;
   name = n;
  }
  //creating three arg constructor
  Student(int r,String n,int m){
   rl= r;
   name = n;
   mark=m;
  }
  void display(){
   System.out.println(rl+" "+name+" "+mark);
  }
  public static void main(String args[]){
    Student s1 = new Student(101,"Mohan");
    Student s2 = new Student(102,"Shohan",425);
    s1.display();
    s2.display();
  }
}


Output

   101   Mohan
   102   Shohan   425

Difference between constructor and method

Java ConstructorJava Method
A constructor is used to initialize the state of an object.A method is used to display the behavior of an object.
A constructor must not have a return type.A method must have a return type.
The constructor is invoked implicitly.The method is invoked explicitly.
The Java compiler provides a default constructor if you don't have any constructor in a class.The method is not provided by the compiler in any case.
The constructor name must be same as the class name.The method name may or may not be same as the class name.

Important Points:

  • If you don't define a constructor, Java provides a default constructor.
  • If you define a constructor (either parameterized or default), Java will not provide a default constructor.
  • The constructor is used to initialize objects, but it is not used to perform operations or return values like regular methods.






 

Post a Comment

0 Comments

Ad Code