Object oriented programming in Java

Abhinav Jha
4 min readFeb 8, 2021
Photo by Clément Hélardot on Unsplash

Object oriented programming can be defined as a programming model which is based upon the concept of objects. It is used to structure a software program into simple, reusable pieces of code blueprints, which are used to create individual instances of objects. There are many object-oriented programming languages including JavaScript, C++, Java, and Python.

what are objects?object is collection of states(attributes) and behaviour (methods) acting upon those attributes. like wise in the real world where in our day to day activity we are always using object based conceptualization to deal with real world, in oops paradigm programs are formulated to create objects and those objects are made to interact with each other to complete the task.

for example, in real world. Take a bird as a object. bird’s wings, eyes, legs can be thought of as states of birds . and action of flying, action of seeing can be thought of as method being performed by bird object.

How to create Objects?multiple objects can be created of the same kind and the different kinds. To facilitate the creation of objects easily , most of the languages provides concept of class.

class can be defined as a blue print of objects from which individual objects are created .where all the data attributes, methods are defined and created respectively. class also contains constructor which has sole responsibility of creation of object. for creating a object this constructor must be called. multiple constructor can be provided explicitly to a class as required by author. if not single constructor is provided, then java compiler implicitly built a constructor for class.

example of class and object creation:-

output:-

My name is Amit

How to make use of the OOP paradigm? while designing a class structures for solving given problem, we should take care of four principles of object oriented programming. which are as follows:

  1. Encapsulation
  2. Abstraction
  3. Inheritance
  4. Polymorphism

what is Encapsulation?

Encapsulation means containing all important information inside an object, and only exposing selected information to the outside world. Attributes and behaviors are defined by code inside the class template.

Encapsulation defines some fields with access specifier some as private and some as public.

  • Private: methods and properties, accessible from other methods of the same class.
  • Public: methods and properties, accessible also from outside the class.

example:

Amit

What is Abstraction?

Abstraction means that the user interacts with only selected attributes and methods of an object. Abstraction uses simplified, high level tools, to access a complex object.Hides complex details from user. Abstraction is an extension of encapsulation.

output:-

Suzuki::accelerate

What is Inheritance?

Inheritance allows classes to inherit features of other classes. in another way, parent classes extend attributes and behaviors to child classes. inheritance supports reusability. If attributes and behaviors are defined in a parent class, child classes can be created extending the functionality of the parent class, and adding additional attributes and behaviors.

below are the types inheritance which can be performed in java:-

  1. Single Inheritance: In single inheritance, sub classes inherit the features of one parent class.

output:-

One
Two

2. Multilevel Inheritance:In Multilevel Inheritance, a derived class will be inheriting a base class and as well as the derived class also act as the base class to other class.

output:-

One
Two
Three

3. Hierarchical Inheritance : In Hierarchical Inheritance, one class serves as a super class (base class) for more than one sub class.

output:

One
One

4. Multiple Inheritance:n Multiple inheritance ,one class can have more than one parent class and inherit features from all parent classes. Java does not support multiple inheritance with classes. In java, we can achieve multiple inheritance only through Interfaces.

output:

One
Two

What Is Polymorphism?

The word polymorphism means having many forms. Polymorphism allows us to perform a single action in different ways. In other words, polymorphism allows you to define one interface and have multiple implementations.

In Java polymorphism is mainly divided into two types:

  • Compile time Polymorphism
  • Runtime Polymorphism
  1. Compile time Polymorphism: It is also known as static polymorphism. This type of polymorphism is achieved by function overloading

Output:

8
42.

2. Runtime Polymorphism: it is also known as dynamic polymorphism. Because in it function call to the overridden method is resolved at Runtime. This type of polymorphism is achieved by Method Overriding. method overriding, on the other hand, occurs when a derived class has a definition for one of the member functions of the parent class. That parent function is said to be overridden.

output:

subclass1
subclass2

Conclusion:

Apart from oop, there is also procedural programming. in this mode of programming if something goes wrong, then one has to go through various functions and make modifications. but on the other hand in oop one has to look for an object and modify it. oop also provides security and flexibility by encaspsulation, polymorphism respectively. That is why to build large build projects developers prefer oop based language.

--

--