Tiny Bunny
λ³Έλ¬Έ λ°”λ‘œκ°€κΈ°
πŸ’»/JAVA

[JAVA] 상속 - super, μΈν„°νŽ˜μ΄μŠ€(Interface), μ˜€λ²„λΌμ΄λ”©(Overriding), μΆ”μƒν΄λž˜μŠ€(Abstract Class)

by soonybutter 2024. 8. 30.
728x90

μžλ°”(JAVA)λŠ” 객체지ν–₯ν”„λ‘œκ·Έλž˜λ°(Object Oriented Programing, OOP)으둜, 4가지 νŠΉμ„±μ„ 가진닀.

1. 상속 (Inheritance)

2. λ‹€ν˜•μ„± (Polymorphism)

3. 좔상화 (Abstraction)

4. μΊ‘μŠν™” (Encapsulation)

 

이 쀑 상속과 μžλ°”μ˜ 닀쀑상속(μΈν„°νŽ˜μ΄μŠ€)에 λŒ€ν•΄ μ •λ¦¬ν•΄λ³΄μž.

1. 상속
1-1. μ˜€λ²„λΌμ΄λ”© @Override
1-2. super 

2. μΈν„°νŽ˜μ΄μŠ€
3. μΆ”μƒν΄λž˜μŠ€

 

 

 

 

1. 상속
  •  λΆ€λͺ¨ 클래슀(슈퍼클래슀)의 속성과 κΈ°λŠ₯을 κ·ΈλŒ€λ‘œ λ¬Όλ €λ°›λŠ”λ‹€.
  • extends ν‚€μ›Œλ“œλ₯Ό μ‚¬μš©ν•œλ‹€.
  • extends λŒ€μƒμ€ ν•˜λ‚˜λ§Œ 선택할 수 μžˆλ‹€. (ν•˜λ‚˜μ˜ λΆ€λͺ¨ν΄λž˜μŠ€λ§Œ 상속받을 수 μžˆλ‹€! ) 🌟
  • μžμ‹μ΄ 상속을 λ°›κ²Œ 되면, λ‹¨μˆœνžˆ λΆ€λͺ¨μ˜ ν•„λ“œμ™€ λ©”μ„œλ“œλ§Œ λ¬Όλ €λ°›λŠ”κ²Œ μ•„λ‹Œ, λΆ€λͺ¨μ˜ ν΄λž˜μŠ€λ„ ν•¨κ»˜ ν¬ν•¨λ˜μ–΄ μƒμ„±λœλ‹€.
  • μƒμ†κ΄€κ³„μ˜ 객체λ₯Ό μƒμ„±ν•˜λ©΄, μ™ΈλΆ€μ—μ„œ λ³Όλ•ŒλŠ” ν•˜λ‚˜μ˜ μΈμŠ€ν„΄μŠ€λ₯Ό μƒμ„±ν•˜λŠ” 것 처럼 λ³΄μ΄λ‚˜, μ‹€μ œ λ‚΄λΆ€μ—μ„œλŠ” λΆ€λͺ¨μ™€ μžμ‹μ΄ λͺ¨λ‘ μƒμ„±λ˜λ©° 각각 곡간이 κ΅¬λΆ„λœλ‹€.

 λ§Œμ•½, carλΌλŠ” λΆ€λͺ¨ 클래슀 μ•„λž˜μ— ElectricCar,GasCar, HydrogenCar μ΄λΌλŠ” 세가지 μžμ‹ ν΄λž˜μŠ€κ°€ μ‘΄μž¬ν•  λ•Œμ˜ μ˜ˆμ‹œλ‹€.

 

-λΆ€λͺ¨ 클래슀 Car 

package extends1.ex3;

public class Car
{
    public void move()
    {

        System.out.println("μ°¨λ₯Ό μ΄λ™ν•©λ‹ˆλ‹€.");
    }
    
    public void charge(){

    }
}

 

-μžμ‹ 클래슀 ElectricCar

package extends1.ex3;

