OOP (Object Oriented Programming) Concepts in JAVA

OOP (Object Oriented Programming) Concepts in JAVA

Object oriented programming pivots around the concept of object creation. This is also known as an instantiation of a class, 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 concept of OOP is to bind together data with a particular method so 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 a 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 three methods had the same method name but different method declaration or argument lists.

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:

  • Superclass: Superclass is the class whose features are inherited. It is also often called a base or a parent class.
  • Subclass: Subclass is the class that inherits properties from the superclass. This is also known as the child class. While the subclass inherits methods and fields from the superclass, 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 subclasses use their own properties but also reuse code from the superclasses.

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’. It binds data and code together in a manner such that it acts as 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. Only a member function or method can access the data through an envelope class.
  • 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 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. The 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. Class declarations include the following components:

  • 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 superclass that it extends. Java does not allow multiple inheritance.
  • Interfaces: If a class implements interfaces, these should also be mentioned using commas one after the other,
  • Body: ‘{ }’ encloses the body and represents the structure of the class. Classes contain methods inside them.

Object

The basic unit of OOP is an object. An object is an instance of a class. A single program creates several objects. An object has the following properties:

  • State: This gives a picture of the attributes of the object.
  • Behaviour: The methods related to the object determine the behaviour.
  • 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.