Wednesday, September 19, 2018

OOPS Concepts in Java


Java is one of the many programming languages ​​and technologies supported by Stackify's main tools, Retrace and Prefix. As in the Stackify we intend to help developers to become better developers, we are analyzing some of the fundamental concepts of the Java programming language. Read about a primer on OOP concepts in Java.

Definition of OOP Concepts in Java

The concepts of OOP in Java are the main ideas behind Java object-oriented programming. They are an abstraction, encapsulation, inheritance and polymorphism. Grabbing them is essential to understand how Java works. Basically, Java OOP concepts allow us to create methods and work variables, and then reuse all or part of them without compromising security.

List of OOP Concepts in Java

There are four main concepts of OOP in Java. Those are:
Abstraction. Abstraction means using simple things to represent complexity. We all know how to connect TV, but we do not need to know how it works to take advantage. In Java, abstraction means that simple things like objects, classes and variables represent more complex underlying codes and data. This is important because it avoids repeating the same job several times.
Encapsulation. This is the practice of keeping the fields within a private class and then providing access to them through public methods. It is a protective barrier that keeps data and code safe within the class itself. In this way, we can reuse objects as code Encapsulation components or variables without allowing open access to the entire data system.
Inheritance This is a special feature of object-oriented programming in Java. It allows programmers to create new classes that share some of the attributes of existing classes. This allows us to build the previous work without reinventing the wheel.
Polymorphism. This Java OOP concept allows programmers to use the same word to mean different things in different contexts. One form of polymorphism in Java is the overload of methods. It is when different meanings are implicit in the code itself. The other way is the substitution of methods. It is when the different meanings are implicit in the values ​​of the variables provided. See more about this below.

How OOP Concepts in Java Work

OOP, concepts in Java work by letting programmers create components that can be re-used in different ways, but still maintain security.

How Abstraction Works

Abstraction as an OOP concept in Java works by allowing programmers to create useful and reusable tools. For example, a programmer can create several different types of objects. These can be variables, functions or data structures. Programmers can also create different kinds of objects. These are the ways to define the objects.
For example, a variable class can be an address. The class can specify that each address object must have a name, street, city and zip code. The objects, in this case, can be employee addresses, customer addresses or supplier addresses.

How Encapsulation Works

The encapsulation allows us to reuse functionality without compromising security. It's a powerful OOP concept in Java because it helps us save a lot of time. For example, we can create a part of the code that calls specific data from a database. It may be useful to reuse this code with other databases or processes. The encapsulation allows us to do this while keeping our original data secret. It also allows us to change our original code without breaking it for other people who adopted it at that time.

How Inheritance Works

Inheritance is another Java OOP concept that saves labor. It works by allowing one new class to adopt the properties of another. We call the class inherited from a subclass or from a daughter class. The original class is often called father. We use the keyword to define a new class that inherits the properties of an old class.

How Polymorphism Works

The polymorphism in Java works by using a reference to a parent class to affect an object in the child class. We can create a class called "horse", extending the "animal" class. This class can also implement the "professional career" class. The "horse" class is "polymorphic", since it inherits the attributes of the "animal" and "professional" class.

Two other examples of polymorphism in Java are method substitution and method overload.

In the method overriding, the secondary class can use the concept of OOP polymorphism to replace a method of its parent class. This allows a programmer to use a method in different ways, depending on whether it is invoked by an object in the primary class or by an object in the child class.

In the method overloading, a single method can perform different functions depending on the context in which it is called. That is, a single method name can work in different ways, depending on what arguments are passed to it.

Best Practices for OOP Concepts in Java

Since the objective of the concepts of OOP in Java is to save time without sacrificing security and ease of use, best practices are all aimed at advancing this main goal.

  • ·        DRY (Don’t Repeat Yourself). This is the central concept in Java. You should never have two blocks of identical code in two different places. Instead, use a method for different applications.


  • ·        If you expect your Java code to change in the future, encapsulate it by making all variables and methods private at the outset. As the code changes, increase access to “protected”  as needed, but not too public.


  • ·        Single Responsibility. Another best practice for OOP concepts in Java is the Single Responsibility Principle. Simply put, a class should always have only  one functionality. That way, it can be called and/or extended on its own when new uses arise for it, without causing coupling between different functionalities.


  • ·        Open Closed Design. Make all methods and classes Closed for modification but Open for an extension. That way, tried and tested code can remain static but can be modified to perform new tasks as needed.




No comments:

Post a Comment

From Java 8 to Java 11

Switching from Java 8 to Java 11 is more complicated than most updates. Here are some of my notes on the process. Modules Java 9 i...