fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6. class Animal {
  7. String type;
  8.  
  9. // (1) 매개변수가 있는 부모 생성자
  10. Animal(String type) {
  11. this.type = type;
  12. System.out.println("Animal 생성자 호출됨: " + type);
  13. }
  14. }
  15.  
  16. class Dog extends Animal {
  17. String breed;
  18.  
  19. Dog(String type, String breed) {
  20. // (2) 부모 클래스의 생성자 호출
  21. // 이 문장이 Dog 생성자의 첫 번째 문장이어야 합니다.
  22. super(type);
  23.  
  24. // (3) 자식 클래스 고유의 초기화
  25. this.breed = breed;
  26. System.out.println("Dog 생성자 호출됨: " + breed);
  27. }
  28. }
  29. /* Name of the class has to be "Main" only if the class is public. */
  30. class Ideone
  31. {
  32. public static void main (String[] args) throws java.lang.Exception
  33. {
  34. // your code goes here
  35. Dog myDog = new Dog("포유류", "골든 리트리버");
  36. }
  37. }
Success #stdin #stdout 0.11s 57504KB
stdin
Standard input is empty
stdout
Animal 생성자 호출됨: 포유류
Dog 생성자 호출됨: 골든 리트리버