
필드와 메소드자동으로 수정됨!class 자식클래스 extends 부모클래스 { ... }
class 자식클래스 extends 부모클래스1, 부모클래스2 { ... } ❌
public class CellPhone {
String model;
String color; }
prublic class DmbCellPhone extends CellPhone {
int channel;
DmbCellPhone(String model, String color, int channel) {
this.model = model;
this.color = color;
this.channel = channel;}
}
부모 객체부터 생성되고 자식 객체가 생성된다.super 내가 안 쓰면 컴파일러가 자동으로 해줌.자식클래스(매개변수 선언, ...) {
super(매개값, ...);
}
super(...) 구문은 반드시 자식생성자의 첫 줄에 위치해야 함.




그렇기 때문에, 부모생성자에 (매개타입, 매개타입,...)이 있는 생성자가 있다면, 자식클래스는 거기에 맞춰서 (그와 형식이 비슷한, 또는 그 형식을 포함하는) 생성자를 만들어야한다는 뜻임.
또 이해가 안된다면 이 강의를 계속 다시 돌려볼 것...
또 있는데,

이 경우에는 부모 클래스에 () 생성자와 (String ...) 생성자가 있음.
이럴 땐 자식 클래스에서 (String ...)으로 생성자를 선언해도 됨. 왜냐면 super()로 커버되니까.
여기서 만약

부모클래스를 이렇게 선언하면 자식클래스는

이렇게 오류 남.


수정해서 자식 클래스에서 재정의하는 것.public class Calculator {
double areaCircle(double r) {
System.out.println("카큘레이터 객체의 아리아써클 실행");
return 3.14159 * r * r;
}
}
public class Computer extends Calculator {
@Override
double areaCircle(double r) {
System.out.println("카큘레이터 객체의 아리아써클 실행");
return Math.PI * r * r;
}
}
방법1) SOurce > Overrid/implement Method
방법2) 걍 일일히 복붙하기
방법3) ctrl + space 상속받을 메소드 하나 선택(더블클릭)
자식 클래스에서 수정되기 전의 부모 메서드를 호출하고자 할경우 super를 사용.
중요한 것은, 바로 이 자식 클래스 안에서만 쓸 수 있다는 거예요.
super.method();
public class Airplane {
public void land() {
System.out.println("착륙합니다");
}
public void fly() {
System.out.println("일반비행합니다");
}
public void takeOff() {
System.out.println("이륙합니다");
}
}
public class SupersonicAirplane extends Airplane {
public static final int NOMAL = 1;
public static final int SUPERSONIC = 2;
public int flyMode=1;
@Override
public void fly() {
if (flyMode ==SUPERSONIC ) {
System.out.println("초음속 비행합니다.");
} else {
super.fly();
}
}
}
오답노트)✅✅✅✅✅✅✅✅ 
무조건❗❗❗ 재정의된 메소드가 호출된다❗❗❗
final 클래스: 부모로 사용할 수 없는 클래스String 클래스final 메소드 : 자식이 재정의할 수 없는 메소드
종류로 생각하면 됨! 고양이는 동물로 설명될 수 있고 강아지도 동물로 설명될 수 있으니까.부모 클래스 : animal 자식 클래스 : cat
Cat cat = new Cat();
Animal animal = cat;
cat == animal >>true

