Skip to main content

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.




Comments

Popular posts from this blog

Assignment of ITA/ Information Technology and Application BCA- Technology369kk

Q1. What is  computer Explain basic computer architecture and Difference components.  2. Discuss the use of memory in computer system, Explain memory hierarchy  in details. 3. What is software? Explain difference types of software with explain. 4. Write short notes on the given:- (I) Internet. (II) LAN (Local area network ) (III) Search engine (IV) Web browser  Q 1.What is computer Explain basic computer architecture, Difference components of computer.   Computer :- Computer is defined as an electronic device that takes input data and instructions from the user and after processing them, it generates useful and desired output quickly.   A computer is designed to execute applications and provides a variety of solutions through integrated hardware and software components.                            It is fast and automatic device. It works with the help of programs and represents the d...

C++ and Java Practical All Questions Answers - BCA -Technology369kk

C++ and Java  In this post see most important questions for practical questions given by college all questions with answers . Guys I want to say that this is only for suggested post for your practical please request to you change same alphabets, words or anything  methods name and variables name because if you write all words same then this is copy paste for another peoples.  Used Topics:  Keywords, Variables, Condition Statements, Function , Array, Structure, Pointer.                           In OOPs, Class and Objects, Constructor, Poly morph, Encapsulation, Access Specifiers,                               Inheritance etc.  So, Without Time Lose Come to the Points, let's go start Now:        *************************************************************************  C++ 12 ...

Assignment of PMO (Principal of Management and Organization) - Technology369kk

 ** Assignment Of PMO ** Agenda: -  4 Questions discuss in this post. Question 1. Write a d etails note on selection why it Called. negative process.  Question 2. Write a details note on 'span of control. Question 3. Planning is an essential process, do you agree ? Discuss  Question 4. Write a note on management function. Q 1. Write a d etails note on selection why it called negative process.  Ans :-  Selection is the process of choosing the most suitable candidates out of the several candidates available.          Selection is a negative process because there may be more rejected then those selected in most of the candidates that is called selection is a negative process. → Selection process has the following steps:-  [ A .] Screening of applicants - Based on the screening of applicants only those candidates. It Called further process of selection. Who are found eligible for the job Standards of the the organization. [ B .] S...