logo

Preťaženie konštruktora v Jave

V Jave môžeme preťažiť konštruktory ako metódy. Preťaženie konštruktora možno definovať ako koncepciu mať viac ako jeden konštruktor s rôznymi parametrami, takže každý konštruktor môže vykonávať inú úlohu.

Zvážte nasledujúce Java program, v ktorom sme v triede použili rôzne konštruktory.

Príklad

 public class Student { //instance variables of the class int id; String name; Student(){ System.out.println('this a default constructor'); } Student(int i, String n){ id = i; name = n; } public static void main(String[] args) { //object creation Student s = new Student(); System.out.println('
Default Constructor values: 
'); System.out.println('Student Id : '+s.id + '
Student Name : '+s.name); System.out.println('
Parameterized Constructor values: 
'); Student student = new Student(10, 'David'); System.out.println('Student Id : '+student.id + '
Student Name : '+student.name); } } 

Výkon:

 this a default constructor Default Constructor values: Student Id : 0 Student Name : null Parameterized Constructor values: Student Id : 10 Student Name : David 

Vo vyššie uvedenom príklade je to trieda Študent konštruktér je preťažený dvoma rôznymi konštruktormi, t.j. predvoleným a parametrizovaným.

Tu musíme pochopiť účel preťaženia konštruktora. Niekedy potrebujeme použiť viacero konštruktorov na inicializáciu rôznych hodnôt triedy.

Musíme si tiež všimnúť, že kompilátor java vyvoláva predvolený konštruktor, keď v triede nepoužívame žiadny konštruktor. Predvolený konštruktor sa však nevyvolá, ak sme v triede použili akýkoľvek konštruktor, či už je predvolený alebo parametrizovaný. V tomto prípade kompilátor java vyvolá výnimku, že konštruktor nie je definovaný.

Zvážte nasledujúci príklad, ktorý obsahuje chybu, pretože objekt Colleges teraz nemožno vytvoriť pomocou predvoleného konštruktora, pretože ho neobsahuje.

 public class Colleges { String collegeId; Colleges(String collegeId){ this.collegeId = 'IIT ' + collegeId; } public static void main(String[] args) { // TODO Auto-generated method stub Colleges clg = new Colleges(); //this can't create colleges constructor now. } } 

Použitie tohto () pri preťažení konštruktora

Toto kľúčové slovo však môžeme použiť vo vnútri konštruktora, ktorý sa dá použiť na vyvolanie druhého konštruktora rovnakej triedy.

Zvážte nasledujúci príklad, aby ste pochopili použitie tohto kľúčového slova pri preťažení konštruktora.

 public class Student { //instance variables of the class int id,passoutYear; String name,contactNo,collegeName; Student(String contactNo, String collegeName, int passoutYear){ this.contactNo = contactNo; this.collegeName = collegeName; this.passoutYear = passoutYear; } Student(int id, String name){ this('9899234455', 'IIT Kanpur', 2018); this.id = id; this.name = name; } public static void main(String[] args) { //object creation Student s = new Student(101, 'John'); System.out.println('Printing Student Information: 
'); System.out.println('Name: '+s.name+'
Id: '+s.id+'
Contact No.: '+s.contactNo+'
College Name: '+s.contactNo+'
Passing Year: '+s.passoutYear); } } 

Výkon:

 Printing Student Information: Name: John Id: 101 Contact No.: 9899234455 College Name: 9899234455 Passing Year: 2018