logo

Sovraccarico del costruttore in Java

In Java, possiamo sovraccaricare i costruttori come i metodi. L'overload del costruttore può essere definito come il concetto di avere più di un costruttore con parametri diversi in modo che ogni costruttore possa eseguire un compito diverso.

Considera quanto segue Giava programma, in cui abbiamo utilizzato diversi costruttori nella classe.

Esempio

 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); } } 

Produzione:

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

Nell'esempio precedente, la classe Student costruttore è sovraccaricato con due diversi costruttori, ovvero default e parametrizzato.

Qui dobbiamo comprendere lo scopo dell'overload del costruttore. A volte è necessario utilizzare più costruttori per inizializzare i diversi valori della classe.

Dobbiamo anche notare che il compilatore Java invoca un costruttore predefinito quando non utilizziamo alcun costruttore nella classe. Tuttavia, il costruttore predefinito non viene richiamato se nella classe è stato utilizzato un costruttore, sia esso predefinito o parametrizzato. In questo caso, il compilatore Java lancia un'eccezione dicendo che il costruttore non è definito.

converte un int in una stringa java

Considera l'esempio seguente, che contiene l'errore poiché l'oggetto Colleges non può essere creato utilizzando il costruttore predefinito ora poiché non ne contiene uno.

 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. } } 

Uso di this () nell'overload del costruttore

Tuttavia, possiamo utilizzare questa parola chiave all'interno del costruttore, che può essere utilizzata per invocare l'altro costruttore della stessa classe.

Considera l'esempio seguente per comprendere l'uso di questa parola chiave nell'overload del costruttore.

 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); } } 

Produzione:

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