Java Tutorials(Object-Oriented Programming Concepts)
Object-Oriented Programming Concepts
Core concepts: objects, messages, classes, and inheritance.
What is an Object?
Real-world objects share two characteristics: 【state】 & 【behavior】
eg: Dog
-> state: name, color, breed, hungry
-> behavior: barking, fetching, wagging tail

Bundling code into individual software objects provides a number of benefits, including:
- Modularity(模块化):The source code for an object can be written and maintained independently of the source code for other objects. Once created , an object can be easily passed around inside the system.
- Information-hiding(信息隐藏): By interacting only with an object's methods. the details of its internal implementation remain hidden from the outside world.
- Code re-use(代码复用): If an object already exists(perhaps written by another software developer), you can use that object in your program. This allows specialists to implement/test/debug complex, task-specific objects, which you can then trust to run in your own code.
- Pluggability and debugging ease(插件化和易于调试): If a particular object turns out to be problematic, you can simple remove it from you application and plug in a different object as its replacement. This is analogous to fixing mechanical problems in the real world. If a bolt breaks, you replace it, not the entire matchine
What is an Class?
A class is blueprint or prototype from which objects are created.
public class Bicycle {
int cadence = 0;
int speed = 0;
int gear = 1;
public int getCadence() {
return cadence;
}
public void setCadence(int cadence) {
this.cadence = cadence;
}
public int getSpeed() {
return speed;
}
public void setSpeed(int speed) {
this.speed = speed;
}
public int getGear() {
return gear;
}
public void setGear(int gear) {
this.gear = gear;
}
public static void main(String[] args) {
// Create two different Bicycle objects from the same blueprint of Bicycle.
Bicycle bike01 = new Bicycle();
Bicycle bike02 = new Bicycle();
bike01.setCadence(50);
bike01.setSpeed(20);
bike02.setCadence(30);
bike02.setCadence(15);
}
}
What is an Inheritance?
Inheritacne provides a powerful and natural mechanism for organizing and structing software.
Object-oriented programming allows classes to inherit commonlly used states and behaviors from other classes.

What is an Inteface?
An interface is a contract(this contract is enforced at build time by the compiler) between a class and outside world.
In its most common form, an interface is a group of related methods with empty bodies.
public interface IBicycle {
public abstract void changeCadence(int newValue);
public abstract void changeGear(int newValue);
public abstract void speedUp(int increment);
public abstract void applyBrakes(int decrement);
}
What is a Package?
A package is a namespace that organize a set of related classes and interfaces.
- Bulletpoints
- Real-world objects contain states and behaviors
- A software object's state is stored in fields
- A software object's behavior is exposed through methods
- Hiding internal data from the outside world, and accessing it only through publicly exposed methods is known as data ecapsulation
- A blueprint for a software object is called a class.
- Common behavior can be defined in a superclass and inherited into a subclass using the extends keyword
- A collection of methods with no implementation is called an interface
- A namespace that organizes classess and interfaces by functionality is called a package
- The term API stands for Application Programming Interface

浙公网安备 33010602011771号