OOP (Object Oriented Programming) Concepts in JAVA

OOP (Object Oriented Programming) Concepts in JAVA

Object Oriented Programming pivots around the concept of object creation, which is also known as instantiation of a class and thereby using the attributes and features of the object to perform further execution. The reason OOP has become so popular among developers is that it tries to implement real-world concepts in programming namely inheritance, data abstraction, polymorphism and many more. The main of OOP is to bind together data with a particular method in such a way that the data cannot be tampered with via any other function. However, this does not make the data inaccessible to other parts of the code. Some of the concepts of OOP are listed below and elaborated on:

Polymorphism

Suppose a class has several methods with the same name. In such case, it becomes difficult for a computer to differentiate between them. Polymorphism is the feature that differentiates similar entities with the help of signature and entity declaration.

Sample Code

public class Sum
{
public int sum (int x, int y)
{
return (x + y);
}

public int sum (int x, int y, int z)
{
return (x +y + z);
}

public double sum (double x, double y)
{
return (x + y);
}

public static void main (String args [])
{
Sum s = new Sum ();
System.out.println (s.sum (10, 20));
System.out.println (s.sum (10, 20));
System.out.println (s.sum (10.5, 20.5));
}
}

OUTPUT

30
60
31.0

In the above code, all the three methods had the same method name but different method declaration or argument list.

Polymorphism in Java is mainly of the two following types:

  • Overloading
  • Overriding

Inheritance

Inheritance is one of the most key features in OOP. As the name suggests, inheritance is the feature by which one class inherits methods and variables from another class in Java. Some of the important terminology associated with inheritance is:

  • Super Class: Super Class is the class whose features are inherited. It is also often called a base or a parent class.
  • Sub Class: Sub Class is the class that inherits properties from the super class. This is also known as the child class. While the sub class inherits methods and fields from the super class, it also has methods and fields of its own.
  • Reusability: By inheriting fields and methods from a class, reusability of code is also taking place. This is one particular trait that proves to be very beneficial when talking in terms of computational efficiency for long codes. Not only can sub classes use their own properties but also reuse code from the super classes.

Syntax

class derived-class extends base-class
{
//methods and fields
}

Encapsulation

Encapsulation is defined as the ‘wrapping up of data under a single heading or unit’. Encapsulation binds data and code together in a manner such that it acts a protective shield over the code not allowing unwanted access and modification of the source thereof.

  • Encapsulation works when variables and/or data of a particular class is hidden from any other class. The data can only be accessed through the envelope class via a member function or method.
  • For its property, encapsulation is often termed as data-hiding as it protects data and variables from other classes.
  • If you declare all private fields in your class, you can achieve somewhat of an easy encapsulation as the scope of your fields will only remain within the class.

Abstraction

Imagine the basic functionality of an automobile. As a high-level user, all you are concerned with is when you press the accelerator pedal, the car should accelerate and when you press the brake pedal, the car should slow down coming to a final halt. You are not really concerned with the mechanism of the what happens inside when you press the pedals. This is called abstraction. It is the property of only showing users data that is essential to them and hiding the nuances. This also helps group entities. Behaviour of a certain object might conform to a certain group and might not conform to another. Abstraction helps easily classify/group objects.

Class

Classes are user-defined blueprints that are instantiated by objects. Classes can be thought of as object factories. It represents a set of properties or methods that are common to all objects of a particular type. The following components are included in class declarations:

  • Modifiers: This defines the scope of the class. Classes can either be public, private, or protected.
  • Class Name: This is the name of the class and it will be used to create objects. Class names should always start with a capital letter.
  • Superclass: If the class is a subclass, then the declaration should also contain the name of the super class that it extends. In Java, multiple inheritance is not allowed.
  • Interfaces: If a class implements interfaces, these should also be mentioned using commas one after the other,
  • Body: This represents the structure of a class and are enclosed by ‘{ }’. Classes contain methods inside them.

Object

The basic unit of OOP is an object. An object is an instance of a class. Several objects are created in a single program. An object has the following properties:

  • State: This gives a picture of the attributes of the object.
  • Behavior: Determined by the methods related to the object.
  • Identity: Every object has a unique name such that objects can interact among themselves.

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.