public class ElectricCar extends Car
{
    @Override public void charge()
    {

        System.out.println("μΆ©μ „ν•©λ‹ˆλ‹€.");

    }

}

 

-μžμ‹ 클래슀 GasCar

package extends1.ex3;

public class GasCar extends Car
{
    @Override public void charge()
    {
        System.out.println("기름을 μ£Όμœ ν•©λ‹ˆλ‹€.");
    }
}

 

-μžμ‹ 클래슀 HydrogenCar

package extends1.ex3;

public class HydrogenCar extends Car{


    @Override public void charge()
    {
        System.out.println("μˆ˜μ†Œλ₯Ό μΆ©μ „ν•©λ‹ˆλ‹€.");
    }
}

 

μ΄λ ‡κ²Œ ν•˜λ‚˜μ˜ λΆ€λͺ¨ 클래슀 μ•„λž˜μ— μ„Έκ°œμ˜ μžμ‹ ν΄λž˜μŠ€κ°€ μžˆλŠ” μ…ˆμ΄λ‹€.

- CarMainμ—μ„œλŠ” 각각의 μžμ‹ ν΄λž˜μŠ€λ“€μ˜ 객체λ₯Ό μƒμ„±ν•˜μ—¬, λΆ€λͺ¨ 클래슀 λ‚΄λΆ€μ˜ move()λ₯Ό ν˜ΈμΆœν•˜κ³  μžˆμŒμ„ 확인할 수 μžˆλ‹€.

-κ°κ°€μ˜ μžμ‹ν΄λž˜μŠ€μ—μ„œλŠ” λΆ€λͺ¨ ν΄λž˜μŠ€μ—μ„œ μ„ μ–Έλ§Œ ν•΄λ‘” ν•¨μˆ˜ 'charge()' λ₯Ό 각각의 μžμ‹ν΄λž˜μŠ€μ—μ„œ

@Overrideν•΄μ„œ  μ •μ˜ν–ˆκΈ° λ•Œλ¬Έμ—,  κ°κ°μ˜μžμ‹ 클래슀 객체둜 ν˜ΈμΆœν•œ charge()λŠ” 각각 μ •μ˜ν•œ λ‚΄μš©μ΄ 좜λ ₯λœλ‹€.

 

[메인문]

package extends1.ex3;

public class CarMain
{
    public static void main(String[] args)
    {
        ElectricCar electricCar=new ElectricCar();
        electricCar.move();
        electricCar.charge();

        GasCar gasCar=new GasCar();
        gasCar.move();
        gasCar.charge();

        HydrogenCar hydrogenCar = new HydrogenCar();
        hydrogenCar.move();
        hydrogenCar.charge();

    }
}

 

 

 


1-1. @Override μ˜€λ²„λΌμ΄λ”©

 

 

λ©”μ„œλ“œ μ˜€λ²„λΌμ΄λ”©(Overriding)

  • λΆ€λͺ¨μ—κ²Œμ„œ 상속받은 κΈ°λŠ₯을 μžμ‹ ν΄λž˜μŠ€μ—μ„œ μž¬μ •μ˜ν•˜λŠ” 것을 λ§ν•œλ‹€.
  • 이미 λΆ€λͺ¨ν΄λž˜μŠ€μ—μ„œ μ •μ˜λ˜μ–΄μžˆλŠ” λ©”μ„œλ“œμ—¬λ„ 상속받은 μžμ‹μ€ 'μž¬μ •μ˜'κ°€ κ°€λŠ₯ν•˜λ‹€.
  • @Override λŠ” μƒμœ„ν΄λž˜μŠ€μ˜ λ©”μ„œλ“œλ₯Ό ν•΄λ‹Ή ν΄λž˜μŠ€κ°€ μ •μ˜ν•¨μ„ λ‚˜νƒ€λ‚΄λŠ” μ• λ…Έν…Œμ΄μ…˜μ΄λ‹€. (ν‘œκΈ°ν•΄μ€˜μ•Όν•¨!)

μœ„μ— 각각의 μžμ‹ 클래슀(ElectricCar, GasCar, HydrogenCar)λ“€μ—μ„œ λΆ€λͺ¨ 클래슀의 charge() λ©”μ„œλ“œλ₯Ό 'μ •μ˜ν•΄μ€„λ•Œ'

@Override λ₯Ό λͺ…μ‹œν•΄μ€€ 것을 확인할 수 μžˆλ‹€.

 

 


 

1-2. super

 

super ν‚€μ›Œλ“œ

  • λΆ€λͺ¨μ™€ μžμ‹μ˜ ν•„λ“œλͺ…(λ©€λ²„λ³€μˆ˜)이 κ°™κ±°λ‚˜ λ©”μ„œλ“œκ°€ μ˜€λ²„λΌμ΄λ”© λΌμžˆλŠ” 경우 -> μžμ‹μ€ λΆ€λͺ¨μ˜ ν•„λ“œ,λ©”μ„œλ“œλ₯Ό ν˜ΈμΆœν•  수 μ—†λ‹€.
  • super ν‚€μ›Œλ“œλ₯Ό μ‚¬μš©ν•˜λ©΄ λΆ€λͺ¨μ˜ λ©”μ„œλ“œ ν˜Ήμ€ ν•„λ“œλͺ…을 가져와 μ°Έμ‘° ν•  수 μžˆμ–΄μ§„λ‹€.

 

μ•„λž˜λŠ” Parent클래슀λ₯Ό μƒμ†λ°›λŠ” μžμ‹ 클래슀 Child ν΄λž˜μŠ€μ΄λ‹€.

package extends1.super1;

public class Child extends Parent{

    public String value ="child";

    @Override
    public void hello(){

        System.out.println("child, hello! ");
    }

    public void call(){
        System.out.println("this value= "+ this.value); //ν˜„μž¬ μžκΈ°μžμ‹ μ˜ value
        System.out.println("super value= "+ super.value); //λ‚΄ λΆ€λͺ¨μ˜ value

        this.hello();
        super.hello();

    }

}

 

μ•„λž˜λŠ” λΆ€λͺ¨ν΄λž˜μŠ€μΈ Parentν΄λž˜μŠ€λ‹€.

package extends1.super1;

public class Parent {

    public String value = "parent";

    public void hello(){
        System.out.println("hello, parent");

    }

}

 

이 두 ν΄λž˜μŠ€λŠ” 각각 λ©€λ²„λ³€μˆ˜λ‘œ λ³€μˆ˜μ΄λ¦„μ΄ value둜 λ™μΌν•œ λ³€μˆ˜λ₯Ό 가지고 있으며,

λΆ€λͺ¨ 클래슀의 λ©”μ„œλ“œ hello() λ©”μ„œλ“œλ₯Ό μžμ‹ν΄λž˜μŠ€κ°€ μ˜€λ²„λΌμ΄λ”© ν•˜κ³  μžˆλ‹€.

λ™μΌν•œ λ³€μˆ˜μ™€ λ©”μ„œλ“œλ₯Ό 가지고 μžˆλŠ” 상황이닀.

 

μžμ‹ 클래슀 내뢀에 call() μ΄λΌλŠ” λ©”μ„œλ“œλ‚΄μ—μ„œ

this.valueλ₯Ό ν•˜λ©΄, μžμ‹ 클래슀 λ‚΄λΆ€μ˜ λ©€λ²„λ³€μˆ˜ valueλ₯Ό 가리킀고,

super.valueλ₯Ό ν•˜λ©΄, λΆ€λͺ¨ 클래슀 λ‚΄λΆ€μ˜ λ©€λ²„λ³€μˆ˜ valueλ₯Ό κ°€λ¦¬ν‚€λŠ” 것을 확인할 수 μžˆλ‹€.

 

κ·Έ μ•„λž˜μ— hello() λ©”μ„œλ“œλ₯Ό κ°€λ¦¬ν‚€λŠ” 것도 λ§ˆμ°¬κ°€μ§€μ΄λ‹€.

this.hello()λŠ” μžμ‹μ΄ μ˜€λ²„λΌμ΄λ”©ν•œ  hello() ,

super.hello()λŠ” λΆ€λͺ¨μ˜ λ©”μ„œλ“œ hello()λ₯Ό 가리킨닀.

 

 

 


 

 

2. μΈν„°νŽ˜μ΄μŠ€(Interface)

 

μžλ°”μ—μ„œ extendsλ₯Ό μ΄μš©ν•œ λΆ€λͺ¨ 클래슀 상속은 ν•˜λ‚˜μ˜ 클래슀만 상속할 수 μžˆλ‹€λŠ” νŠΉμ„±μ„ 가지기 λ•Œλ¬Έμ—,

μΈν„°νŽ˜μ΄μŠ€(Interface)λ₯Ό μ‚¬μš©ν•˜μ—¬ μ—¬λŸ¬ 클래슀λ₯Ό 닀쀑상속 받을 수 μžˆλ‹€.

 

 

μΈν„°νŽ˜μ΄μŠ€(Interface)

  • λ‚΄λΆ€ λͺ¨λ“  λ©”μ„œλ“œλŠ” public abstract 둜 μ •μ˜ν•œλ‹€. (μƒλž΅κ°€λŠ₯)
  • λ‚΄λΆ€ λͺ¨λ“  ν•„λ“œλŠ” public static final μƒμˆ˜λ‘œ μ •μ˜ν•œλ‹€. (μƒλž΅κ°€λŠ₯)
  • implements ν‚€μ›Œλ“œλ‘œ κ΅¬ν˜„ν•œλ‹€.
  • 였직 'μΆ”μƒλ©”μ„œλ“œ'(λ©”μ„œλ“œ)와 'μƒμˆ˜'(ν•„λ“œ)λ§Œμ„ λ©€λ²„λ‘œ κ°€μ§ˆ 수 μžˆλ‹€.
  • μΈν„°νŽ˜μ΄μŠ€λŠ” μΈν„°νŽ˜μ΄μŠ€λ‘œλΆ€ν„°λ§Œ 상속받을 수 μžˆλ‹€.
  • μ—¬λŸ¬κ°œμ˜ μΈν„°νŽ˜μ΄μŠ€λ‘œλΆ€ν„° μƒμ†λ°›λŠ” 것이 κ°€λŠ₯ν•˜λ‹€.

🌟 λͺΈν†΅μ΄ μžˆλŠ” λ©”μ„œλ“œμ™€ λ©€λ²„λ³€μˆ˜λ₯Ό κ°€μ§ˆ 수 μ—†λ‹€. 

= μ„ μ–Έλ§Œ ν•΄μ€€ ν˜•νƒœμ˜ λ©”μ„œλ“œ, λ©€λ²„λ³€μˆ˜λ₯Ό 가져야함.

 

🌟 좔상 λ©”μ„œλ“œμ˜ λ©”μ„œλ“œμ˜ λͺΈν†΅μ„ λ§Œλ“€μ–΄μ€„ 클래슀λ₯Ό λ”°λ‘œ μž‘μ„±ν•΄μ•Όν•œλ‹€.

 

 

[μΈν„°νŽ˜μ΄μŠ€ Base ]

package com.poly.test03;

//μΈν„°νŽ˜μ΄μŠ€ interface
public interface Base {
	
	public void bark();
	public abstract void eat(String ani); //public abstractκ°€ μ•žμ— μƒλž΅κ°€λŠ₯
	
	

}

 

[μΈν„°νŽ˜μ΄μŠ€ Test ]

package com.poly.test03;

public interface Test {
	

}

 

[Cat ν΄λž˜μŠ€μ—μ„œ interface Base와 Test을 상속받고(닀쀑상속) μ •μ˜]

package com.poly.test03;

