Home >>Java Tutorial >Java OOPs Concepts
In this tutorial we will be discussing about the Java OOPs Concepts and the advantages of it. OOP generally elaborates to Object-Oriented Programming and it is considered as the procedural programming that is basically about writing the procedures or we can say methods that perform the operations on the data and on the other hand object-oriented programming is basically about creating objects that consists of both data and the methods.
There are several advantages of Object-oriented programming over procedural programming like: OOP is known to be faster and easier to execute and it provides a clear structure for the programs. OOPs assists the programmer in order to keep the Java code DRY that elaborates to "Don't Repeat Yourself" and makes the code easier to maintain, modify and debug. By the use of OOP you can create full reusable applications that are with less code and shorter development time.
In layman's terms you can understand the Object-Oriented Programming as a paradigm that provides various concepts like inheritance, data binding, polymorphism and many more. In the field of object-oriented programming language, Simula is considered to be the first of its kind. The second in this list is smalltalk that is considered as the first object-oriented programming language. There are various other names that are present in this list like: Java, C#, PHP, Python, C++ and many more. However, the prime focus of these object-oriented programming is to implement real-world entities, for instance, polymorphism, object, classes, inheritance, abstraction and various others.
Let's understand the object first, it basically means a real-world entity like a pen, computer, chair, watch, table, etc. As discussed above, object-oriented Programming is basically a methodology or paradigm we can say that is used to design a program by the help of classes and objects. The main motto is to simplify software development and maintenance by delivering the concepts that are as depicted below:
There are some other terms, along with these concepts that are generally used in the Object-Oriented design:
An object is basically any entity that possess state and behavior, for instance, pen, a chair, keyboard, table, bike, etc. The object can be physical or logical. In other terms, you can define an Object as an instance of a class. It basically consists of an address and occupy some space in memory. Objects have a special feature that they can communicate without knowing the details of each other's data or the code.
Let's understand it with an example: A chair is an object because it has states like color, shape, material, and it has behaviors like folding, strong etc.
Class in Java is basically a logical entity and in other words it is a collection of objects. It can also be defined as a blueprint from which the programmer can create an individual object. There is an interesting thing about class that it doesn't occupy any space.
Inheritance is very simple to understand as this is basically the natural process of acquiring all the properties and behaviours of a parent object by the child object. Inheritance is known to provide the code reusability and is very frequently used to achieve the runtime polymorphism.
Polymorphism can be defined as the process of performing one task in many different ways. For instance: In order to convince a client differently, in order to draw something like shape, rectangle, triangle, etc. Polymorphism in Java is basically performed by the help of method overloading and method overriding. In order to understand it perfectly let’s take another instance like expressing feelings, this cis done by various species in various other ways like humans speaks, dog licks the face or wags his tail etc.
Abstraction is basically the process of hiding internal details and displaying the functionality. For instance cash withdrawal from the ATM, the normal person don't know the internal processing. In order to achieve the abstraction in Java, the abstract class and interface is used.
Encapsulation in simple terms is the process of binding (or wrapping) code and data together into a single unit. For instance, a chocolate, it is made with different flavours. A simple example of the encapsulation is the java class. Java bean is known to be the fully encapsulated class as of the fact that all the data members in it are private.
Coupling in simple terms is basically the knowledge or information or dependency of another class. It comes in to the picture when the classes are aware of each other. In case that a class has all the details and information of another class then it is called as strong coupling. In order to display the visibility level of a class, method, and field; the private, protected, and public modifiers are used in Java. As there is no concrete implementation hence, the programmers can use interfaces for the weaker coupling.
The level of a component that executes a single well-defined task is known as the cohesion in Java. A single well-defined task is generally performed by a highly cohesive method. In order to split the task into separate parts, the programmers can use the weakly cohesive method. The java.io package is known to be a highly cohesive package as of the fact that it possess I/O related classes and interface. You should be aware that the java.util package is basically a weakly cohesive package as of the fact that it has unrelated classes and interfaces.
The relationship between the objects is represented by the association. The basic concept is that one object can be associated with one of the objects or many other objects. Associations in Java can only be unidirectional or bidirectional.
There are basically four types of association that exists between the objects that are: One to One, One to Many, Many to One, and Many to Many. Let's get to know the relationship with real-time examples. For instance, One office can have one CEO (one to one), and a CEO can have many managers (one to many). Also, many managers can have one CEO (many to one), and many managers can have many departments (many to many).
A method or way to achieve Association in Java is called Aggregation. Aggregation in Java basically represents the relationship in which one object consists of other objects as a part of its state. It also represents the weak relationship that exists between the objects. In simple terms it can be said that it has a relationship in Java. It is basically defined as another way to reuse the objects.
In order to achieve Association in Java, there are various ways and the composition is one of them. It basically represents the relationship in which one object consists of other objects as a part of its state. In Java there is a powerful relationship between the containing object and the dependent object. It is basically the state that represents the fact that a containing object do not have an independent existence. All the child objects will be automatically deleted if the programmer delete the parent object.
Here are the advantages of OOPs over the Procedure-oriented programming language that depicted below:
Let's understand the difference between an object-oriented programming language and object-based programming language ?
All the features of OOPs are followed by the Object-based programming language and the only exception is Inheritance. The examples of object-based programming languages are JavaScript and VBScript.