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
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· winform 绘制太阳,地球,月球 运作规律
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· AI 智能体引爆开源社区「GitHub 热点速览」
· 写一个简单的SQL生成工具