Java OOPs Concepts

In this online tutorials, we will learn about the basics of OOPs. Object-Oriented Programming is a paradigm that provides many concepts, such as inheritancedata bindingpolymorphism, etc.

Simula is considered the first object-oriented programming language. The programming paradigm where everything is represented as an object is known as a truly object-oriented programming language.

Smalltalk is considered the first truly object-oriented programming language.

The popular object-oriented languages are Java, Python, etc.

OOP concepts in Java are the main ideas behind Java’s Object Oriented Programming. They are an abstraction, encapsulationinheritance, and polymorphism. Grasping them is key to understanding how Java works. Basically, Java OOP concepts let us create working methods and variables, then re-use all or part of them without compromising security.

List of OOP Concepts in Java

There are four main OOP concepts in Java. These are:

  • Abstraction. Abstraction means using simple things to represent complexity. We all know how to turn the TV on, but we don’t need to know how it works in order to enjoy it. In Java, abstraction means simple things like objectsclasses, and variables represent more complex underlying code and data. This is important because it lets avoid repeating the same work multiple times.
  • Encapsulation. This is the practice of keeping fields within a class private, then providing access to them via public methods. It’s a protective barrier that keeps the data and code safe within the class itself. This way, we can re-use objects like code components or variables without allowing open access to the data system-wide.
  • Inheritance. This is a special feature of Object Oriented Programming in Java. It lets programmers create new classes that share some of the attributes of existing classes. This lets us build on previous work without reinventing the wheel.
  • Polymorphism. This Java OOP concept lets programmers use the same word to mean different things in different contexts. One form of polymorphism in Java is method overloading. That’s when different meanings are implied by the code itself. The other form is method overriding. That’s when the different meanings are implied by the values of the supplied variables. See more on this below.

What is Abstraction:

Abstraction is an OOP concept in Java works by letting programmers create useful, 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 classes of objects. These are ways to define the objects.

For instance, a class of variable might be an address. The class might specify that each address object shall have a name, street, city, and zip code. The objects, in this case, might be employee addresses, customer addresses, or supplier addresses.

What is Encapsulation:

Encapsulation lets us re-use functionality without impacting the security. It’s a powerful OOP concept in Java because it helps us save a lot of time. For example, we may create a piece of code that calls specific data from a database. It may be useful to reuse that code with other databases or processes. Encapsulation lets us do that while keeping our original data private. It also lets us alter our original code without breaking it for others who have adopted it in the meantime.

What is Inheritance:

Inheritance is another labor-saving Java OOP concept. It works by letting a new class adopt the properties of another. We call the inheriting class a subclass or a child class. The original class is often called the parent. We use the keyword extends to define a new class that inherits properties from an old class.

What is Polymorphism:

Polymorphism in Java works by using a reference to a parent class to affect an object in the child class. We might create a class called “horse” by extending the “animal” class. That class might also implement the “professional racing” class. The “horse” class is “polymorphic,” since it inherits attributes of both the “animal” and “professional racing” class.

Two more examples of polymorphism in Java are method overriding and method overloading.

In method overriding, the child class can use the OOP polymorphism concept to override a method of its parent class. That allows a programmer to use one method in different ways depending on whether it’s invoked by an object of the parent class or an object of the child class.

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

package com.graasstech;
public class Student {
 private String name;
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name
 }
}
package com.graasstech;
class Test {
 public static void main(String[] args) {
  Student s = new Student();
  s.setName(“graasstech”);
  System.out.println(s.getName());
 }
}

Example of Inheritance in Java

It’s quite simple to achieve inheritance as an OOP concept in Java. Inheritance can be as easy as using the extends keyword:

class Mammal { 
} 
class Aardvark extends Mammal {
}

Short Example of Polymorphism in Java:

Polymorphism as an OOP concept in Java, we have two classes: Person and Employee. The Employee class inherits from the Person class by using the keyword extends. Here, the child class overrides the parent class. For the full example

class Person {
 void walk() {
  System.out.println(“Can Run….”);
 }
}
class Employee extends Person {
 void walk() {
  System.out.println(“Running Fast…”);
 }
 public static void main(String arg[]) {
   Person p = new Employee(); //upcasting p.walk(); 
   } 
}

Apart from these concepts, there are some other terms which are used in Object-Oriented design:

  • Coupling
  • Cohesion
  • Association
  • Aggregation
  • Composition

Coupling

Coupling refers to the knowledge or information or dependency of another class. It arises when classes are aware of each other.

If a class has the details information of another class, there is strong coupling.

In Java, we use private, protected, and public modifiers to display the visibility level of a class, method, and field.

You can use interfaces for the weaker coupling because there is no concrete implementation.

Cohesion

Cohesion refers to the level of a component which performs a single well-defined task. A single well-defined task is done by a highly cohesive method.

The weakly cohesive method will split the task into separate parts. The java.io package is a highly cohesive package because it has I/O related classes and interface.

However, the java.util package is a weakly cohesive package because it has unrelated classes and interfaces.

Association

Association represents the relationship between the objects. Here, one object can be associated with one object or many objects. There can be four types of association between the objects:

  • One to One
  • One to Many
  • Many to One, and
  • Many to Many

Let’s understand the relationship with real-time examples.

Association can be unidirectional or bidirectional.

Aggregation

Aggregation is a way to achieve Association. It is also termed as a has-a relationship in Java. Like, inheritance represents the is-a relationship. It is another way to reuse objects.

Composition

The composition is also a way to achieve Association. . There is a strong relationship between the containing object and the dependent object. It If you delete the parent object, all the child objects will be deleted automatically.

Best Practices for OOP Concepts in Java:

Since the aim of OOP concepts in Java is to save time without sacrificing security and ease of use, the best practices are all oriented toward advancing that main goal.

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.

Reference:

%d bloggers like this: