super


  • μ°Έμ‘°λ³€μˆ˜ super
    • 객체 μžμ‹ μ„ κ°€λ¦¬ν‚€λŠ” μ°Έμ‘°λ³€μˆ˜

    • λΆ€λͺ¨ 클래슀 멀버와 μžμ‹ 클래슀 멀버 이름이 같을 λ•Œ super ν‚€μ›Œλ“œ μ‚¬μš©ν•΄ ꡬ별

    • μΈμŠ€ν„΄μŠ€ λ©”μ„œλ“œμ—μ„œλ§Œ μ‚¬μš© κ°€λŠ₯ (클래슀 λ©”μ„œλ“œμ—μ„œ μ‚¬μš© X)


  • μƒμ„±μž super()
    • λΆ€λͺ¨ 클래슀의 μƒμ„±μžλ₯Ό ν˜ΈμΆœν•  λ•Œ μ‚¬μš©

    • λΆ€λͺ¨ 클래슀의 멀버λ₯Ό μ΄ˆκΈ°ν™”ν•˜κΈ° μœ„ν•΄μ„œλŠ” μžμ‹ 클래슀의 μƒμ„±μžμ—μ„œ λΆ€λͺ¨ 클래슀의 μƒμ„±μžκΉŒμ§€ ν˜ΈμΆœν•΄μ•Όν•¨ > λΆ€λͺ¨ν΄λž˜μŠ€μ˜ μƒμ„±μžλ₯Ό λͺ…μ‹œμ μœΌλ‘œ ν˜ΈμΆœν•˜μ§€ μ•ŠμœΌλ©΄ μžμ‹ 클래슀의 μƒμ„±μž 첫 쀄에 μ»΄νŒŒμΌλŸ¬κ°€ μžλ™μœΌλ‘œ λ””ν΄νŠΈ μƒμ„±μž μΆ”κ°€ > λΆ€λͺ¨ 클래슀의 멀버λ₯Ό μ΄ˆκΈ°ν™” ν•  수 μžˆλ„λ‘ 함

    • but, λΆ€λͺ¨ ν΄λž˜μŠ€μ— κΈ°λ³Έ μƒμ„±μž μ—†λ‹€λ©΄ 였λ₯˜ > λͺ…μ‹œμ μœΌλ‘œ 선언해야함


  • ex)
    • Customer.java

        public class Customer {
                  
            protected String customerName;
            protected String customerGrade;
            protected int bonusPoint;
                  
            public Customer(String customerName) {
                this.customerName = customerName;
                      
                customerGrade = "SILVER";
      
            }
                  
            public String showCustomerInfo() {
                return customerName + "λ‹˜μ˜ 등급은 " + customerGrade + 
                        "이며, λ³΄λ„ˆμŠ€ ν¬μΈνŠΈλŠ” " + bonusPoint + " μ μž…λ‹ˆλ‹€.";
            }
        }
      
    • VIPCustomer.java

        public class VIPCustomer extends Customer{
      
            public VIPCustomer(String customerName) {
                      
                super(customerName);
                      
                customerGrade = "VIP";
            }
                  
            public String showCustomerInfo() {
                return customerName + "λ‹˜μ˜ 등급은 " + customerGrade + 
                        "이며, λ³΄λ„ˆμŠ€ ν¬μΈνŠΈλŠ” " + bonusPoint + " μ μž…λ‹ˆλ‹€.";
            }
        }
      
    • Main.java

        public class Main {
      
            public static void main(String[] args) {
      
                Customer customerA = new Customer("λ°•ν•˜λŠ˜");
                customerA.bonusPoint = 1000;
                System.out.println(customerA.showCustomerInfo());
                      
                VIPCustomer customerB = new VIPCustomer("κΉ€λ°”λ‹€");
                customerB.bonusPoint = 15000;
                System.out.println(customerB.showCustomerInfo());
            }
        }
      

Categories:

Java