- 기존의 클래스를 재사용하여 새로운 클래스를 작성하는 것
- 상속해주는 클래스를
부모 클래스
라 하고 상속 받는 클래스를자손 클래스
라 한다.
새로 작성하고자 하는 클래스의 이름 뒤에 상속받고자 하는 클래스의 키름을 키워드
extends
와 함께 써 주기만 하면 된다.
class Parent {
int age;
}
class Child extends Parent{ }
class GrandParent {
public GrandParent() {
System.out.println("GrandParent 생성자 호출");
}
}
class Parent extends GrandParent {
public Parent() {
//super(); 생략
System.out.println("Parent 생성자 호출");
}
}
class Child extends Parent {
public Child() {
//super(); 생략
System.out.println("Child 생성자 호출");
}
}
public class extendsConstructor {
public static void main(String[] args) {
Child child = new Child();
}
}
GrandParent 생성자 호출
Parent 생성자 호출
Child 생성자 호출
부모로부터 물려받은 변수는 자식 클래스에서 초기화하는게 아니라 부모 클래스의 생성자를 통해 초기화 하는게 바람직하다.
super
를 붙여서 구별class Parent1 {
int x = 10;
}
class Child1 extends Parent1{
int x = 20;
void method() {
System.out.println("x = " + x);
System.out.println("this.x = " + this.x);
System.out.println("super.x = " + super.x);
}
}
public class superVar {
public static void main(String[] args) {
Child1 child1 = new Child1();
child1.method();
}
}
x = 20
this.x = 20
super.x = 10
super()
는 부모의 생성자를 호출하는데 사용class Tv{
private int size;
public Tv(int size) {
this.size = size;
}
public int getSize() {
return size;
}
}
class ColorTv extends Tv {
private int resolution;
public ColorTv(int size, int resolution) {
super(size);
this.resolution = resolution;
}
public int getResolution() {
return resolution;
}
}
public class Ex1 {
public static void main(String[] args) {
ColorTv myTv = new ColorTv(32, 1024);
System.out.println(myTv.getSize() + "인치 " + myTv.getResolution() + "컬러");
}
}
32인치 1024컬러
class Tv{
private int size;
public Tv() {
}
public Tv(int size) {
this.size = size;
}
protected int getSize() {
return size;
}
}
class ColorTv extends Tv {
private int color;
public ColorTv(int size, int color) {
super(size);
this.color = color;
}
public void printProperty() {
System.out.println(super.getSize() + "인치 " + color + "컬러");
}
}
public class Ex1 {
public static void main(String[] args) {
ColorTv myTv = new ColorTv(32, 1024);
myTv.printProperty();
}
}
class Tv{
private int size;
public Tv(int size) {
this.size = size;
}
public int getSize() {
return size;
}
}
class ColorTv extends Tv {
private int resolution;
public ColorTv(int size, int resolution) {
super(size);
this.resolution = resolution;
}
public int getResolution() {
return resolution;
}
}
class IpTv extends ColorTv {
private String ip;
public IpTv(String ip, int size, int resolution) {
super(size, resolution);
this.ip = ip;
}
void printProperty() {
System.out.println("나의 IpTv는 " + ip + " 주소의 " +
super.getSize() + "인치 " + super.getResolution() + "컬러");
}
}
public class Ex1 {
public static void main(String[] args) {
IpTv ipTv = new IpTv("192.1.1.2", 32, 2048);
ipTv.printProperty();
}
}
변경
하는 것class Parent2{
void parentMethod() throws IOException, SQLException {
System.out.println("Parent2");
}
}
class Child2 extends Parent2 {
@Override
// 부모보다 예외 더 많이 선언하거나 범위가 더 좁은 접근 제어자 private 을 쓰면 ERROR
void parentMethod() throws IOException {
System.out.println("Child2");
}
}
public class overridingEx {
public static void main(String[] args) throws IOException {
Child2 child2 = new Child2();
child2.parentMethod();
}
}
새로운
메서드를 정의하는 것println
이 오버로딩
의 대표적 예시class Parent{
void parentMethod() {
System.out.println("Hello");
}
}
class Child extends Parent {
void parentMethod(String name) {
System.out.println("Hello " + name);
}
}
public class overloadingEx {
public static void main(String[] args) {
Child child = new Child();
child.parentMethod();
child.parentMethod("JaeHyun");
}
}
Hello
Hello JaeHyun
Parent p = new Child(); // Ok
Child c = new Parent(); // Error
class Phone {
String phoneNum;
void sendMessage() {
System.out.println("메시지 전송");
}
}
class SmartPhone extends Phone {
void sendKakao() {
System.out.println("카카오톡 전송");
}
}
public class polyEx {
public static void main(String[] args) {
Phone phone = new SmartPhone();
Phone.sendMessage();
}
}
실제 메모리에 올라가는 것 보다 자식 참조 변수로 호출할 수 있는 것이 많기 때문에 Error
true
or false
true
라면 참조변수가 검사한 타입으로 형변환이 가능하다는 것class Car {
void run() {
System.out.println("run");
}
}
class FireEngine extends Car {
void water() {
System.out.println("water");
}
}
class Ambulance extends Car {
void medic() {
System.out.println("medic");
}
}
public class Instanceof {
public static void main(String[] args) {
Car car = new Ambulance();
if (car instanceof Ambulance) {
Ambulance am = (Ambulance) car;
am.medic();
}
}
}
class Car {
void run() {
System.out.println("run");
}
}
class FireEngine extends Car {
void water() {
System.out.println("water");
}
}
class Ambulance extends Car {
void medic() {
System.out.println("medic");
}
}
public class Instanceof {
public static void main(String[] args) {
Car[] cars = new Car[2];
cars[0] = new FireEngine();
cars[1] = new Ambulance();
for (Car car : cars) {
if (car instanceof Ambulance) {
Ambulance am = (Ambulance) car;
am.medic();
}
else if (car instanceof FireEngine) {
FireEngine fe = (FireEngine) car;
fe.water();
}
}
}
}