메서드명이 클래스명과 동일하고 리턴 자료형을 정의하지 않는 메서드
class HouseDog extends Dog {
// 생성자 코드 에시
HouseDog (String name) {
this.setName(name);
}
}
class Dog extends Animal {
// 디폴트 생성자
Dog() {
}
void sleep() {
System.out.println(this.name + " zzz");
}
}
생성자의 입력 항목이 없고 생성자 내부에 아무 내용이 없는 생성자를 디폴트 생성자라고 부른다.
만약 클래스에 생성자가 하나도 없다면 컴파일러는 자동으로 이와 같은 디폴트 생성자를 추가 한다.
하지만 사용자가 작성한 생성자가 하나라도 구현되어 있다면 컴파일러는 디폴트 생성자를 추가하지 않는다.
class HouseDog extends Dog {
HouseDog(String name) {
this.setName(name);
}
HouseDog() {}
HouseDog(int type) {
if (type == 1) {
this.setName("bulldog");
} else {
this.setName("jindo");
}
}
}
입력 항목이 다른 생성자를 여러 개 만들 수 있는데 이러한 방식을 생성자 오버로딩 (constructor overloading) 이라 한다.