public class Cat implements Base, Test{
	@Override
	public void bark() {
		System.out.println("μ•Όμ˜Ή");
	}
	
	@Override
	public void eat(String ani) {
		System.out.println(ani + "λ¨ΉλŠ”λ‹€");
		
	}
	

}

 

 

[Dog ν΄λž˜μŠ€μ—μ„œ interface Base와 Test을 상속받고(닀쀑상속) μ •μ˜]

package com.poly.test03;


public class Dog implements Base,Test{
	@Override
	public void bark() {
		System.out.println("멍멍");
	}
	
	@Override
	public void eat(String t) {
		System.out.println(t + "λ¨ΉλŠ”λ‹€");
		
	}

}

 

 

[메인 λ©”μ„œλ“œμ—μ„œ 객체 생성]

  • Interface base λŠ” Dogκ³Ό Catκ°€ μΈν„°νŽ˜μ΄μŠ€λ‘œ κ΅¬ν˜„ν•œ 클래슀의 쑰상이닀. λ”°λΌμ„œ ν•΄λ‹Ή μΈν„°νŽ˜μ΄μŠ€ νƒ€μž…μ˜ μ°Έμ‘°λ³€μˆ˜λ‘œ 이λ₯Ό κ΅¬ν˜„ν•œ 클래슀의 μΈμŠ€ν„΄μŠ€λ₯Ό μ°Έμ‘°ν•  수 μžˆλ‹€.
  • λ˜ν•œ μΈν„°νŽ˜μ΄μŠ€μ˜ νƒ€μž…μœΌλ‘œ ν˜•λ³€ν™˜λ„ κ°€λŠ₯ν•˜λ‹€.

 

package com.poly.test03;

public class MTest {

	public static void main(String[] args) {
		
		Base b = new Cat();
		b.bark();
		b.eat("참치");
		
		b = new Dog();
		b.bark();
		b.eat("μ‚¬λ£Œ");

	}

}

 

μ‹€ν–‰ κ²°κ³Ό

 

 


 

 

 

 

3. 좔상 클래슀(Abstract Class)

 

μΆ”μƒν΄λž˜μŠ€ (Abstract Class)

  • 좔상 ν΄λž˜μŠ€λŠ” λΆ€λͺ¨ν΄λž˜μŠ€λ₯Ό μƒμ†ν•˜μ—¬ λΆ€λͺ¨ ν΄λž˜μŠ€κ°€ 가진 κΈ°λŠ₯듀을 κ΅¬ν˜„ν•˜λŠ” 경우, μ‚¬μš©ν•œλ‹€.
  • non-static, non-final ν•„λ“œ 선언이 ν•„μš”ν•œ 경우
  • 각 λ©”μ„œλ“œλ“€μ˜ {} μ•ˆμ„ μ •μ˜ν•œ 것을 μƒμ†ν•˜κ³  μ‹Άμ„λ•Œ 
  • 상속할 κ°μ²΄λ“€μ˜ 곡톡점(λ³€μˆ˜νƒ€μž…)을 μ°Ύμ•„ μΆ”μƒν™”μ‹œμΌœλ†“μ€ 것이닀. λ”°λΌμ„œ μ€‘λ³΅λ˜λŠ” 멀버듀을 톡합 ν•  μˆ˜λ„ μžˆλ‹€. (μΈν„°νŽ˜μ΄μŠ€λŠ” λΆˆκ°€λŠ₯ν•˜λ‹€.)

 

 

 

[μ˜ˆμ‹œ]

class NewlecExam {
    int kor; // μ€‘λ³΅λ˜λŠ” 멀버
    int eng; // μ€‘λ³΅λ˜λŠ” 멀버
    int math; // μ€‘λ³΅λ˜λŠ” 멀버
    int com;

    void total(){} // μ€‘λ³΅λ˜λŠ” 멀버
    void avg(){} // μ€‘λ³΅λ˜λŠ” 멀버
}

