What is object-oriented programming?
Object-oriented programming (OOP) is a computer programming model that organizes software design around data, or objects, rather than functions and logic. An object can be defined as a data field that has unique attributes and behavior.
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 programs that are large, complex and actively updated or maintained. This includes programs for manufacturing and design, as well as mobile applications; for example, OOP can be used for manufacturing system simulation software.
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. The first step in OOP is to collect all of the objects a programmer wants to manipulate and identify how they relate to each other -- an exercise known as data modeling.
Examples of an object can range from physical entities, such as a human being who is described by properties like name and address, to small computer programs, such as widgets. Once an object is known, it is labeled with a class of objects that defines the kind of data it contains and any logic sequences that can manipulate it. Each distinct logic sequence is known as a method. Objects can communicate with well-defined interfaces called messages.
What is the structure of object-oriented programming?
The structure, or building blocks, of object-oriented programming include the following:
Classes are user-defined data types that act as the blueprint for individual objects, attributes and methods. Objects are instances of a class created with specifically defined data.
Objects can correspond to real-world objects or an abstract entity. When class is defined initially, the description is the only object that is defined.
Methods are functions that are defined inside a class that describe the behaviors of an object. Each method contained in class definitions starts with a reference to an instance object. Additionally, the subroutines contained in an object are called instance methods. Programmers use methods for reusability or keeping functionality encapsulated inside one object at a time.
Attributes are defined in the class template and represent the state of an object. Objects will have data stored in the attributes field. Class attributes belong to the class itself.
What are the 5 basic concepts of OOP?
1. Objects & Classes
Objects are the basic unit of OOPS representing real-life entities. They are invoked with the help of methods. These methods are declared within a class. Usually, a new keyword is used to create an object of a class in Java.
Class is a predefined or user-defined template from which objects are created. It represents properties/methods that are common to all objects of the same class. It has several features, such as access modifiers, class names, interfaces, and class bodies.
Abstraction means showing only the relevant details to the end-user and hiding the irrelevant features that serve as a distraction. For example, during an ATM operation, we only answer a series of questionsto process the transaction without any knowledge about what happens in the background between the bank and the ATM.
Encapsulation is a means of binding data variables and methods together in a class. Only objects of the class can then be allowed to access these entities. This is known as data hiding and helps in the insulation of data.
4. Inheritance – Single, Multilevel, Hierarchical, and Multiple
Inheritance is the process by which one class inherits the functions and properties of another class. The main function of inheritance is the reusability of code. Each subclass only has to define its features. The rest of the features can be derived directly from the parent class.
Single Inheritance – Refers to a parent-child relationship where a child class extends the parent class features. Class Y extends Class X.
Multilevel Inheritance – Refers to a parent-child relationship where a child class extends another child’s class. Class Y extends Class X. Class Z extends Class Y.
Hierarchical Inheritance – This refers to a parent-child relationship where several child classes extend one class. Class Y extends Class X, and Class Z extends Class X.
Multiple Inheritance – Refers to a parent-child relationship where one child class is extending from two or more parent classes. JAVA does not support this inheritance.
5. Polymorphism – Static and Dynamic
It is an object-oriented approach that allows the developer to assign and perform several actions using a single function. For example, “+” can be used for addition as well as string concatenation. Static Polymorphism is based on Method Overloading, and Dynamic Polymorphism is based on Method Overriding.