yt

Header Ads

Chapter -3 What is Constructor in java

 Constructor in java 

INTRO IN HINDI:- यदि आप object को create करने से पहले कुछ task perform करना चाहते है जैसे की variable को initialize करना, तो आप constructor Create करते है। Objects को यूज़ करने से पहले जो जरुरी काम आप करना चाहते है,वो सब आप constructor में कर सकते है।

उदाहरण के लिए आप जिस class का object create कर रहे है, उसके variables में कोई values नहीं है। ये values आप user से object create करते समय input करवाना चाहते है। Object क्रिएट करते समय user values (arguments) pass करता है। अब आप को इन values को अपनी class के variables को assign करना है। ये काम आप constructor के through करते है। यदि आप constructor में उन values को assign नहीं करते है, तो object तो create होता है लेकिन वो किसी काम का नहीं होता है। क्योंकि उसमे कोई values ही नहीं होती है। लेकिन जब आप constructor यूज़ करते है तो constructor के execute होने के बाद ही object user के लिए ready होता है। जब आप कोई constructor क्रिएट नहीं करते है तो भी java automatically constructor create करती है। 

Constructor की कुछ characteristics होती है। इन्हे आप constructors क्रिएट करने के rules भी कह सकते है। 
  • Constructors java के memory allocation process का part है। 
  • Constructor एक method की तरह ही होता है।
  • Constructor का नाम class के नाम जैसा ही होता है।
  • Constructor का कोई return type नहीं होता है।
Types of java constructors Java में 4 तरह के constructors होते है।  इन सभी  के बारे में निचे दिया जा रहा है।

1.Default constructor:- 

जब आप कोई constructor create नहीं करते है, too at the run time java automatically default constructor call  करती है| ये constructor variables उनके data type के according initial value के साथ memory allocate कर देता है। 

2.Parameterized Constructors :- 

Es Constructors में  parameters को pass कर सकते है। Objects क्रिएट करते समय आप variables की value arguments की तरह pass कर सकते है। ये values आप constructor में variables को assign कर सकते है। इसका उदाहरण निचे दिया जा रहा है।

3.Copy constructor:- 

Copy constructor को object को किसी दूसरे object ke साथ copy करने के लिए यूज़ किया जाता है। जब आप एक object की value दूसरे object में copy करते है तो दोनों objects ही same values को  print करते है। Copy constructors में आप एक class का object create करते समय उसी class का दूसरा object argument की तरह pass करते है।


Introduction  of Constructor in English 

  • It is special member functions of class that executes when we create the instance(object) of that class. In other word we can say that there are is no need to call a constructor .
  • Its name is same name as class name.
  • It has no return type.
  • It may be parameterized or non-parameterized.
  • It is used to initialize class level variable.
  • It is automatically executed at the time of instance (Object), So we should define a Constructor in every class.
  • If  didn't provided user defined  Constructor in the class then compiler provides default constructor.
  • If Constructor may be overloaded the class have more than one constructors having different signature.
  • Constructor always declared public
  • Before learn constructor methods clear your all doubts in function.
According to the google Constructor Definitions:  A 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. It can be used to set initial values for object attributes. In Java, a constructor is a block of codes similar to the method.

Function of Signature/ Parameter are three types: 

  1.  Numbers of Parameter.
  2. Types of Parameter.
  3. Sequence parameter.

Example of Constructor :- 


An Simple Example:-
class Demo{
    int a, b, c;
    Demo(){ // Constructor // No return type but same same name as class name
        a=4;
        b=10;
        c=a+b;
        System.out.print("Addition of A and B:"+c);
    }
    public static void main(String arg[]){
        Demo obj = new Demo();

    }
}

----Output---- 


Addition of A and B: 14



Constructor are three Types  

  1. Default Constructor  ( no  any argument )
  2. Parameterized Constructor ( with argument)
  3. Copy Constructor (object copied into another object).

1.Default Constructor :  According to this constructor no any argument is known as default constructor. It is invoked at the time of creating object, and automatically call.

An Example Of Default Constructor :  Store Students details


import java.util.Scanner;
// package com.DataFlair.StringInput;  // Using for coomand line arguments package
import java.util.*;
class Student{
    int id;
    float marks;
    String name;
// Create a Constructor  with No return type but same name as class name  
    Student(){    
        Scanner sc= new Scanner(System.in);
        System.out.print("Enter Your id:");
        id=sc.nextInt();
        System.out.print("Enter Your Marks:");
        marks=sc.nextFloat();
        System.out.print("Enter Your Name: ");
        name=sc.next();

        System.out.println("Your Id is:"+id);
        System.out.println("Your Marks is:"+marks);
        System.out.println("Your Name is:"+name);
    }
    public static void main(String[] args){
        Student obj= new Student(); // here now just create object run code
    }
}



----Output---- 



Enter Your id:820487
Enter Your Marks:456
Enter Your Name: Shailesh
Your Id is:8200
Your Marks is:456.0
Your Name is:Shailesh



1.Default Constructor :  According to this constructor with parameter or argument known as Parameterized constructor.

An Example Of Parameterized Constructor :  Find EvenOdd Program

class EvenOdd{
    int num; // Data members
    EvenOdd(int num){  // No return type but same same name as class name
        Scanner eo= new Scanner (System.in);
        System.out.print("Enter any Number:");
        num= eo.nextInt();       // Get user value
        if(num %2==0){  
        System.out.println("THis is Even Number:");
        }
        else{
        System.out.println("THis is Odd Number");
        }
    }
        public static void main(String arg[]){
       
        EvenOdd obj = new EvenOdd(00);
// here now pass value anythig no matter but pass is complassary
}
}

----Output---- 

1st Run
Enter any Number:45 THis is Odd Number

2nd Run
Enter any Number:12
THis is Even Number:



1.Copy Constructor :  According to this constructor one object with parameter is copied into another object so it is known as Copy constructor.

In another word: A copy Constructor is an overloaded constructor used to declared and initialize an object from one to another object.

An Example Of Copy Constructor :  Find Area of rectangle


class Rectcopy{
    int area;
    int height;
    int width;
    Rectcopy(int height , int width){        // passing  arguments
    area = height*width;
    }
    void show(){
    System.out.println("Area of Ractangle:" +area);
    }
    public static void main(String args[]){
        Rectcopy obj1 = new Rectcopy(10,23);
        obj1.show();
        Rectcopy obj2=obj1;  // passing obj1 in obj2
        obj2.show();
    }
}


---Output---- 

1st Run :
Area of Ractangle:230
Area of Ractangle:230



Questions. find Factorial of any number get input by user. 


class Factorial{
    public
    int fact=1;
    Factorial( int number){
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter a number:");
        number= sc.nextInt();
        for(int i=1; i<=number; i++){
            fact = fact*i;
        }
}
        void show(){
        System.out.println("Your Factorial value is:"+ fact);
        }
    public static void main(String args[]){
    Factorial obj = new Factorial(0);         // create obj
    obj.show();
    Factorial obj1=obj;
    obj1.show();
    }
}


---Output---- 


Enter a number:5
Your Factorial value is:120
Your Factorial value is:120



I Hope this post is helpful for you thanks for watching.




No comments

Theme images by Dizzo. Powered by Blogger.