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:

  1. 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.
  2. Information-hiding(信息隐藏): By interacting only with an object's methods. the details of its internal implementation remain hidden from the outside world.
  3. 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.
  4. 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
  1. Real-world objects contain states and behaviors
  2. A software object's state is stored in fields
  3. A software object's behavior is exposed through methods
  4. Hiding internal data from the outside world, and accessing it only through publicly exposed methods is known as data ecapsulation
  5. A blueprint for a software object is called a class.
  6. Common behavior can be defined in a superclass and inherited into a subclass using the extends keyword
  7. A collection of methods with no implementation is called an interface
  8. A namespace that organizes classess and interfaces by functionality is called a package
  9. The term API stands for Application Programming Interface
posted @   Felix_Openmind  阅读(245)  评论(0编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· AI 智能体引爆开源社区「GitHub 热点速览」
· 写一个简单的SQL生成工具
*{cursor: url(https://files-cdn.cnblogs.com/files/morango/fish-cursor.ico),auto;}
点击右上角即可分享
微信分享提示