package com.ljh;
import com.ljh.extd.ChildClass;
import com.ljh.extd.ParentClass;
import com.ljh.impl.InterfaceClass;
import com.ljh.impl.TestAbstract;
import com.ljh.impl.TestClassAbst;
import com.ljh.impl.TestClassImpl;
public class MainClass {
public MainClass() {
}
//인터페이스는 "강제적"으로 "필수기능구현"해야 하는 목적으로 만들어짐(다형성)
//부수적인것 : 데이터형을 통일시켜 사용할수 있음;
public static void main(String[] args) {
ParentClass p = new ParentClass();
// p.a = 10; default : 동일 패키지에서 접근가능
// p.b=1; protected 상속된 자식 클래스에서 접근 가능;
ChildClass c = new ChildClass();
c.prn();
// InterfaceClass i =new InterfaceClass();
// 인터페이스는 생성자를 보유할수 없기 때문에 단독으로 객채생성 불가능!
TestClassImpl tci = new TestClassImpl();
TestClassAbst tca = new TestClassAbst();
//두개 한곳에 몰아넣을예정 / 인터페이스 구현받았기 때문에 interface 배열로 구현
InterfaceClass[] i = new InterfaceClass[2];
i[0] = tci; i[0].prn();
i[1] = tca; i[1].prn();
// i[0].n = 0; //인터페이스로 부터 받은 상수이기 때문에 변경 불가능
// i[1].n = 1; //인터페이스로 부터 받은 상수이기 때문에 변경 불가능
//추상클래스의 생성자는 상속할 경우에만 사용됨! 단독으로 사용불가능
//AbstractClass a = new AbstractClass();
TestAbstract ta = new TestAbstract();
boolean b = ta.b;
String a = ta.IP;
ta.avg();
//셋다 접근 가능
}
}
부모클래스
package com.ljh.extd;
public class ParentClass {
int a;
protected int b;
public ParentClass(int a, int b) {
this.a = a;
this.b = b;
}
public ParentClass() {
}
public void prn(){
System.out.println("Parent 메소드");
}
}
package com.ljh.extd;
자식클래스
public class ChildClass extends ParentClass {
public ChildClass() {
super(10,20); //자식 클래스가 부모클래스를 호출하는 클래스
//this()는 내 생성자, super은 부모 생성자...
super.prn(); //부모의 메소드 호출
}
//부모로부터 물려받은 메소드 내부를 자식클래스에서 수정사용가능!
// Overriding
public void prn(){
System.out.println("Child 수정 메소드");
}
}
인터페이스
package com.ljh.impl;
//impliment 안됨;; 당연히? 선언만 되고 구현이 안되니까;
public interface InterfaceClass extends InterfaceClass2, InterfaceClass3 {
public int n1= 10; //상수만 선언가능(반드시 초기값 필요함!)
//public final int n = 10; //과 동일함
public void prn(); //메소드는 이름까지만 선언 가능
//public abstract void prn(); //과 동일함
// public void sum(){ //인터페이스는 메서드를 가질수 없다.
// }
// public InterfaceClass(){ //생성자를 가질수 없음
// }
}
댓글
댓글 쓰기