package kr.s16.object.extention;
//부모클래스
class Parent{
int a =100;
}
class Child extends Parent{
int b = 200;
}
public class ExtentionMain01 {
public static void main(String[] args) {
Child ch = new Child();
System.out.println(ch.a);//parent의 a
System.out.println(ch.b);//child의 b
}
}
100
200
package kr.s16.object.extention;
//부모 클래스
class People extends Object{ //Object는 최상위 클래스
public void eat() {
System.out.println("식사하다.");
}
}
//자식 클래스
class Student extends People {
public void study(){
System.out.println("공부하다.");
}
}
public class ExtentionMain02 {
public static void main(String[] args) {
Student s = new Student();
s.eat(); //People의 메서드를 상속 받아서 호출
s.study(); //Student의 메서드
System.out.println(s.toString());//toString 은 Object의 메서드를 상속받아서 호출
}
}
식사하다.
공부하다.
kr.s16.object.extention.Student@515f550a
package kr.s16.object.extention;
//부모클래스
class A{
int x = 100;
private int y =200;
public int getY() {
return y;
}
}
//자식클래스
class B extends A{
int z = 300;
}
public class ExtentionMain03 {
public static void main(String[] args) {
B bp = new B();
System.out.println(bp.x);
System.out.println(bp.getY());//그냥 y는 불가능. y는 private이기 때문에 get메서드를 만든다음호출해야됨
System.out.println(bp.z);
}
}
100
200
300