카테고리 없음

상속 복습

장꾸꾸 2020. 10. 13. 11:00

17. 상속(Inheritance)
    : 코드의 재사용 및 간결성을 위해 사용된다
      기존의 클래스(Super, Parent)라하고 확장하여 새롭게 정의한 
      클래스를 (Sub, Child)라 부른다. 다중상속 안된다.

      형식) class 부모클래스  
             {
             }
             class 자식클래스 extends 부모클래스  
             {
             }
          (다중상속이 안됨, 저장파일 : main이 들어있는 클래스명과 동일)

 

package inhreitanceex;

class SuperEx{
	public void view() {
		System.out.println("view1 method");
	}
}
class SubEx extends SuperEx{
	public void view() {
		super.view();
		System.out.println("view2 method");
	}
}
public class inhreitanceEx1 {
	public static void main(String[] args) {
		new SubEx().view();
	}
}

//-----------------------------------------------------
/*
class SuperEx{
	public void view1() {
		System.out.println("view1 method");
	}
}
class SubEx extends SuperEx{
	public void view2() {
		System.out.println("view2 method");
	}
}
public class inhreitanceEx1 {
	public static void main(String[] args) {
		SubEx ob=new SubEx();
		ob.view1();
		ob.view2();
	}
}
*/







 

package inheritanceex;

class ParentEx{ //이건 사실 extends Object임. 디폴트로 오브젝트를 상속받음.
	public ParentEx() {
		super();
		System.out.println("parent class");
	}
}
class ChildEx extends ParentEx{
	public ChildEx() {
		super();//(생략가능)모든 생성자는 기본으로 부모의 생성자를 호출 
					//f3 누르면 기본 디폴트 생성자로 감
		System.out.println("child class");
	}
}

public class inheritanceEx2 {
	public static void main(String[] args) {
		new ChildEx();
	}
}

 

이때 ChildEx가 Object와 Parent를 둘다 다중 상속 받은 게 아닌가 할 수 있다.

그러나! 다중상속은 동등한 클래스를 양쪽에서 상속받는 것이고

ChildEx의 경우는 수직으로 상속받기 때문에 다중상속에 해당하지 않는다.

package inheritanceex;
//Super와 this의 차이

class Branch{
	public Branch(String str) { //4. 찍어줌
		System.out.println(str+" "+ "Branch constructor");
	}
	public Branch(String str, int num) { //3. 
		this(str);
		System.out.println(str+" "+num+" "+ "Branch constructor"); // 5 
	}
}
class Leaf extends Branch{
	public Leaf(String str, int num) {
		super(str, num); // 2. 생성자 호출
		System.out.println(str+" "+num+" "+ "Leaf constructor"); // 6 ㅉ어줌
	}
}
public class inheritanceEx3 {
	public static void main(String[] args) {
		new Leaf("ABC", 10); //1. 리프에서 출발
	}
}

 

 

package inhreitanceex;

//super 와  this
class Branch{
	public Branch() {
		System.out.println("branch default constructor");
	}
	public Branch(String str, int num) {
		this();
		System.out.println(str + "  "+ num + " branch constructor");
	}
}
class Leaf extends Branch{
	public Leaf() {
		this("ABC");
		System.out.println("leaf default constructor");
	}
	public Leaf(String str) {
		super(str, 10);
		System.out.println(str + " leaf constructor");
	}
}
public class inhreitanceEx3 {
	public static void main(String[] args) {
		new Leaf();
	}
}

 

 

package overrideex;

class Test1{
	public void view1() { System.out.println("view1 method");}
	public void view3() { System.out.println("view3 method");}
}
class Test2 extends Test1{
	public void view1() { System.out.println("view11 method");}
	public void view2() { System.out.println("view22 method");}
}
public class OverrideEx {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
			Test2 ob = new Test2();
			ob.view1(); //view11 method
			ob.view2(); //view22
			ob.view3(); //view3
	}

}