Basic or Features Object Oriented Programming (OOP)

 Basics of Object Oriented Programming

Object Oriented Programming (OOP) is a technique in which programs are written on the basis of objects. An object is a collection of data and functions. Object may represent a person, thing or place in real world. In OOP data and all possible functions are grouped together. Object oriented programs are easy to learn and modify. Some of the object-oriented languages are

· C++

· JAVA

· C# etc.

Features of Object-Oriented Programming

Object: OOP provide the facility of programming based on objects. An object is a collection of data and functions.

Class: classes are designed for creating objects. OOP provides the facility to design classes for creating different objects. All properties and functions of an object are specified in classes.

Encapsulation: Encapsulation is a process of combining data members and functions in a single unit. Class is the example of encapsulation. It is one of the popular feature of Object Oriented Programming(OOPs) that helps in data hiding.

Abstraction: Abstraction refers to showing only the necessary details to the intended user. For example: phone call, we don't know the internal processing.

Inheritance: Inheritance is a technique that allows a programmer to use the code of existing program to create new programs.

Polymorphism: is an ability of object to behave in multiple ways.

Objects

An object represents an entity in the real world such as a person, thing or concept etc. An object is identified by its name. An object consists of the following two things:

• Properties: Properties are the characteristics of an object.

• Functions: Functions are the action that can be performed by an object.

Classes

A collection of objects with same properties and functions is known as class. A class is used to define the characteristics of the objects. It is used as a model for creating different objects of same type. For example, class Person can be used to define the characteristics and functions of a person. It can be used to create many objects of type Person such as Ali, Usman, Abdullah etc. All objects of Person class will have same characteristics and functions. However, the values of each object can be different. The values are of the objects are assigned after creating an object. Each object of a class is known as an instance of its class. For example, AIi , Usman and Abdullah are three instances of a class Person. Similarly, mybook and yourBook can be two instances of a class Book.

Declaring a Class

A class is declared in the same way as a structure is declared. The keyword class is used to declare a class. A class declaration specifies the variables and functions that are common to all objects of that class. The variable, declared in a class are known as member variables or data members. The functions declared in a class are called member functions.

 

Syntax

The syntax of declaring a class is as follows:

class identifier

{

body of the class

}; 

class: It is the keyword that is used to declare a class.

Identifier: It is the name of the class. The rules for class name are same as the rules for declaring a variable. The class declaration always ends with semi colon. All data members and member functions are declared within the braces known as body of the Class.

Example

class Test

{

int n;

char c;

float x;

};

Access Specifiers

The commands that are used to specify the access level of class members are known as access specifiers . Two most important access specifiers are as follows:

The private Access Specifier

The private access specifier is used to restrict the use of class member within the class. Any member of the class declared with private access specifier can only be accessed within the class. It cannot be accessed from outside the class. it is also the default access specifier. The data members are normally declared with private access specifier. It is because the data of an object is more sensitive.

The private access specifier is used to protect the data member from direct access from outside the class. These data members can only be used by the functions declared within the class.

The public Access Specifier

The public access specifier is used to allow the user to access a class member within the class as well as outside the class. Any member of the class declared with public access specifier can be accessed from anywhere in the program. The members functions are normally declared with public access specifier.



 

The above figure shows that data members a, c and x of class Test cannot be accessed from outside the class because they are declared with private access specifier. The member functions show() and input() are accessible from outside the class because they are declared with public access specifier.

 

Creating Objects

 An object is created in the same way as other variables are created. When an object of a class is created, the space for all data members defined in the class is allocated in the memory according to their data types. An object is also known as instance. The process of creating an object of a class is also called instantiation.

Syntax

class_name object_name;

Example:           Test obj;

Executing Member Functions

An object of a particular class contains all data members as well as member functions defined in that class. The data members contain the value related to the object. The member functions are used to manipulate data members. The member functions can be executed only after creating an object.

Syntax

object_name.memberfunction( );

Example:         Test obj;       

                      obj.input( );

Java OOPs Concepts:

We will learn about the basics of OOPs. Object-Oriented Programming is a paradigm that provides many concepts, such as inheritancedata bindingpolymorphism, etc.

Simula is considered the first object-oriented programming language. The programming paradigm where everything is represented as an object is known as a truly object-oriented programming language.