class YBMExam{
    int kor; // μ€‘λ³΅λ˜λŠ” 멀버
    int eng; // μ€‘λ³΅λ˜λŠ” 멀버
    int math; // μ€‘λ³΅λ˜λŠ” 멀버
    int toeic;

    void total(){} // μ€‘λ³΅λ˜λŠ” 멀버
    void avg(){} // μ€‘λ³΅λ˜λŠ” 멀버
}

 

μœ„ μ½”λ“œμ™€ 같이 각각의 κ³Όλͺ©λ“€κ³Ό λ©”μ„œλ“œκ°€ μ€‘λ³΅λ˜λŠ” μƒν™©μΌλ•Œ,

1. 곡톡적인 κ΅­,영,수 멀버 ν•„λ“œλ₯Ό 상속을 톡해 μƒμœ„ 클래슀둜 λ¬ΆλŠ”λ‹€.

2. λ©”μ„œλ“œλ₯Ό ν†΅ν•©ν•˜κ³  좔상화(Abstract) ν•΄μ„œ λ‹€λ₯Έ μ’…λ₯˜μ˜ 클래슀λ₯Ό 좔가해도 효율적으둜 μ‚¬μš©ν•  수 μžˆλ‹€.

 

 

[톡합 μ˜ˆμ‹œ]

abstract class Exam {
    int kor;
    int eng;
    int math;

    abstract void total();
    abstract void avg();
}

class NewlecExam extends Exam {
    int com;

    void total(){}
    void avg(){}
}

class YBMExam extends Exam {
    int toeic;

    void total(){}
    void avg(){}
}

 

-μ½”λ“œ μ˜ˆμ‹œ 좜처: [Inpa Dev πŸ‘¨‍πŸ’»:ν‹°μŠ€ν† λ¦¬]

 

 

 

 

 

 

 

 

 

 


*κ³΅λΆ€ν•˜κΈ° μ’‹μ•˜λ˜ μ˜ˆμ‹œ

 

 

 

 

 

public abstract class Animal {
	
	public abstract void bark();
	
	public void eat(String ani) {
		System.out.println(ani + "λ¨ΉλŠ”λ‹€");
		
	}
	

}

 


public interface Bird {

	void fly();
	
}

 


public class Cat extends Animal {
	@Override
	public void bark() {
		System.out.println("μ•Όμ˜Ή");
	}
	
	@Override
	public void eat(String feed){
		System.out.println("고양이가"+ feed+"λ¨ΉλŠ”λ‹€!");
	}
	
}

 

 


public class Dog extends Animal {
	@Override
	public void bark() {
		System.out.println("멍멍");
	}
	
	public void bite() {
		System.out.println("κΉ¨λ¬Όλ‹€..");
	}
	
	@Override
	public void eat(String feed){
		System.out.println("강아지가"+ feed+"λ¨ΉλŠ”λ‹€!");
	}

}

 

 

 

public class Pigeon extends Animal implements Bird {
	
	@Override
	public void bark() {
		System.out.println("9999999");
		
	}
	
	
	@Override
	public void fly() {
		
		System.out.println("파λ‹₯파λ‹₯");
		
	}

}

 

 

public class MTest {

	public static void main(String[] args) {
		
	    Cat cat = new Cat();
		cat.bark();
		cat.eat("참치");
		
		Dog dog = new Dog();
		dog.bark();
		dog.eat("개껌");
		dog.bite();
		
	
		Pigeon p = new Pigeon();
		p.bark();
		p.eat("λΉ΅");
		p.fly();
		
		

	}

}

 

 

 

 

 


 

 

 

 

 

 

ν‹€λ¦° λΆ€λΆ„μ΄λ‚˜ μ΄μƒν•œ λΆ€λΆ„이 μžˆμœΌλ©΄ λŒ“κΈ€λ‘œ μ§€μ ν•΄μ£Όμ„Έμš”! 

κ°μ‚¬ν•©λ‹ˆλ‹€ :)

728x90

TOP

Designed by ν‹°μŠ€ν† λ¦¬