This module delved into object-oriented programming (OOP) and its relevance to Java. You started out the module by learning about objects and classes, which form the basis for OOP.
You also learned about inheritance, which allows classes to inherit functionality from other classes.
To summarize what was previously discussed, Java is an object-oriented programming (OOP) language. Object orientation helps a developer to achieve a modular, extensible, maintainable, and reusable system. To write good-quality programs, a programmer must have a firm command of OOP concepts. Object-oriented programming revolves around the concept of an object, which encapsulates data and behavior acting on the data together.
An object provides its services through a well-defined interface. This interface specifies "what" the object offers, abstracting "how" (actual implementation). Object orientation provides support for modeling solutions at a higher level of abstraction in terms of classes, a hierarchy of related classes (inheritance), association among classes, and dynamic binding (dynamic polymorphism).
For any OCPJP exam, you are expected to know essential OOP concepts.
The foundations of OOP are:
- abstraction,
- encapsulation,
- inheritance, and
- polymorphism.
The concepts related to classes covered constructors and access modifiers in detail.
-
Essentials of OOP:
To get a sense of the world of object-oriented programming, take a mental stroll around the television department of your local consumer electronics retailer. A television is an abstraction that offers certain functionality through the proper interface (a TV remote). As a viewer, you need not understand how the TV works; the TV abstracts all the details of its operation from the viewer (abstraction). A television object encapsulates properties of the television (such as brightness, channel number, and volume) and associated behavior to control these properties in a single entity (encapsulation). In other words, the access to these properties is restricted to the associated operations. There are different types of televisions, such as CRT television, LED television, and LCD television; they belong to a single family forming an inheritance hierarchy. Although all types of televisions support "display" functionality, the internal technology enabling the display of content may differ (polymorphism). With this television analogy in mind and with the help of the programming examples discussed in this module, you now have a better understanding of OOP concepts.
In Object-Oriented Programming (OOP), abstraction and encapsulation are two fundamental principles that help in managing complexity and improving the design of software systems. While they often work together, they serve different purposes:
Abstraction
- Definition:Abstraction is the process of hiding unnecessary details from the user and showing only the relevant features of an object. It focuses on what an object does rather than how it does it.
- Purpose:
- To simplify complex systems by modeling classes based on the essential properties and behaviors.
- To provide a clear, high-level interface that users can understand and interact with without needing to know the internal mechanics.
- Implementation:
- Abstract Classes:Classes that can't be instantiated and are meant to be inherited by other classes.
- Interfaces:A contract that specifies methods that must be implemented by any class that implements it.
- Example:
- Imagine a `Vehicle` class. The user of this class might only need to know that a vehicle can `start()` and `stop()`. The internal combustion process, fuel efficiency calculation, or how the brakes function are abstracted away.
Encapsulation
- Definition:Encapsulation is about bundling data (attributes) and methods (behaviors) that operate on the data into a single unit (object) and restricting direct access to some of the object's components. This is often achieved through mechanisms like making attributes private and providing public methods to interact with those attributes.
- Purpose:
- To protect the integrity of the object's state by preventing direct manipulation of its data from outside the class.
- To control how the data can be accessed and modified.
- Implementation:
- Private Attributes:Attributes that can only be accessed within the class itself.
- Getters and Setters:Public methods that allow controlled access to private attributes.
Modern Java
- Example:
- In a `BankAccount` class, the balance might be a private attribute. Users can't directly read or change it; instead, they use methods like `deposit(amount)` and `withdraw(amount)`. This ensures that no one can directly set a negative balance or withdraw more than what's available.
Key Differences
- Focus:
- Abstraction focuses on the external behavior of the object and what operations can be performed on it.
- Encapsulation focuses on the internal implementation details and data hiding.
- Visibility:
- Abstraction deals with what is shown to the outside world (interface).
- Encapsulation deals with what is hidden from the outside world (implementation details).
- Outcome:
- Abstraction simplifies the model by extracting common features of objects into a single abstract concept.
- Encapsulation ensures that the internal representation (data and methods) of an object is protected from direct modification.
In practice, these principles complement each other. Abstraction decides what features are available to users, while encapsulation ensures those features are implemented in a safe, controlled manner.