: 서브 클래스에서 슈퍼 클래스의 메소드 중복 작성
: 슈퍼 클래스 메소드의 원형(메소드 이름, 인자 타입 및 개수, 리턴 타입)과 동일하게 작성



paint(line);
이때 line은 Line 클래스의 객체이지만, Line 클래스는 Shape 클래스를 상속받고 있습니다. 따라서 업캐스팅(Upcasting) 이 일어납니다.

: 실질적인 공통 기능 구현의 강제와 확장을 목적으로 사용됩니다.
: abstract로 선언된 메소드, 메소드의 코드는 없고 원형만 선언

만약 추상메서드를 가지고 있다면, abstract로 선언해줘야함
아니면 오류
하위 클래스(서브 클래스)에서 반드시 오버라이딩 해야함
추상 클래스를 상속받으면 추상 클래스가 됨
서브 클래스도 abstract로 선언해야함
하위 클래스(서브 클래스)가 추상 클래스를 상속받았을 때 추상 메서드를 모두 구현하면, 해당 서브 클래스는 더 이상 abstract로 선언할 필요가 없다.
--추상 메서드 모두 구현--
abstract class A {
abstract int add(int x, int y); // 추상 메서드
}
class C extends A {
@Override
int add(int x, int y) {
return x + y; // 추상 메서드 구현
}
}
public class Main {
public static void main(String[] args) {
C c = new C(); // 객체 생성 가능
System.out.println(c.add(3, 5)); // 출력: 8
}
}
--추상 메서드 구현 X--
abstract class A {
abstract int add(int x, int y); // 추상 메서드
}
abstract class B extends A {
// add() 메서드를 구현하지 않음
}
public class Main {
public static void main(String[] args) {
B b = new B(); // 컴파일 오류: 추상 클래스는 객체 생성 불가
}
}
: 상수와 추상 메서드로만 구성
(변수 필드 없음)
interface PhoneInterface {
int BUTTONS = 20; // 상수 필드 선언
void sendCall(); // 추상 메소드
void receiveCall(); // 추상 메소드
}
상수와 추상 메서드로만 구성
-> 메서드 : public abstract 타입으로 생략 가능
-> 상수 : public static final 타입으로 생략 가능
인터페이스의 객체 생성 불가
인터페이스 간에 상속 가능
interface MobilePhoneInterface extends PhoneInterface {
void sendSMS(); // 새로운 추상 메소드 추가
void receiveSMS(); // 새로운 추상 메소드 추가
}
interface MusicPhoneInterface extends PhoneInterface, MP3Interface {
......
}
interface Animal {
void sound(); // 추상 메서드
}
class Dog implements Animal {
@Override
public void sound() {
System.out.println("멍멍");
}
}
class Cat implements Animal {
// sound() 메서드 미구현 -> 컴파일 오류 발생
}
패키지명.클래스
toString() : 객체를 문자열로 리턴
getClass() : 객체의 클래스 정보를 담은 class 객체 리턴 -> 클래스 이름 등
hashCode() : 객체의 해시코드 값을 리턴하며, 객체마다 다름 -> 16진수 값
Point a = new Point(2,3);
String s = a + "추가글자";
System.out.println(s);
->
Point a = new Point(2,3);
String s = a.toString() + "추가글자";
System.out.println(a.toString());
이렇게 클래스의 객체를 나타내면, toString()으로 바뀜

: 객체만 사용할 수 있는 컬렉션 등에 기본 타입의 값을 사용하기 위해
즉, 기본타입으로 여러 메소드를 사용하기 위해

: 기본 타입의 값을 Wrapper 객체로 변환하는 것
: Wrapper 객체에 들어 있는 기본 타입의 값을 빼내는 것

-> String 클래스는 문자열을 나타냄
String 객체는 수정이 불가능함

-> 가변 스트링을 다루는 클래스임
StringBuffer sb = new StringBuffer("This");
sb.append(" is pencil."); // sb = "This is pencil."
sb.insert(7, " my"); // sb = "This is my pencil."
sb.replace(8, 10, "your"); // sb = "This is your pencil."
System.out.println(sb); // "This is your pencil." 출력

import java.util.StringTokenizer;
public class StringTokenizerEx {
public static void main(String[] args) {
String query = "name=kitae&addr=seoul&age=21";
StringTokenizer st = new StringTokenizer(query, "&");
int n = st.countTokens(); // 분리된 토큰 개수
System.out.println("토큰 개수 = " + n);
while(st.hasMoreTokens()) {
String token = st.nextToken(); // 토큰 얻기
System.out.println(token); // 토큰 출력
}
}
}
<결과>
토큰 개수 = 3
name=kitae
addr=seoul
age=21
-> 0보다 크거나 같고 1.0 보다 작은 실수 난수 발생
Math.random()*100 + 1 -> 1~100까지의 랜덤 정수 발생