Object means a real word entity such as pen, chair, table etc. Object-Oriented Programming is a methodology or paradigm to design a program using classes and objects. Object-oriented programming (OOP) is programming using "objects" to develop computer programs. There are three main features of oops objects, encapsulation, inheritance, and polymorphism, have data (fields) and behavior (methods) and do the work in an object-oriented program. OOps is a programming paradigm that uses "objects" to design applications and computer program. The Simula programming language was the first to introduce the concepts underlying object-oriented programming (objects, classes, subclasses, virtual methods, coroutines, garbage collection and discrete event simulation) as a superset of Algol. Smalltalk was the first programming language to be called "object-oriented".
Some of the essential elements of the object oriented programming:
- Three OOps principles
- Composition / Aggregation
- Three OOps principles
- Message, Method and Object Interaction
- Composition / Aggregation
Objects are the real world entities. Entities are something which has extension in real world. If we consider the real-world we can find many objects around us, Cars, Dogs, Humans, etc. All these objects have a state and behavior. Software objects also have a state and behavior. A software object's state is stored in fields and behavior is shown via methods.
Any entity that has state and behavior is known as an object. For example: chair, pen, table, keyboard, bike etc. It can be physical and logical.
An object has three characteristics:
represents data (value) of an object.
(Methods / Function / Operatoins)represents the behavior (functionality) of an object such as deposit, withdraw etc.
Object identity is typically implemented via a unique ID. The value of the ID is not visible to the external user. But,it is used internally by the JVM to identify each object uniquely.
Example: Pen is an object. Its name is Reynolds, color is white etc. known as its state. It is used to write, so writing is its behavior.
Class is a collection of similar type of object. A class is a blue print for the object. That blueprint describes the each object behaviors and state variable. Collection of objects is called class. It is a logical entity.
A class represent a category of objects by abstracting their properties and behaviour. A class can be thought of as blueprint for creating objects. An object has the properties and behaviour defined by its class. The class can be thought of as a user-defined data type and object as a variable/ instance of the data type represented by the class.
The properites/attribute of an object are defined by data members/ fields in java. A data member/field in a class is a variable that can hold data. The behaviors / operations of an object are defined using methods in java. Both data members and method are reffered to as members of the class.
Abstraction is a process of hiding the implementation details and showing only functionality to the user. It shows only important things to the user and hides the internal details for example sending sms, you just type the text and send the message. You don't know the internal processing about the message delivery.
Inheritance is a process by which features and proprieties of one class ( called super class ) is transferred to another class (called as sub class ) without changing the super class and adding new feature in sub class. It is also called as code reusability. It save the programmer's time. Inheritance defines relationships among classes in an object-oriented language.
The idea behind inheritance in java is that you can create new classes that are built upon existing classes. When you inherit from an existing class, you can reuse methods and fields of parent class, and you can add new methods and fields also.
Inheritance represents the IS-A relationship, also known as parent-child relationship.
When two or more same statements having different meaning then it is called as polymorphism. The most common use of polymorphism in OOP occurs when a parent class reference is used to refer to a child class object. Polymorphism in java is a concept by which we can perform a single action by different ways. Polymorphism is derived from 2 greek words: poly and morphs. The word "poly" means many and "morphs" means forms. There are two types of polymorphism in java: compile time polymorphism and runtime polymorphism. We can perform polymorphism in java by method overloading and method overriding.
Encapsulation is a bundling of member variable and member method together in a class as a single entity is known as called as encapsulation. Encapsulation is the technique of making the fields in a class private and providing access to the fields via public methods. If a field is declared private, it cannot be accessed by anyone outside the class, thereby hiding the fields within the class. For this reason, encapsulation is also referred to as data hiding. for example capsule i.e. mixed of several medicines. Data Hiding is declaring a member has private we can hide them from external objects.
Java is a pure object-oriented programming model, objects interact by sending messages to each other. In the Java model, objects interact by invoking each other's methods. Methods provide access to the information contained in an object. The type of access varies depending on the method. When an object receives a message, the object invokes a method to process the message. The method may change the state of the object, return information contained in the object .
Persistence is the term used to refer to the situation where the objects data is stored even after the object is destroyed or the application is closed. Persistence is implemented using Serialization where the data of the object is serialized into flat files and stored into the system. These files can be de-serialized to form the objects by using de-serialization.
The Java Generics programming is introduced in JDK 5 to deal with type-safe objects. Before generics, we can store any type of objects in collection i.e. non-generic. Now generics, forces the java programmer to store specific type of objects.
Aggregation:It is a specialized form of Association where all object have their own lifecycle but there is ownership. This represents “whole-part or a-part-of” relationship. This is represented by a hollow diamond followed by a line.
Example : relationship between Department and Teacher. A Teacher may belongs to multiple departments. Hence Teacher is a part of multiple departments. But if we delete a Department, Teacher Object will not destroy.
Composition:It is a specialized form of Aggregation. It is a strong type of Aggregation. In this relationship child objects does not have their lifecycle without Parent object. If a parent object is deleted, all its child objects will also be deleted. This represents “death” relationship. This is represented by a solid diamond followed by a line.
Example:relationship between Questions and options. Single questions can have multiple options and option cannot belong to multiple questions. If we delete questions options will be automatically deleted.