Smalltalk is considered the first truly object-oriented programming language.

The popular object-oriented languages are JavaC#PHPPythonC++, etc.

The main aim of object-oriented programming is to implement real-world entities, for example, object, classes, abstraction, inheritance, polymorphism, etc.

OOPs (Object-Oriented Programming System)

Object means a real-world entity such as a pen, chair, table, computer, watch, etc. Object-Oriented Programming is a methodology or paradigm to design a program using classes and objects. It simplifies software development and maintenance by providing some concepts:

Object

Class

Inheritance

Polymorphism

Abstraction

Encapsulation

Apart from these concepts, there are some other terms which are used in Object-Oriented design:

Coupling

Cohesion

Association

Aggregation

Composition


Object

 


Any entity that has state and behavior is known as an object. For example, a chair, pen, table, keyboard, bike, etc. It can be physical or logical.

An Object can be defined as an instance of a class. An object contains an address and takes up some space in memory. Objects can communicate without knowing the details of each other's data or code. The only necessary thing is the type of message accepted and the type of response returned by the objects.

Example: A dog is an object because it has states like color, name, breed, etc. as well as behaviors like wagging the tail, barking, eating, etc.

Class

Collection of objects is called class. It is a logical entity.

A class can also be defined as a blueprint from which you can create an individual object. Class doesn't consume any space.

Inheritance

When one object acquires all the properties and behaviors of a parent object, it is known as inheritance. It provides code reusability. It is used to achieve runtime polymorphism.

Inheritance


 

Polymorphism

If one task is performed in different ways, it is known as polymorphism. For example: to convince the customer differently, to draw something, for example, shape, triangle, rectangle, etc.

In Java, we use method overloading and method overriding to achieve polymorphism.

Another example can be to speak something; for example, a cat speaks meow, dog barks woof, etc.

Abstraction

Hiding internal details and showing functionality is known as abstraction. For example phone call, we don't know the internal processing.

In Java, we use abstract class and interface to achieve abstraction.

Abstraction in Java , C++ , OOP
Abstraction

 

Encapsulation

Binding (or wrapping) code and data together into a single unit are known as encapsulation. For example, a capsule, it is wrapped with different medicines.

A java class is the example of encapsulation. Java bean is the fully encapsulated class because all the data members are private here.

Coupling

Coupling refers to the knowledge or information or dependency of another class. It arises when classes are aware of each other. If a class has the details information of another class, there is strong coupling. In Java, we use private, protected, and public modifiers to display the visibility level of a class, method, and field. You can use interfaces for the weaker coupling because there is no concrete implementation.

Cohesion

Cohesion refers to the level of a component which performs a single well-defined task. A single well-defined task is done by a highly cohesive method. The weakly cohesive method will split the task into separate parts. The java.io package is a highly cohesive package because it has I/O related classes and interface. However, the java.util package is a weakly cohesive package because it has unrelated classes and interfaces.

Association:     

Association represents the relationship between the objects. Here, one object can be associated with one object or many objects. There can be four types of association between the objects:

One to One

One to Many

Many to One, and

Many to Many

Let's understand the relationship with real-time examples. For example, One country can have one prime minister (one to one), and a prime minister can have many ministers (one to many). Also, many MP's can have one prime minister (many to one), and many ministers can have many departments (many to many).

Association can be unidirectional or bidirectional.

Aggregation

Aggregation is a way to achieve Association. Aggregation represents the relationship where one object contains other objects as a part of its state. It represents the weak relationship between objects. It is also termed as a has-a relationship in Java. Like, inheritance represents the is-a relationship. It is another way to reuse objects.

Composition

The composition is also a way to achieve Association. The composition represents the relationship where one object contains other objects as a part of its state. There is a strong relationship between the containing object and the dependent object. It is the state where containing objects do not have an independent existence. If you delete the parent object, all the child objects will be deleted automatically.

 

Comments

  1. This article is very useful for my assignment

    ReplyDelete
  2. Very helpful
    You are doing a great job keep it up bro 💯

    ReplyDelete
  3. I found it very helpful for my academic assignment. Hope to see more like this one on different topics.

    ReplyDelete

Post a Comment

Popular posts from this blog

The Power of Education