Header Ads Widget

Ticker

6/recent/ticker-posts

What is Constructor In Java and Rules To Write a Constructor -HINDI

What is Constructor In Java and Rules To Write a Constructor -HINDI


What is Constructor definition-


Hindi में -हम जब भी कोई object बनाते हैं तब कुछ CODE Automatically executed होजाते हैं object को initialization करने केलिए .इसी  code को constructors कहते हैं .तो constructor का main objective होता है object को initialization करना .

English में -whenever we are creating an object some piece of the code will be executed automatically to perform initialization of an object this piece of the code is nothing but constructor .

Example-

class Student

{

String name;

int rollno;

Student(String name,int rollno)  //Constructor

{

this.name=name;

this.rollno=rollno;

}

public static void main(String[] args)

{

Student s1=new Student("harikrisna", 102);

Student s2=new Student("nimai", 104);

}

}

Rules to write Constructors-


constructor का नाम and class का नाम निश्चित रूप से same होना चाहिए .

constructor के पहले कुछ भी return type नही होना चाहिए नही तो वह constructor नही method होजायेगा .

Example-


class Test{

void Test(){  //इसमें जो return type वोयड है न वह नही रहना चाहिए constructor में
}

}

आप constructors केलिए 4 modifiers use कर सकते हैं वह है public,default,private and protected .यदि इसके अलाबा यदि और किसी modifier constructor में use करते हो तो आपको कोम्पिले time error देखायेगा .

Default Constructor


Hindi में -यदि आप कुछ भी constructor नही लिखते हो तो ,compiler एक constructor generate करता है उशे Default Constructor कहा जाता है .यदि आप एक भी constructor लिखते हो तो compiler default constructor नही generate करता है .लिकिन यह दोनों कभी भी एक साथ नही लिखा जा सकता है .

English में -if we are not writing at least one constructor then the compiler will generate default constructor .if we are  writing at least one constructor then compiler won't generate any default constructor .hence every  class contains either compiler generated constructors or, programmer written constructor but not both simultaneously .

Prototype Of Default Constructor-


यह हर time no argument constructor .

default constructor का modifier same होना चाहिए class modifier के साथ .

default constructor में super() रहेता है .

Corejava

Post a Comment

0 Comments