본문 바로가기

Java

상속 실습

class Parent1{
 public int p1;

 //public Parent1(){
 // System.out.println("부모 생성자");
 //}

 public Parent1(int p1){
  System.out.println("부모 생성자");
 }

 public void methodP1(){
  System.out.println("methodP1() 호출");
  //System.out.println(p1);
  System.out.println("this : "+this); 

class Child1 extends Parent1{
 public int c1;

 public Child1(){
  //디폴트 생성자
  //super();
  super(10); //자식 클래스에서 명시적으로 부모 생성자를 호출
  //this();
  //super(). this() : 생성자 관련된 내용 
  System.out.println("자식 생성자");
 }

 public void methodC1(){
  super.methodP1();
  System.out.println("methodC1() 호출");
  System.out.println("super : "+super.toString());
  //this와 참조값이 같음
 }
}

public class ExamEx02{
 public static void main(String[] args){
  Child1 c1 = new Child1();
  c1.methodC1();
 }
}

'Java' 카테고리의 다른 글

interface  (0) 2013.03.15
추상 클래스  (0) 2013.03.15
Varargs 사용하기  (0) 2013.03.15
다른 폴더의 패키지 import  (0) 2013.03.15
패키지 실습  (0) 2013.03.15