부모클래스명 변수명 = new 자식생성자();
부모와 자식간의 공통 요소(재정의 메소드) 만 사용 가능하며 , 자식 클래스의 추가된 것 들은 사용할 수 없다. 부모 타입으로 선언 되었기 때문에 부모의 필드만 사용가능하지만 자식 클래스에서 재정의했다면 재정의 된 메서드로 사용된다.
자식 클래스 타입으로 부모 생성자를 호출하는 것으로 오류!!!
부모의 범위가 더 크기 때문에 자식에 담을 수 없다.
각각의 타입을 확인할때 사용하는 문법
값 instanceof 클래스타입 (값이 클래스 타입이니?)
Car 클래스를 생성하고 superCar가 Car를 상속받아 타입을 비교하는 문법을 사용해보자.
package day18;
class Car {
String brand;
String color;
int price;
public Car() {}
public Car(String brand, String color, int price) {
super();
this.brand = brand;
this.color = color;
this.price = price;
}
void engineStart() {
System.out.println("열쇠로 시동 킴");
}
void engineStop() {
System.out.println("열쇄로 시동 끔");
}
}
//Car를 상속 받음. 자식 : SuperCar, 부모 : Car
class SuperCar extends Car{
String mode;
public SuperCar() {}
public SuperCar(String brand, String color, int price, String mode) {
super(brand, color, price);
this.mode = mode;
}
void changeMode(String newMode) {
this.mode = newMode; //위에서 정의된 변수도 다시 정의 할 수 있음.
System.out.println("모드가 바뀌었습니다.");
}
//부모와 같은 메서드를 자식에서 재정의하여 사용됨
@Override
void engineStart() {
System.out.println("음성으로 시동 킴");
}
@Override
void engineStop() {
System.out.println("음성으로 시동 끔");
}
}
public class CastingTest {
public static void main(String[] args) {
//업 캐스팅
Car noOptionFerrari = new SuperCar();
noOptionFerrari.engineStart();
//부모클래스가 자식 생성자를 불러옴으로써 자식 클래스의 메소드를 사용할 수 있다.
//downCasting - 뿌리부터 잘못된 오류 강제로 형변환해도 오류남.......
/*SuperCar brokenCar = (SuperCar)new Car();
brokenCar.changeMode("스포츠");*/
Car car = new Car(); //부모클래스에서 부모생성자. 가능
SuperCar ferrari = new SuperCar(); //자식클래스에서 자식생성자. 가능
//car가 Car 타입 ? true
if(car instanceof Car) {
System.out.println("nice casting");
}else {
System.out.println("err: wrong casting");
}
//ferrari가 Car 타입? new SuperCar 생성자 호출되고 SuperCar는 Car를 상속받아서 true
if(ferrari instanceof Car) {
System.out.println("nice casting");
}else {
System.out.println("err: wrong casting");
}
//car가 SuperCar타입? car는 부모값 다운 캐스팅으로 false
if(car instanceof SuperCar) {
System.out.println("nice casting");
}else {
System.out.println("err: wrong casting");
}
//noOptionFerrari가 SuperCar 타입? new SuperCar 생성자 호출했고 SuperCar는 Car를 상속 받아서 true
if(noOptionFerrari instanceof SuperCar) {
System.out.println("nice casting");
}else {
System.out.println("err: wrong casting");
}
}
}
객체가 단 1개만 존재할 때 외부에서 new를 하지 못하게 막아주고 클래스 내부에서 new를 한 후 외부에서 선언이 아닌 사용만 해준다.
public class IronMan{
private IronMan(){}; // 다른 곳에서 new로 접근하지 못함.
}
public class IronMan{
private IronMan(){}; // 다른 곳에서 new로 접근하지 못함.
public static IronMan getInstance(){
IronMan im = new IronMan();
return im;
}
}
public class IronMan{
private IronMan(){}; // 다른 곳에서 new로 접근하지 못함.
public static IronMan getInstance(){
IronMan im = new IronMan();
return im;
}
public static void fight(){
System.out.println("빔 발사");
}
public void walk() {
System.out.println("날아간다.");
}
}
public class Stadium {
public static void main(String[] args) {
IronMan i = new IronMan();
}
}
public class Stadium {
public static void main(String[] args) {
IronMan.figth(); // 빔 발사
}
}
public class Stadium {
public static void main(String[] args) {
IronMan i = IronMan.getInstance();
i.walk(); //접근가능.
}
}