-
[Eclipse] "Cannot instantiate the type OOO"트러블 슈팅(Trouble Shooting) 2022. 8. 15. 21:41
- DeskTop 클래스
public class DeskTop extends Computer { public void display() { System.out.println("DestTop display"); } public void typing() { System.out.println("DeskTop typing"); } }
- Computer 클래스
public abstract class Computer { public abstract void display(); public abstract void typing(); public void turnOn() { System.out.println("전원을 켭니다."); } public void turnOff() { System.out.println("전원을 끕니다."); } }
- ComputerTest 클래스
public class ComputerTest { public static void main(String[] args) { Computer computer = new DeskTop(); computer.display(); } }
Computer computer = new Computer();
=> ComputerTest 클래스에서 위 코드 입력 시 오류 발생, computer 클래스는 추상 클래스이기 때문
computer.display();
찍었을 때 불러올 메서드가 없음(구현이 안 되어 있기 때문)- 따라서 Computer는 인스턴스화 될 수 없음
- DeskTop은 인스턴스화 될 수 있는 이유는 상속에서 상위 클래스 타입의 변수로 하위 클래스의 인스턴스가 생성되어 대입될 수 있기 때문
- 묵시적 형 변환
- 추상 클래스 하나를 상속받은 여러 클래스를 동일한 타입으로 상위 클래스 타입으로 핸들링해서 많이 사용
- 위 오류는 OOO 클래스가 인스턴스를 만들 수 없는 Abstract(추상) 클래스이기 때문에 발생
- 따라서 슈퍼 생성자를 호출하거나 해당 클래스의 추상화를 제거하면 됨
- 추상 클래스는 new() 객체 생성이 불가능
- stack overflow 참조 : https://stackoverflow.com/questions/30317070/java-returns-error-cannot-instantiate-the-type
'트러블 슈팅(Trouble Shooting)' 카테고리의 다른 글
[MySQL] 계정 비밀번호 변경 및 에러 해결방법 (0) 2022.09.02 [MySQL Workbench] 테이블 세팅 시 PK, NN, UQ, BIN, ZF, AI, G 열 플래그 의미 (0) 2022.09.02 [Mac OS] M1 한영전환 딜레이 해결방법 (0) 2022.08.28 [Intellij] Cannot resolve symbol 'String' 에러 발생 (0) 2022.08.24 [Intellij] "o.s.b.d.LoggingFailureAnalysisReporter" 에러 해결 (0) 2022.08.22