Object Programming  «Prev  Next»
Lesson 7

Object Oriented Programming Conclusion

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:
  1. abstraction,
  2. encapsulation,
  3. inheritance, and
  4. 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.


Difference between abstraction and encapsulation in Object Oriented Programming

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.


Glossary terms

This module discussed how the following terms relate to Java:
  1. Object-oriented programming (OOP): Object-oriented programming is the implementation of a program or algorithm using objects.
  2. Object: An object is a collection of data and the procedures (methods) that act on that data.
  3. Class: A class is a template that defines the implementation of an object, effectively acting as a blueprint for the object. A class defines data and methods and is a unit of organization in a Java program.
  4. Inheritance: Inheritance is the ability to derive new classes from existing classes in object-oriented programming.
  5. Method: A method is an isolated section of code in an object that performs a particular function.
  6. Message: In object-oriented programming, sending a message to an object to invoke a method is similar to calling a procedure in structured programming languages.

Object Programming Quiz

Click the Quiz link below to test what was explained in this module.
Object Programming - Quiz

SEMrush Software