OOPs Concepts In Java

Object-oriented programming (OOPs) is a technique of programming that consists of identifying classes of objects that have a strong connection to the methods (functions) associated with them. It is a method for storing data with the procedures required for processing them, based on a mathematical discipline known as "abstract data types”.

In OOPs, classes and objects are used as the fundamental analysis and design units. A program is categorized into reusable pieces of code called blueprints (typically called classes). The blueprints are then used as the basis for creating instance objects. Let us continue with the basic concepts of OOPs.

Basic Concepts of OOPs

1. Objects

Objects are the first things to think about when designing a program, as they are the units of code that are finally derived from the process. To bridge this gap, every object is first generalized into a class of things. Then, more types are defined so that entities can share their models and reuse their class definitions.

Objects are specific classes or subclasses with methods and variables specific to a particular category. They are the key elements of a computer program.

2. Classes

In OOPs, classes contain a blueprint for creating objects (a particular data structure), as well as the initial state (member variables or attributes), having the implementation of behavior (member functions or methods).

The class keyword is used to create user-defined objects. A class is described by a blueprint that specifies the nature of a future object. The instance of a class is an actual, distinct object. OOPs uses styles to create and manage new objects and support inheritance – a key component of reusing code.

There are different classes of objects, which are abstract blueprints to create more tangible things. A class can refer to a broad category, such as a car or dog, with similar characteristics. Class attributes are defined for specific colors, but the values are not designated for specific objects.

The methods in classes are available to only the objects belonging to that class.

3. Abstraction

We abstract data by creating complex data types and hiding their implementations. We do not go into how these data types are implemented; only the operations to manipulate them are exposed. Furthermore, we can change the implementation without affecting the behavior presented to the user with this approach, which has the advantage of being flexible.

4. Inheritance

OOPs emphasizes inheritance as its fundamental principle. Inheritance's function is to enable code reusability. It is simpler to inherit the properties of one class into another rather than using the same code repeatedly.

A class can inherit the methods and attributes of another class by inheriting them from that particular class. As the name implies, the parent class is from which all properties and methods are inherited. The class inheriting properties from the parent class is known as the child class.

5. Polymorphism

An object-oriented programming language has a feature called polymorphism that allows a routine to use variables of different types at particular points in time. Polymorphism provides the same interface for different kinds of data underlying a programming language. Each object responds to the same message differently. This phenomenon is called polymorphism. To learn more about OOPs concepts in Java, you must understand the basics like polymorphism.

6. Encapsulation

The concept of encapsulation is fundamental to OOPs. Combined data analysis refers to the combination of methods for processing the data. A structured data object's values or state is hidden inside a class, preventing unauthorized parties from accessing them directly.

The class usually provides public methods, so-called getters, and setters, to access the values, and other client classes use these methods to perform operations on the objects they access.

It's the process of condensing and encapsulating a number of data elements into one unit. This mechanism joins the data it manipulates with the code that makes them work. The encapsulation concept can also be viewed as a protective shield that stops outside code from accessing the data.

6.1 Cohesion in Java
The cohesion of a Java class can be described as the object-oriented principle that assures a class is designed with a single, focused purpose in mind. A class’ cohesion is determined by how it is intended in object-oriented design. It is easier to maintain high-cohesion classes than low-cohesion ones (less frequently changed). High cohesion also has the advantage of making classes with a well-defined purpose more reusable.

6.2 Association in Java
Object-oriented programming is based on the fundamental concept of association, which is one of Java's building blocks. A relationship or connection between two class instances is an association.

These two classes are shown as being associated with each other. Associations define the number of objects related to each other and connect the classes.

6.3 Aggregation
When applied to OOPs, an aggregate refers to an object relationship between two or more objects, wherein each has its independent life cycle and ownership relationships. OOPs combines two words (aggregation and OOPS) to form a single word. In semantics, an aggregate is a type of weak relation that occurs between unrelated objects.

6.4 Composition in Java
Java's composition approach implements a has-a relationship using a design technique. The Java Inheritance mechanism is used to reuse code, and the same can be done by using composition. When instance variables reference underlying objects, the document is obtained.

Methods in Java

In Java, a method is a block of code or a group of statements that work together to accomplish a particular task. Methods are used to make code reusable.

Once a method is defined, it can be reused indefinitely. Moreover, it provides easy modification and readability of code. Methods are only invoked when they are called.

As with all programming languages, Java has two methods: pre-definition and user-definition. Methods defined in class libraries are called predefined methods. A method written by an individual or program is called a user-defined method in computer programming. Custom-made methods enable developers to modify a method to fit their specific needs.

Let's discuss:

  • Static method
  • Finalize method
  • The abstract method
  • Equals method

1. Static Method

The static method is one whose declaration contains the static keyword. On the other hand, randomly allocated methods belong to an entire class rather than a specific instance.

Using the keyword ‘static’ before the method's name can also result in a static method. Static methods are useful because they can be invoked without creating a new object. They can access o static data members and modify them. An instance method is developed from static data members.

2. Abstract Method

A method whose declaration contains the word ‘abstract’ is called an abstract method. There is no implementation, individuals, and code block in the abstract method. If a class has an abstract method, it must be declared abstract. One can also say the class should be declared abstract if it has an abstract method.

Abstract classes don't need a mandatory abstract method since they are abstract if they have an abstract method, but vice versa is not valid. Furthermore, if an ordinary class extends an abstract class, then the normal class must implement all the abstract methods of the abstract parent class or be declared abstract.

3. Final Method

This term is used to describe a method that has been declared final. A final method cannot be overridden. As a result, the child class can still call the parent class’ final method, but it cannot override it. Making a method final aims to prevent a subclass from modifying it.

4. Equals Method

The .equals() method in Java compares two objects to determine if they are equal. The .equals() method checks whether the two strings have similar values by performing a character-by-character comparison.

You should not confuse the .equals() method with the == operator. Using the .equals() method, two strings are compared based on content, while the == operator uses an address comparison. The .equals() function returns true if the contents of both strings match; otherwise, it returns false. The function returns false if all characters do not match.

Summary

As Java adheres to the OOPs philosophy, programs are structured around objects and well-defined interfaces. Abstractions, encapsulation, inheritance, and polymorphism are the main OOPs concepts in Java. Object-oriented programming uses these concepts to implement objects remotely integrated into a program.

If you're interested in learning Java, enroll in Board Infinity's Core & Advanced Java course. Learn the fundamentals of core and advanced Java from leading industry experts and get certified upon successful completion of the course.