상속


  • 이미 κ΅¬ν˜„λœ 클래슀λ₯Ό 상속(inheritance) λ°›μ•„ μ†μ„±μ΄λ‚˜ κΈ°λŠ₯을 ν™•μž₯ν•˜μ—¬ 클래슀λ₯Ό κ΅¬ν˜„

  • 이미 κ΅¬ν˜„λœ ν΄λž˜μŠ€λ³΄λ‹€ 더 ꡬ체적인 κΈ°λŠ₯을 가진 클래슀λ₯Ό κ΅¬ν˜„ν•΄μ•Ό ν•  λ•Œ κΈ°μ‘΄ 클래슀 상속

  • class B extends A{} : Bν΄λž˜μŠ€κ°€ A클래슀λ₯Ό μƒμ†λ°›μŒ

  • 단일 상속(single inheritance) : extends ν‚€μ›Œλ“œ λ’€μ—λŠ” 단 ν•˜λ‚˜μ˜ 클래슀만이 올 수 있음

    • μƒμœ„ ν΄λž˜μŠ€λŠ” ν•˜μœ„ ν΄λž˜μŠ€λ³΄λ‹€ 더 일반적인 κ°œλ…κ³Ό κΈ°λŠ₯
    • ν•˜μœ„ ν΄λž˜μŠ€λŠ” μƒμœ„ ν΄λž˜μŠ€λ³΄λ‹€ 더 ꡬ체적인 κ°œλ…κ³Ό κΈ°λŠ₯
    • ν•˜μœ„ ν΄λž˜μŠ€κ°€ μƒμœ„ 클래슀의 속성과 κΈ°λŠ₯을 ν™•μž₯(extends)


  • ex)
    • Customer.java

        public class Customer {
      
            //ν•˜μœ„ν΄λž˜μŠ€μ—μ„œ μ ‘κ·Όν•  수 μžˆλ„λ‘ protected
            protected String customerName;
            protected String customerGrade;
            int bonusPoint;
            double bonusRatio;
      
            public String getCustomerName() {
                return customerName;
            }
      
            public void setCustomerName(String customerName) {
                this.customerName = customerName;
            }
      
            public String getCustomerGrade() {
                return customerGrade;
            }
      
            public void setCustomerGrade(String customerGrade) {
                this.customerGrade = customerGrade;
            }
      
                  
            public Customer() {
                customerGrade = "SILVER";
                bonusRatio = 0.01;
            }
                  
            public String showCustomerInfo() {
                return customerName + "λ‹˜μ˜ 등급은 " + customerGrade + 
                        "이며, λ³΄λ„ˆμŠ€ ν¬μΈνŠΈλŠ” " + bonusPoint + "점 μž…λ‹ˆλ‹€.";
            }
                  
        }
      
    • VIPCustomer.java

        public class VIPCustomer extends Customer{
                  
            double salesRatio; //ν• μΈμœ¨
            private String agentId; //λ‹΄λ‹Ή 상담원
                  
            public String getAgentId() {
                return agentId;
            }
      
            public void setAgentId(String agentId) {
                this.agentId = agentId;
            }
      
            public VIPCustomer() {
                customerGrade = "VIP";
                bonusRatio = 0.05;
                salesRatio = 0.1;
            }
        }
      
    • Main.java

        public class Main {
      
            public static void main(String[] args) {
      
                Customer customerA = new Customer();
                customerA.setCustomerName("λ°•ν•˜λŠ˜");
                customerA.bonusPoint = 1000;
                System.out.println(customerA.showCustomerInfo());
                      
                VIPCustomer customerB = new VIPCustomer();
                customerB.setCustomerName("κΉ€λ°”λ‹€");
                customerB.bonusPoint = 15000;
                System.out.println(customerB.showCustomerInfo());
                //Customer클래슀 μ•ˆ λ©”μ„œλ“œμ΄μ§€λ§Œ 상속 λ°›μ•„ μ‚¬μš© κ°€λŠ₯
            }
      
        }
      
    • 좜λ ₯κ²°κ³Ό 좜λ ₯κ²°κ³Ό

Categories:

Java