공통 속성이나 기능을 추출하는 작업
객체가 내부적으로 어떻게 기능을 구현하는지 감춘다.
private 으로 접근 제한을 둔다.클래스는 다른 클래스로부터 파생(derived)될 수 있고, 그 클래스들의 필드와 메서드를 상속(inheriting)받을 수 있다.
Object 클래스는 수퍼클래스가 없다.Object를 제외한 모든 클래스는 명시적인 다른 수퍼클래스 뿐만 아니라 내부적으로 Object를 수퍼클래스로 가진다.Object 클래스가 위치한다.Java Platform에서 Object 클래스의 자손 객체들

public, protected 멤버(필드, 메서드, 중첩 클래스 nested class)를 상속받아 재사용할 수 있다.package-private 멤버도 상속 가능private 멤버들에도 접근 가능IS-A 관계가 아니고 단순 재사용을 위해 상속을 받으면 문제가 발생할 수 있다.public class Bicycle {
// the Bicycle class has three fields
public int cadence;
public int gear;
public int speed;
// the Bicycle class has one constructor
public Bicycle(int startCadence, int startSpeed, int startGear) {
gear = startGear;
cadence = startCadence;
speed = startSpeed;
}
// the Bicycle class has four methods
public void setCadence(int newValue) {
cadence = newValue;
}
public void setGear(int newValue) {
gear = newValue;
}
public void applyBrake(int decrement) {
speed -= decrement;
}
public void speedUp(int increment) {
speed += increment;
}
}
public class MountainBike extends Bicycle {
// the MountainBike subclass adds one field
public int seatHeight;
// the MountainBike subclass has one constructor
public MountainBike(int startHeight,
int startCadence,
int startSpeed,
int startGear) {
super(startCadence, startSpeed, startGear);
seatHeight = startHeight;
}
// the MountainBike subclass adds one method
public void setHeight(int newValue) {
seatHeight = newValue;
}
}
MountainBike는 4개의 필드와 5개의 메서드를 가진 것처럼 작성되었다.super 키워드를 사용하여 수퍼클래스의 생성자를 호출하였다.하나의 변수명, 함수명이 상황에 따라 다른 동작을 할 수 있다.
public class Bicycle {
// ...
public void printDescription(){
System.out.println("\nBike is " + "in gear " + this.gear
+ " with a cadence of " + this.cadence +
" and travelling at a speed of " + this.speed + ". ");
}
}
public class MountainBike extends Bicycle {
private String suspension;
public MountainBike(int startCadence, int startSpeed, int startGear, String suspensionType){
super(startCadence, startSpeed, startGear);
this.setSuspension(suspensionType);
}
// ...
@Override
public void printDescription() {
super.printDescription();
System.out.println("The " + "MountainBike has a" + getSuspension() + " suspension.");
}
}
public class RoadBikeextends Bicycle {
// In millimeters (mm)
private int tireWidth;
public MountainBike(int startCadence, int startSpeed, int startGear, int newTireWidth){
super(startCadence, startSpeed, startGear);
this.setTireWidth(newTireWidth);
}
// ...
@Override
public void printDescription() {
super.printDescription();
System.out.println("The RoadBike" + " has " + getTireWidth() + " MM tires.");
}
}
MountainBike와 RoadBike는 Bicycle을 상속받았다.MountainBike와 RoadBike는 Bicycle의 printDescription()을 override하고 행위를 추가했다.