https://docs.oracle.com/javase/tutorial/java/concepts/object.html
https://docs.oracle.com/javase/tutorial/java/concepts/class.html
https://docs.oracle.com/javase/tutorial/java/concepts/inheritance.html
https://docs.oracle.com/javase/tutorial/java/concepts/interface.html
https://docs.oracle.com/javase/tutorial/java/concepts/package.html
Object
Software objects are conceptually similar to real-world objects: they too consist of state and related behavior.
An object stores its state in fields (variables in some programming languages) and exposes its behavior through methods (functions in some programming languages).
Methods operate on an object's internal state and serve as the primary mechanism for object-to-object communication.
Hiding internal state and requiring all interaction to be performed through an object's methods is known as data encapsulation — a fundamental principle of object-oriented programming
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 simply remove it from your 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 machine.
Class
A class is the blueprint from which individual objects are created.
A class define their interaction with the outside world through the methods that they expose.
Inheritance
Object-oriented programming allows classes to inherit commonly used state and behavior from other classes.
keywords: extends
Interface
An interface is a group of related methods with empty bodies.
Implementing an interface allows a class to become more formal about the behavior it promises to provide. Interfaces form a contract between the class and the outside world, and this contract is enforced at build time by the compiler. If your class claims to implement an interface, all methods defined by that interface must appear in its source code before the class will successfully compile.
keywords: implements
Package
A package is a namespace that organizes a set of related classes and interfaces. Conceptually you can think of packages as being similar to different folders on your computer.
Keep things organized by placing related classes and interfaces into packages
Quiz:
- Real-world objects contain state and behavior.
- 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 encapsulation.
- 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 classes and interfaces by functionality is called a package.
- The term API stands for Application Programming Interface.
No comments:
Post a Comment