ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • JAVA ์ž๋ฐ” ์ƒ์†, ์ ‘๊ทผ์ง€์ •์ž
    JAVA 2020. 8. 6. 00:28

    ๐ŸŽฏ ์ž๋ฐ” ์ƒ์†, superํ‚ค์›Œ๋“œ, ์ ‘๊ทผ์ง€์ •์ž๋ฅผ ์•Œ์•„๋ณธ๋‹ค.

     

     

     

    ํด๋ž˜์Šค๋“ค์˜ ๊ด€๊ณ„

     

    has a ๊ด€๊ณ„ : ํ•œ ๊ฐ์ฒด๊ฐ€ ๋‹ค๋ฅธ ๊ฐ์ฒด๋ฅผ ํฌํ•จํ•˜๋Š” ๊ด€๊ณ„์ด๋‹ค. ํ”ํžˆ ์‚ฌ์šฉ๋œ๋‹ค.

    is a ๊ด€๊ณ„ : ๋น„์Šทํ•œ ์†์„ฑ ๋ฐ ๋™์ž‘์„ ๊ฐ€์ง„ ๊ฐ์ฒด๋ฅผ ํฐ ๊ฐœ๋…๊ณผ ์ž‘์€ ๊ฐœ๋…์œผ๋กœ ์„ค์ •ํ•œ ๊ด€๊ณ„์ด๋‹ค. ์ด๋“ค์€ ๊ณตํ†ต์ ์„ ๊ฐ€์กŒ๋‹ค.

     

     

    ์ƒ์†

    public class ์ž์‹ํด๋ž˜์Šค extends ๋ถ€๋ชจํด๋ž˜์Šค { }

    ๊ฐ์ฒด๋“ค ๊ฐ„ is a ๊ด€๊ณ„๊ฐ€ ์„ฑ๋ฆฝ๋˜๋ฉด ์ƒ์†์„ ์ ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ฐ์ฒด๋“ค ๊ฐ„์˜ ๊ณตํ†ต์ ์„ ์ถ”์ถœํ•ด์„œ ์ƒ์œ„๊ฐœ๋… ๊ฐ์ฒด๋กœ ๋งŒ๋“ค๊ณ  ํ•˜์œ„ ๊ฐ์ฒด์—์„œ ํ•„์š”ํ•œ ๊ฒƒ๋“ค์„ ์ƒ์†๋ฐ›์•„ ์‚ฌ์šฉํ•˜๋ฉด ์ค‘๋ณต์ œ๊ฑฐ์™€ ์žฌ์‚ฌ์šฉ์„ฑ ํšจ๊ณผ๋ฅผ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

    ๋ถ€๋ชจํด๋ž˜์Šค์˜ ๋ชจ๋“  ๋ฉค๋ฒ„(์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜, ๋ฉ”์†Œ๋“œ)๋ฅผ ์ž์‹ํด๋ž˜์Šค์—์„œ ์„ ์–ธ ์—†์ด ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ ์ž์‹ํด๋ž˜์Šค๋Š” ๋‹จ ํ•˜๋‚˜์˜ ๋ถ€๋ชจํด๋ž˜์Šค๋งŒ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋‹ค.

     

    class Employee{
    
    	String name;
    	int salary;
    
    	public String getEmployee() {
    		return name + " " + salary;
    	}
    
    	public Employee(){
    		System.out.println("Employee ๋ถ€๋ชจ ๊ธฐ๋ณธ์ƒ์„ฑ์ž ํ˜ธ์ถœ=========");
    	}
    	public Employee(String name, int salary) {
    		this.name = name;
    		this.salary = salary;
    	}
    }
    
    class Manager extends Employee{
    	String depart;
    	public String getManager() {
    		return name + " " + salary + " "+depart;
    	}
    	public Manager(String name, int salary,String depart) {
    		super(); //๋ถ€๋ชจ ๊ธฐ๋ณธ ์ƒ์„ฑ์ž ํ˜ธ์ถœ
    		this.name=name;
    		this.salary = salary;
    		this.depart = depart;
    		System.out.println("Manager ์ž์‹์ƒ์„ฑ์ž ํ˜ธ์ถœ=========");
    	}
    }
    	
    public class Ex06_1 {
    	public static void main(String[] args) {
    		Employee emp = new Employee("ํ™๊ธธ๋™", 2000);
    		Manager man = new Manager("์ด์ˆœ์‹ ", 4000, "๊ฐœ๋ฐœ");
    		System.out.println(emp.getEmployee());
    		System.out.println(man.getManager());
    	}
    
    }

    Manager ํด๋ž˜์Šค๋Š” Employee ํด๋ž˜์Šค์— ์ƒ์†๋œ๋‹ค. ๋”ฐ๋ผ์„œ ๋ถ€๋ชจํด๋ž˜์Šค์˜ ๋ณ€์ˆ˜๋ฅผ ์„ ์–ธ ์—†์ด ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ํ•˜์ง€๋งŒ ์ƒ์„ฑ์ž๋Š” ์ž์‹ํด๋ž˜์Šค์— ์ƒ์†๋˜์ง€ ์•Š๋Š”๋‹ค. ๋”ฐ๋ผ์„œ ํ˜ธ์ถœ์„ ํ†ตํ•ด ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. 

    source - Generate constructors from superclass...
    ๊ธฐ๋ณธ์ƒ์„ฑ์ž์™€ super() ์ž๋™์ƒ์„ฑ

    ์ดํด๋ฆฝ์Šค ๋„๊ตฌ๋กœ ๊ธฐ๋ณธ ์ƒ์„ฑ์ž๋ฅผ ๋งŒ๋“ค์–ด๋ณด๋ฉด ์ž์‹ํด๋ž˜์Šค์˜ ์ƒ์„ฑ์ž ์ฒซ ๋ผ์ธ์— super()๋กœ ๋ถ€๋ชจ์˜ ๊ธฐ๋ณธ์ƒ์„ฑ์ž ํ˜ธ์ถœํ•จ์ˆ˜๋ฅผ ์ž๋™์œผ๋กœ ๋ถˆ๋Ÿฌ์˜จ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค. ๋ถ€๋ชจํด๋ž˜์Šค๊ฐ€ ๋จผ์ € ์ƒ์„ฑ๋˜์–ด์•ผ ๋ถ€๋ชจ์˜ ๋ฉค๋ฒ„๋ฅผ ์ž์‹์ด ์ด์šฉํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๋ถ€๋ชจํด๋ž˜์Šค์˜ ๋ฉค๋ฒ„๋ฅผ ์‚ฌ์šฉํ•  ๋• ์ฒซ๋ผ์ธ์— super()๋ฅผ ๋ช…์‹œํ•ด์„œ ๋ถ€๋ชจํด๋ž˜์Šค์˜ ๊ฐ์ฒด๋ฅผ ๋จผ์ € ์ƒ์„ฑํ•œ๋‹ค. 

     

    class Employee {
    	String name;
    	int salary;
    	public String getEmployee() {
    		return name+ " "+ salary;
    	}
    	public Employee() {	}
    	public Employee(String name, int salary) {
    		this.name = name;
    		this.salary = salary;
    	}
    }
    
    class Manager extends Employee {
    	String depart;
    	public String getManager() {
    		return super.getEmployee()+ "\t"+ depart;
    	}
    	public Manager(String name, int salary, String depart) {
    		super(name, salary);
    		this.depart = depart;
    	}
    }
    	
    public class Ex06_3 {
    	
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		Employee emp = new Employee("ํ™๊ธธ๋™",2000);
    		Manager man = new Manager("์ด์ˆœ์‹ ", 4000, "๊ฐœ๋ฐœ");
    		System.out.println(emp.getEmployee());
    		System.out.println(man.getManager());
    	}
    
    }

    ๋งค๋‹ˆ์ € ํด๋ž˜์Šค์˜ ๋ฉ”์†Œ๋“œ์™€ ์ƒ์„ฑ์ž๋ฅผ ๋ณด๋ฉด Employeeํด๋ž˜์Šค์˜ name, salary ๋ณ€์ˆ˜๋ฅผ ๊ทธ๋Œ€๋กœ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  super ํ‚ค์›Œ๋“œ๋ฅผ ํ†ตํ•ด ๋ถ€๋ชจ์˜ ์ธ์Šคํ„ด์Šค๋ฅผ ๋ถˆ๋Ÿฌ์˜ค๋Š” ๊ฑธ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

     

    ์—ฐ์Šต) Pet ํด๋ž˜์Šค๋ฅผ ์ƒ์†๋ฐ›๋Š” cat, dog, fish ํด๋ž˜์Šค์™€ TestPetํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ ๋‹ค.

    // Pet
    
    public class Pet {
    	String name;
    	int age;
    	
    	public Pet() {
    	}
    	
    	public Pet(String name, int age) {
    		this.name = name;
    		this.age = age;
    	}
    
    	public String getName() {
    		return name;
    	}
    	public void setName(String name) {
    		this.name = name;
    	}
    	public int getAge() {
    		return age;
    	}
    	public void setAge(int age) {
    		this.age = age;
    	}
    	
    	public void printInfo() {
    		System.out.println("์ด๋ฆ„" +"\t"+ "๋‚˜์ด");
    	}
    }

    cat, dog, fish ๋™๋ฌผ๋“ค์˜ ๊ณตํ†ต ์†์„ฑ์ธ name๊ณผ age๋ฅผ ๋ชจ์•„์„œ ๋ถ€๋ชจํด๋ž˜์Šค์ธ Pet์„ ๋งŒ๋“ค์—ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๊ธฐ๋ณธ์ƒ์„ฑ์ž๋ฅผ ํฌํ•จํ•ด ์ƒ์„ฑ์ž๋ฅผ ๋งŒ๋“ค๊ณ , ํ˜ธ์ถœ์„ ์œ„ํ•ด get set ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค๊ณ , ๋ชจ๋“  ์ •๋ณด๋ฅผ ์ถœ๋ ฅํ•  ์ˆ˜ ์žˆ๋„๋ก printInfo ํ•จ์ˆ˜๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.

    //Cat
    
    public class Cat extends Pet {
    	private String gender;	//์œ ์ผ์†์„ฑ
    	
    	public Cat() {
    		super();
    	}
    	
    	public Cat(String name, int age, String gender) {
    		super(name, age); //๊ทธ๋ƒฅ this์จ๋„ ๋จ
    		this.gender = gender;
    	}
    
    	//๋™์ž‘
    	public void cryCat() {
    		System.out.print("์•ผ์˜น~");
    	}
    	public void eatCat() {
    		System.out.println("์ƒ์„  ๋จน๊ธฐ");
    	}
    	public void sleepCat() {
    		System.out.println("์ƒˆ๊ทผ์ƒˆ๊ทผ~");
    	}
    		
    	// name๊ณผ age ๊ฒŸ์…‹์€ ๋‚ ๋ ค๋ฒ„๋ฆผ. ๋ถ€๋ชจ์— ์žˆ์œผ๋‹ˆ๊นŒ
    
    	}
    	public String getGender() {
    		return gender;
    	}
    	public void setGender(String gender) {
    		this.gender = gender;
    	}
            public void printInfo() {
    		System.out.println(name +"\t"+ age+"\t"+ gender);
    	}
    }
    //dog
    
    public class Dog extends Pet {
    	private String gender; //์œ ์ผ์†์„ฑ
    	private String color; //์œ ์ผ์†์„ฑ
    	
    	public Dog() {
    	}
    	
    	public Dog(String name, int age,String gender, String color) {
    		super(name, age);
    		this.gender = gender;
    		this.color = color;
    	}
    	
    	//๊ฐœ์— ๊ด€๋ จ๋œ ๋™์ž‘ ํ•จ์ˆ˜ 
    	public void cryDog() {
    		System.out.println("๋ฉ๋ฉ~");
    	}
    	public void eatDog() {
    		System.out.println("์‚ฌ๋ฃŒ ๋จน๊ธฐ");
    	}
    	public void sleepDog() {
    		System.out.println("์ฟจ์ฟจ~");
    	} 
    	//๋‹ค๋ฅธ ์• ์™„๋™๋ฌผ์— ์—†๋Š” ๊ธฐ๋Šฅ ๊ฐ€์ •... 
    	public void runDog() {
    		System.out.println("๋ฐœ๋ฐœ~");
    	}
    		
    	public String getGender() {
    		return gender;
    	}
    	public void setGender(String gender) {
    		this.gender = gender;
    	}
    	public String getColor() {
    		return color;
    	}
    	public void setColor(String color) {
    		this.color = color;
    	}
    	
    	public void printInfo() {
    		System.out.println(name +"\t"+ age+"\t"+ gender+"\t"+color);
    	}
    }
    // Fish
    
    public class Fish extends Pet{
    	private String gender; //์œ ์ผ์†์„ฑ
    	private double tailSize; //Fish์—๋งŒ ์กด์žฌํ•˜๋Š” ์œ ์ผ์†์„ฑ
    
    	public Fish() {
    	}
    	
    	public Fish(String name, int age,String gender, double tailSize) {
    		super(name, age);
    		this.gender = gender;
    		this.tailSize = tailSize;
    	}
    	
    	//๋™์ž‘
    	public void swimFish() {
    		System.out.println("์–ดํ‘ธ์–ดํ‘ธ~");
    	}	
    	
    	//Fish ํด๋ž˜์Šค ์œ ์ผ ํ•จ์ˆ˜
    
    	public String getGender() {
    		return gender;
    	}
    
    	public void setGender(String gender) {
    		this.gender = gender;
    
    	public double getTailSize() {
    		return tailSize;
    	}
    
    	public void setTailSize(double tailSize) {
    		this.tailSize = tailSize;
    	}
    	
    	public void printInfo() {
    		System.out.println(name +"\t"+ age+"\t"+ gender+"\t"+ tailSize);
    	}
    	
    }

    name๊ณผ age๋Š” ๋ถ€๋ชจํด๋ž˜์Šค์— ์กด์žฌํ•˜๋Š” ๊ณตํ†ต ์†์„ฑ์ด๊ธฐ ๋•Œ๋ฌธ์— ๋‚ ๋ ค๋ฒ„๋ฆฌ๊ณ  ์œ ์ผํ•œ ์†์„ฑ๋งŒ ๋‚จ๊ฒผ๋‹ค. ๋งˆ์ง€๋ง‰ ์ค„์˜ printInfoํ•จ์ˆ˜๋ฅผ ๋ณด๋ฉด ์„ ์–ธ ์—†์ด ๋ถ€๋ชจํด๋ž˜์Šค์— ๊ธฐ์žฌํ•œ name๊ณผ age๋ฅผ ๊บผ๋‚ด ์“ธ ์ˆ˜ ์žˆ๋Š” ๊ฑธ ์•Œ ์ˆ˜ ์žˆ๋‹ค.

    // TestPet
    
    
    public class TestPet {
    
    	public static void main(String[] args) {
    	
    				Pet p = new Pet();
    				Cat c = new Cat("์•ผ์˜น์ด", 3, "์•”๋†ˆ");
    				Dog d = new Dog("๋ฉ๋ฉ์ด", 4, "์ˆซ๋†ˆ", "๊ฒ€์ •");
    				Fish f = new Fish("๋ฌผ๊ณ ๊ธฐ", 4, "์•”๋†ˆ", 1.5);
    				p.printInfo();			
    				c.printInfo();
    				d.printInfo();
    				f.printInfo();
    	}
    
    }

    mainํ•จ์ˆ˜๋ฅผ ๊ฐ€์ง„ TestPetํด๋ž˜์Šค์—์„œ ๊ฐ์ฒด์ƒ์„ฑ์œผ๋กœ ์ƒ์„ฑ์ž์— ๊ฐ’์„ ๋ณด๋‚ธ ํ›„ printInfoํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค.

     

     

    ์ ‘๊ทผ์ง€์ •์ž

    ํด๋ž˜์Šค๋“ค ๊ฐ„ ์ƒํ˜ธ์ž‘์šฉ์‹œ ๋ณด์•ˆ๊ณผ ์ฝ”๋“œ ํšจ์œจ์„ฑ์„ ์œ„ํ•ด ์ ‘๊ทผ์„ ์ œ์–ดํ•  ์ˆ˜ ์žˆ๋‹ค. ํด๋ž˜์Šค, ๋ฉ”์†Œ๋“œ, ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜๋ฅผ ์„ ์–ธํ•  ๋•Œ ์ ‘๊ทผ์ง€์ •์ž๋ฅผ ๋ถ™์ผ ์ˆ˜ ์žˆ๋‹ค. 'public, protected, (default), private'

     

    class Super{
    	public int num1 = 10; // ์‚ฌ์šฉ๊ฐ€๋Šฅ. ์ ‘๊ทผ์ œํ•œ ์—†์Œ
    	protected int num2 = 20; //๊ฐ™์€ ํŒจํ‚ค์ง€์ธ ๊ฒฝ์šฐ ์‚ฌ์šฉ๊ฐ€๋Šฅ
    	int num3 = 30; //default ์‚ฌ์šฉ๊ฐ€๋Šฅ
    	private int num4 = 40; //์‚ฌ์šฉ ๋ถˆ๊ฐ€๋Šฅ. ๊ฐ™์€ ํด๋ž˜์Šค์—์„œ๋งŒ ์‚ฌ์šฉ ๊ฐ€๋Šฅ.
    	
    	public int getNum4() { // private์— ์ ‘๊ทผํ•˜๊ธฐ ์œ„ํ•ด ํผ๋ธ”๋ฆญ ๋ฉ”์„œ๋“œ ์‚ฌ์šฉ
    		return num4;
    	}
    }
    
    class Sub extends Super{
    	private int num5;  //private์ด์ง€๋งŒ ๊ฐ™์€ ํด๋ž˜์Šค ๋‚ด ์ ‘๊ทผ์€ ์‚ฌ์šฉ๊ฐ€๋Šฅ.
    	
    	public void print() {
    		System.out.println(num1);
    		System.out.println(num2);
    		System.out.println(num3);
    		System.out.println(getNum4());
    		System.out.println(num5); //๊ธฐ๋ณธ๊ฐ’ 0์ถœ๋ ฅ
    	}
    }
    
    public class ex06_4 {
    
    	public static void main(String[] args) {
    		Sub sub = new Sub();
    		sub.print();
    	}
    
    }

    ์ผ๋ฐ˜์ ์œผ๋กœ ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜์— private์„ ์‚ฌ์šฉํ•œ๋‹ค. public์€ ์ ‘๊ทผ ์ œํ•œ์ด ์—†์œผ๋ฉฐ protected๋Š” ํŒจํ‚ค์ง€๊ฐ€ ๋‹ค๋ฅด์ง€๋งŒ ์ƒ์†๊ด€๊ณ„์ธ ๊ฒฝ์šฐ์—” ์ ‘๊ทผ ๊ฐ€๋Šฅํ•˜๋‹ค. default๋Š” ํŒจํ‚ค์ง€๊ฐ€ ๋‹ค๋ฅด๋ฉด ์ ‘๊ทผํ•  ์ˆ˜ ์—†๋‹ค.

     

     

     

    ๐Ÿ˜œ NEXT : ์ž๋ฐ” ๋‹คํ˜•์„ฑ๊ณผ ์˜ค๋ฒ„๋ผ์ด๋”ฉ ๊ณต๋ถ€ํ•˜๊ธฐ

    ๋ฐ˜์‘ํ˜•

    ๋Œ“๊ธ€

๊ฐœ๋ฐœ๊ณต๋ถ€