Object Oriented Programming System!

OOPs Concept In Java:

Rupam Jha
JavaMadeTranquil
Published in
6 min readFeb 27, 2021

--

Hello Comrades,

We will be looking into one of the most primary concept of Java — the OOPs concept. Basically, Java OOP concepts lets us create working methods and variables, then re-use all or part of them without compromising security.

Object Oriented Programming is a programming paradigm that relies on the concepts of classes and objects. Java is a class-based object-oriented programming (OOP) language that is built around the concept of objects. OOP concepts intend to improve code readability and reusability by defining how to structure a Java program efficiently. The main principles of object-oriented programming are: Abstraction, Encapsulation, Inheritance, Polymorphism, Association, Aggregation and Composition. OOP is used to structure the software program into a simple, reusable code [usually called classes]. The building blocks of OOP concepts are Classes, Objects, methods and attributes. A class is a user defined data type, where we create blueprint for the structures of methods and attributes. Classes contains fields for attributes and methods for behaviours. Objects are instances of classes created with specific data. Attributes are the individuals that differentiate one object from another and determine the appearance, state, or other qualities of that object. Attributes are defined in classes by variables. Method represents behaviour, they perform actions and might return values of an object.

Understanding the OOP Principles:

  • Abstraction: Abstraction means data hiding…. i.e. hiding the complexity of implementation and exposing the essential features. Its main goal is to handle complexity by hiding unnecessary details from the user.

A real life example to relate to: Making coffee with a coffee machine is a good example of abstraction. You need to know how to use your coffee machine to make coffee. You need to provide water or milk and coffee beans, switch it on and select the kind of coffee you want to get. The thing you don’t need to know is how the coffee machine is working internally to brew a fresh cup of delicious coffee. You don’t need to know the ideal temperature of the water or the amount of ground coffee you need to use. You just interact with a simple interface that doesn’t require any knowledge about the internal implementation.

abstraction
  • Encapsulation: Encapsulation is the process of keeping the data and the code that manipulates that data together as a unit. Since encapsulation is more about protecting data from being accessed by other classes so encapsulation is also referred as data hiding. It describes the idea of bundling data and methods that work on that data within one unit. If you’re familiar with any object-oriented programming language, you probably know that these methods as getter and setter methods. As the names indicate, a getter method retrieves an attribute, and a setter method changes it. Depending on the methods that you implement, you can decide if an attribute can be read and changed, or if it’s read-only, or if it is not visible at all. You can use the encapsulation concept to implement an information-hiding mechanism. Similar to the abstraction concept, this is one of the most commonly used mechanisms in Java.

A real life example to relate to: The common example of encapsulation is capsule. In capsule all medicine are encapsulated inside capsule. Basically, capsule encapsulates several combinations of medicine. If combinations of medicine are variables and methods then the capsule will act as a class and the whole process is called Encapsulation.

encapsulation
  • Inheritance: Inheritance is a mechanism by which a class inherits the properties and methods of another class. SuperClass– The class whose members are inherited by another class is known as the superclass (also base class or parent class). SubClass– The inheriting class is known as the sub class (also as derived or child class). Sub class can also add fields and methods of its own other than what it inherits from the parent class. Sub class can also override the method of the parent class. There are many types of Inheritance single inheritance, multi-level inheritance, hybrid inheritance and hierarchical inheritance. Inheritance is a concept that enables you to implement a subclass that extends a superclass. By doing that, the subclass inherits all protected and public attributes and methods, and the types of the superclass. You can then use the inherited attributes of the superclass, use or override the inherited methods, and cast the subclass to any type of its superclass. You can use an abstract class to define a general abstraction that can’t be instantiated. Within that class, you can declare abstract methods that need to be overridden by non-abstract subclasses. That is often used if the implementation of that method is specific for each subclass, but you want to define a general API for all classes of the hierarchy. Java doesn’t support multiple inheritance.

A real life example to relate to: A child and parent, almost all the properties of father are inherited by his child, also a child can have its own properties as well.

inheritance
  • Polymorphism: Polymorphism is a Greek word where poly means “many” and morph means “change from one form to another”. In object oriented terms it relates to the same object reference taking many forms. The concept of polymorphism in Java is designed as an interface having a method and the derived classes implementing the interface as per their requirement. Then using the reference of that single interface any of those implemented class methods can be invoked. So one interface reference can take many forms here based on which class it is referring to. There are two types of Polymorphism in Java- Compile time polymorphism– Also known as static polymorphism. Static polymorphism in Java is achieved through Method overloading as it is known at compile time itself which of the overloaded method will be called. Runtime polymorphism– Also known as dynamic polymorphism. Dynamic polymorphism in Java is achieved through Method overriding. In method overriding it is resolved at run time which of the overridden method would be called.

A real life example to relate to: A human can have multiple behavioral forms.In a shopping mall we behave like customers, in a class we behave like student, at home we behave like kids, at work we behave like an employee.

an electronic item can be of any form: Polymorphism
  • Association: Association is relation between two separate classes which establishes through their Objects. Association can be one-to-one, one-to-many, many-to-one, many-to-many. Composition and Aggregation are the two forms of association. Aggregation is a special form of Association where: it represents Has-A relationship, it is a unidirectional association i.e. a one way relationship. For example, department can have students but vice versa is not possible and thus unidirectional in nature. In Aggregation, both the entries can survive individually which means ending one entity will not affect the other entity. Composition is a restricted form of Aggregation in which two entities are highly dependent on each other. Composition represents part-of relationship. In composition, both the entities are dependent on each other. When there is a composition between two entities, the composed object cannot exist without the other entity.
Association

Hope this article brings in clarity of understanding these OOPs concept of Java.

Until next time…

Peace Out!

Rupam Pawan Jha

--

--

Rupam Jha
JavaMadeTranquil

DevOps Engineer. Automation Enthusiast. Sharing my experiences on CI/CD, Docker, Kubernetes, and IaC.