logo

Przeciążanie konstruktora w Javie

W Javie możemy przeciążać konstruktory, takie jak metody. Przeciążanie konstruktora można zdefiniować jako koncepcję posiadania więcej niż jednego konstruktora o różnych parametrach, dzięki czemu każdy konstruktor może wykonać inne zadanie.

Rozważ następujące Jawa program, w którym w klasie użyliśmy różnych konstruktorów.

Przykład

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

Wyjście:

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

W powyższym przykładzie klasa Student konstruktor jest przeciążony dwoma różnymi konstruktorami, tj. domyślnym i sparametryzowanym.

W tym miejscu musimy zrozumieć cel przeciążania konstruktora. Czasami musimy użyć wielu konstruktorów, aby zainicjować różne wartości klasy.

Musimy również zauważyć, że kompilator Java wywołuje konstruktor domyślny, gdy nie używamy żadnego konstruktora w klasie. Jednakże konstruktor domyślny nie jest wywoływany, jeśli w klasie użyliśmy dowolnego konstruktora, niezależnie od tego, czy jest on domyślny, czy sparametryzowany. W tym przypadku kompilator Java zgłasza wyjątek informujący, że konstruktor jest niezdefiniowany.

Rozważmy następujący przykład, który zawiera błąd, ponieważ nie można teraz utworzyć obiektu Colleges przy użyciu domyślnego konstruktora, ponieważ go nie zawiera.

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

Użycie tego () w przeciążeniu konstruktora

Możemy jednak użyć tego słowa kluczowego wewnątrz konstruktora, którego można użyć do wywołania innego konstruktora tej samej klasy.

Rozważ następujący przykład, aby zrozumieć użycie tego słowa kluczowego w przeciążeniu konstruktora.

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

Wyjście:

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