Java #10 - Introduction to OOP

Java #10 - Introduction to OOP

What is object-oriented programming?

  • It is a programming language model for software design that revolves around objects rather than logic.
  • It is based on an object which consists of
    • state (data or fields)
    • behavior (methods)
  • An object-oriented program is a collection of objects working together.
  • The state is hidden from other objects and is accessed only through the objects' own method.
  • Let's say there is a Box object with the state: width, height and depth. We can hide the state of the object so that it is not accessible outside and can only be accessed by internal methods.

image.png

Why object-oriented?

  • OOP focuses on the objects that developers want to manipulate rather than the logic required to manipulate them. This approach to programming is well-suited for large, complex programs and actively updated or maintained.
  • The organization of an object-oriented program also makes the method beneficial to collaborative development, where projects are divided into groups. Additional benefits of OOP include code reusability, scalability and efficiency.

OOP Components

  • Object - Objects are building blocks of an OO program and it consists of state (data/attributes) and behavior (methods) same as a real-world objects.
  • Class - It is a template for an object. When you create/instantiate an object, you use a class as the basis for how the object is built. An object is an instance of a class with its own attribute value called instance variables.
  • Attributes - The data stored within an object represents the state of the object. Inn OO programming terminology, this data is called attributes.
  • Methods - The behavior of an object represents what the object can do. In procedural languages, the behavior is defined by procedures, functions, and subroutines. In OO programming terminology, these behavior are contained in methods, and you invoke a method by sending a message to it.

OOP Principles

  • Encapsulation - This principle states that all important information is contained inside an object and only select information is exposed. The implementation and state of each object are privately held inside a defined class. Other objects do not have access to this class or the authority to make changes. They are only able to call a list of public functions or methods. This characteristic of data hiding provides greater program security and avoids unintended data corruption.
  • Abstraction - Objects only reveal internal mechanisms that are relevant for the use of other objects, hiding any unnecessary implementation code. The derived class can have its functionality extended. This concept can help developers more easily make additional changes or additions over time.
  • Inheritance - Classes can reuse code from other classes. Relationships and subclasses between objects can be assigned, enabling developers to reuse common logic while still maintaining a unique hierarchy. This property of OOP forces a more thorough data analysis, reduces development time and ensures a higher level of accuracy.
  • Polymorphism - Objects are designed to share behavior and they can take on more than one form. The program will determine which meaning or usage is necessary for each execution of that object from a parent class, reducing the need to duplicate code. A child class is then created, which extends the functionality of the parent class. Polymorphism allows different types of objects to pass through the same interface.

Did you find this article valuable?

Support SUBODH SINGH by becoming a sponsor. Any amount is appreciated!