Best blog on OOPs in java for beginners

The only blog you need to read to understand concept oops in java with lots of examples.

ยท

5 min read

What is OOP?

Object-Oriented Programming (OOP) is an approach to program organization and development, which attempts to eliminate some of the pitfalls of conventional programming methods by incorporating the best of structured programming features with several new concepts.

Everything got above my head.. -_-

Let me explain. OOps is an approach that provides a way of modularizing (organise something in modules or sections) programms by creating partitioned memory area for both data and methods that can by used as templates for creating copies of such modules on demand.

languages that support OOP

  • Smalltalk
  • Objective C
  • C++
  • Ada
  • Object Pascal
  • Java

OOP is a word that is understood differently by different people that's why it is important to understand the basic concepts and some general terms of OOP which form the heart of Java language.

Objects

Objects are the basic runtime entities in an object-oriented system. Anything that exist in a real world is an object. eg: a person, a table of data, a bank account, etc. When a program is executed, the objects interact by sending messages to one another. For example, 'teacher' and 'student' are two objects in a school. A teacher object may send a message to the student object asking any question from the topic being discussed by her.

Objects can intereact without having to know the details of each other's data or code (properties). It is sufficient to know the type of message accepted and the type of response returned by the objects. In our example it is not important for a teacher to know the name of student or whatever he does in his daily life. The only thing she is interested in is knowing the message/response returned by the student.

Each object contains data and methods(code to manipulate the data) data is something that the object knows already. And method is how the objects responds. If you are an object then your name, your age, your address, etc. will be your data and the things you can do like go to school, go to house, play games, etc. are you methods.

java-class-objects.webp

Classes

The entire set of data and code of an object can be made a user-defined data using the concept of class. Once a class is defined, we can create any number of objects belonging to that class. example: dog, cat, cow, horse and lion are members of the class animals. Classes are user-defined data types and behave like the built-in types of a programming language. Basically it represents the set of properties or methods that are common to all objects of one type.

Some terms used in OOPs

Data Abstraction & Encapsulation

The act of putting various components into a single unit(class) is known as encapsulation. The data is not accessible to the outside world nad only those methods which are in the class can access it. This insulation of the data from direct access of the program is called data hiding. Encapsulation makes it possible for each object to perform a specific task without any concern of internal implementation.
example: Car ๐Ÿš˜ is a single entity with music player + air conditioner in a single box.

1_CLBzWEo22SXvh-0dT3eV_w.png

Abstraction refers to the act of representing essential features without including the background details or explanations. Basically hiding internal details.

This concept can better be understood by considering the example of a mobile phone. It is a complex device, which is used for many purposes. How it works internally, how it enable the user to talk to any person is not a concern of the user. He is only concerned about th fact that by pressing the green button he can talk to the person on the other side. Thus, the user is isolated from all the complex circuitry which is well encapsulated within the mobile phone. Think of some standard operations that can be performed by all the mobile phones. like sending and receiving messages, calls, etc. Also consider some data elements i.e., address book, inbox, etc. We can refer to these sets of data as an abstraction of a generic mobile phone. We can say that all mobile phones from different manufactures are nothing but different instances of this common abstraction of a generic mobile phone.

Inheritance

Inheritance is the process by which objects of one class acquire the properties of objects of another class. It is simply the act of deriving new things from the existing things. In OOP, the concept of Inheritance provides the idea of reusability.

0_PCrObyBu1ektfPWd.png This means we can add additional features to an existing class without modifying it. This is possible way of deriving a new class from the existing one. This class will have the combined features of both the classes. In Java, the derived class is known as 'subclass'.

Polymorphism

Polymorphism means the ability to take more than one form. An operation may exhibit different behaviour in different instances. For example, a Smartphone has a behaviour of a phone, calculator and a music player.

original.png there's a common speak function but it is taking different form depending upon the animal.

Polymorphism plays an important role in allowing objects having different internal structures to share the same external interface. It is extensively used in implementing inheritance.

Benefits of OOP

  • Through inheritance, we can eliminate redundant code and extend the use of existing classes.
  • We can build programs from the objects rather than having to start writing the code from scratch.
  • Principle of data hiding helps the programmer to build secure programs that can't be invaded by code in other parts of program.
  • It is easy to work in a project based on objects.
  • Software complexity can be easily managed. A software that is easy to use is hard to build. It is hoped that thhe object-oriented programming languages like C++ and Java would help manage this problem.

Summary

Java is an object-oriented language. It enables us not only to organize our prohram code into logical units called objects but also to take advantage of encapsulation, inheritance, and polymorphism. I've introduced the basic concepts of bject-oriented programming which include Encapsulation, Inheritance and Polymorphism.

Did you find this article valuable?

Support Akash Dev's Blog by becoming a sponsor. Any amount is appreciated!

ย