재정의 된 method2와, method3ex) 👍 매우 중요해요 이해하세요.
Chile chile = new Child();
Parent parent = child;
parent.method1(); >>Ok
parent.method2(); >>Ok (재정의된 method2가 호출 됨)
parent.method3(); >> X (Parent에는 method3이 없으니)
public class Tire {
public int maxRotation;
public int accumulatiedRotation;
public String location;
public Tire(String location, int maxRotation) {
this.location = location;
this.maxRotation = maxRotation;
}
public boolean roll() {
++accumulatiedRotation;
if (accumulatiedRotation < maxRotation) {
System.out.println(location + "Tire 수명" + (maxRotation - accumulatiedRotation));
return true;
} else {
System.out.println("***" + location + "Tire 펑크***");
return false;
}
}
}
public class HankookTire extends Tire {
public HankookTire(String location, int maxRotation) {
super(location, maxRotation);
}
@Override
public boolean roll() {
++accumulatiedRotation;
if (accumulatiedRotation < maxRotation) {
System.out.println(location + "한국Tire 수명" + (maxRotation - accumulatiedRotation));
return true;
} else {
System.out.println("***" + location + "한국Tire 펑크***");
return false;
}
}
}
public class KumhoTire extends Tire {
public KumhoTire(String location, int maxRotation) {
super(location, maxRotation);
// TODO Auto-generated constructor stub
}
@Override
public boolean roll() {
++accumulatiedRotation;
if (accumulatiedRotation < maxRotation) {
System.out.println(location + "금호Tire 수명" + (maxRotation - accumulatiedRotation));
return true;
} else {
System.out.println("***" + location + "금호Tire 펑크***");
return false;
}
}
}
public class Car {
Tire frontLeftTire = new Tire("앞 왼쪽", 6);
Tire frontrightTire = new Tire("앞 왼쪽", 2);
Tire backLeftTire = new Tire("앞 왼쪽", 3);
Tire backrigntTire = new Tire("앞 왼쪽", 4);
int run() {
System.out.println("자동차가 달립니다");
if(frontLeftTire.roll() == false) {stop();return 1;}
if(frontrightTire.roll() == false) {stop();return 2;}
if(backLeftTire.roll() == false) {stop();return 3;}
if(backrigntTire.roll() == false) {stop();return 4;}
return 0;
}
void stop() {
System.out.println("자동차가 멈춥니다");
}
}
public class CarExample {
public static void main(String[] args) {
Car car = new Car();
for (int i =0; i<=5; i++) {
int problemLocation = car.run();
switch(problemLocation) {
case 1:
System.out.println("앞 왼쪽 HankookTire로 교체");
car.frontLeftTire = new HankookTire("앞 왼쪽",15);
break;
case 2:
System.out.println("앞 왼쪽 HankookTire로 교체");
car.frontrightTire = new HankookTire("앞 왼쪽",15);
break;
case 3:
System.out.println("앞 왼쪽 HankookTire로 교체");
car.backLeftTire = new HankookTire("앞 왼쪽",15);
break;
case 4:
System.out.println("앞 왼쪽 HankookTire로 교체");
car.backrigntTire = new HankookTire("앞 왼쪽",15);
break;
}
}
}
}
class Car {
Tire[] tires = {
new Tire("앞왼쪽",6),
new Tire("앞왼쪽",6),
new Tire("앞왼쪽",6),
new Tire("앞왼쪽",6) };
}
tire[1] = new KumhoTire("앞오른쪽", 13);
배열에 넣기만 해도 자동 타입 변환이 되는구나!
Tire[] tires = {
new Tire("앞왼쪽",6),
new Tire("앞오른쪽",6),
new Tire("뒤왼쪽",6),
new Tire("뒤오른쪽",6),
};
class Driver(Vehicle vehicle) {
vehicle.run();
} } >> vehicle 또는 그 자식객체이 들어온다고 선언한 것.

ClassCastException 예외 발생Parent parent = new Parent();
Child child = (Child) parent;❌
boolean result - 좌항(객체) instanceof 우항(타입)단독으로 객체 생성을 할 수 없고, 부모 클래스로만 사용된다Animal animal = new Animal(); ❌그럼 왜 만드는가? : 실체 클래스의 공통된 필드와 메소드의 이름을 통일할 목적public abstrat class 클래스 {
...
}
*** 부모 클래스 객체 안 만들고(=못 만들고) 바로 메소드 접근하는거니까...그래서 사용하는건가...
public class AnimalExample {
public static void main(String[] args) {
Dog dog = new Dog();
Cat cat = new Cat();
dog.sound();
cat.sound();
Animal animal = null;
animal = new Dog();
animal.sound();
Animal animal2 = null;
animal = new Cat();
animal2.sound();
animalSound(new Dog());
animalSound(new Cat());
}
public static void animalSound(Animal animal) {
animal.sound();
}
}
public abstract void sound();