OOP Concepts in software development

A brief introduction to the OOP concept in Java.

An object is a real-world entity that has both properties(states) and behaviors(methods). For ex. a book item can have properties like title, author, price, publishers, etc, and can have methods like getBookTitle, getBookPrice, etc. similarly in our software system there can be many entities that can be treated as an object. Let us take the example of e-commerce sites there can be many entities like shopping cart, customer, product, order, order item, etc. which can be designed as objects. A customer object can have properties like username, email, password, address, phone number, etc., and different methods to retrieve and set those properties.

So object-oriented analysis focus on designing a system by treating every entity as an object, finding the relationship between objects, making the interface for each object, and writing code that can be converted to executables. UML(Unified Modeling Language ) is a tool used by developers to perform object-oriented analysis.

Following is a brief description of different OOP concepts.

Objects: Objects represent a real-world entity that has properties and methods. An object can also contain some other object. For example, an library management system can have objects like books, students, etc.

Class: Class is the blueprint or we can say prototype of objects. This decides the nature and behavior of an object.

Encapsulation: Encapsulation is a way of binding data and methods that operate on that data to a single unit so that data is not accessed from outside our class. We provide methods to access those data through methods.

Abstraction: Abstraction is just an extension of encapsulation. While encapsulation deals with data security, abstraction is used to expose only relevant methods to end-user by hiding the internal implementation. In java, we have a concept of abstract class and interfaces where we declare methods. The class who implement those abstract class and interface override the methods.

Inheritance: Inheritance is a mechanism by which a child object acquires all properties and behavior from the parent object. Parent and child class has Is-a relationship between them. For ex. a dog is an animal, dog class inherits all properties and behavior of the animal class.

Polymorphism: Polymorphism means the ability to take different forms. An object can take a different form based on the way the object is called. In the case of method overloading an object method can exist in different forms in a class or its subclass which is also called compile-time polymorphism. In inheritance a method overriding way to achieve polymorphism in which child class implements a method in a different way than parent class which is also called runtime polymorphism.

Class is the blueprint of the object. Class represent all common properties and methods which will be available in all instance of this class. A class has the following components.

1. Access modifiers: Access modifiers specifies the availability or accessibility of class, methods, constructor, or fields. There is 4 type of access modifiers in java. which are as following.

  1. Private: Access level is only within the class.
  2. Default: Access level is only within the package.
  3. Protected: Access level is only within the package. By child class, it can be accessed from another package.
  4. Public: This can be accessed from any package.

2. Class Keyword: In Java class is created using the class keyword.

3.ClassName: Classes are named using the PascalNaming convention in which the first letter of each word is in upper case. Methods are named as per camelNaming convention in which the first letter of the first word is lower rest as per pascal convention

4. Class body: All content of the class is inside the curly bracket {}.

Object: Object is an instance of the class. It has state(fields) and behavior(methods).

Instantiation(Declaration) of Object: It is declared like a primitive variable. Which will be useful to refer to the object of type, here the Triangle object is instantiated. Triangle triangle;

Initialization of object: Object is initialized by a new keyword. By default constructor without argument is used by the java compiler to create an object. If we initialize through parametrized constructors we have to create all types of constructors.

Encapsulation

Encapsulation is a mechanism of hiding data so that it can not be manipulated by end user. In the below, example the points field is protected. It can be initialized with a value between 1 to 100. Our data and methods are wrapped inside a class.

Abstraction is a way to hide the internal implementation of any function/method. For example, when we call an object, we call by method name we don’t know how this method is implemented.

Abstraction by abstract class

Abstraction by Interface

We use an interface when more than one class has the same functionality. So instead of overriding everywhere we just override at one place.

Inheritance

Inheritance is the mechanism by virtue of which the child class inherits from the parent class. Types of inheritance.

  1. Single inheritance: When children inherit from a single parent.
  2. Multiple inheritance: When children inherit from more than two-parent.Although Java doesn't support multiple inheritances. It can be achieved through interfaces.
  3. Multi-Level inheritance: When a child has a grandparent in a linear fashion
  4. Hierarchical inheritance: When two children inherit from the same parent.
  5. Hybrid inheritance: when inheritance is a mix of more than one above type.

Polymorphism

Polymorphism is the property of an object to exist in two or more forms. In OOP polymorphism can be achieved via overriding or overloading. An overriding method with the same name can exist in different forms in parent and child. We call it run time polymorphism because at runtime it is decided by the compiler after looking at the type of object. In case of overloading methods in the same class or subclass have the same name but different arguments.

Below is an example of method overloading also called compile-time polymorphism.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Aditya Kumar

Hi, I am Aditya. I studied at IIT BHU Varanasi , India. I am a Java developer. I partricipate in competitive programming.