Object Oriented Programming (OOP) in a Nutshell
The two most important words in Object Oriented Programming are Object and Class. A class is a blueprint of an object. You can think of a class as a concept, and the object is the embodiment of that concept.Though they are often used interchangeably but they are not the same thing. You need to have a class before you can create an object.
Object is not similar to Class (Object != Class)
Class is a programming code that is created/defined using keyword class and has properties and methods. Properties and methods together are known as members of a class.
Fig: Person Class with properties : Name and Age |
Objects are the basic units of object oriented programming. An Object is an instance of a class and it is created/defined using new keyword which is assigned/referenced to a variable. This variable references the resulting object , so, it is known an object variable (i.e person).
var person = new Person(); person.Name = 'Anup';
Object variable is used to get or set any of the properties identified in the class. As shown above, object variable (person) is being used to set a property(Name) is to 'Anup'. It holds the state of the object, i.e, it retains the values of its properties and use it to call the methods identified in the class.
Fig: Class vs. Object (Example using Car) |
Example (Class vs Object)
So, let's say you want to use a person in your program. You want to be able to describe the person and have the person do something. A class called 'person' would provide a blueprint for what a person looks like and what a person can do. To actually use a person in your program, you need to create an object. You use the Person class to create an object of the type 'person.' Logically, you would expect a person to have a name. This would be considered a property of the person. You could also expect a person to be able to do something, such as walking or driving. This would be considered a method of the person. Now you can describe this person and have it do something.
Once you have your objects, they can interact with each other to make something happen. Let's say you want to have a program where a person gets into a car and drives it from A to B. You would start by describing the objects, such as a person and car. That includes methods: a person knows how to drive a car, and a car knows what it is like to be driven. Once you have your objects, you bring them together so the person can get into the car and drive.
A single class is used repeatedly to create multiple object instances. Consider the example of where you don't want to use just one person but 100 people. Rather than describing each one in detail from scratch, you can use the same person class to create 100 objects of the type 'person.' You still have to give each one a name and other properties, but the basic structure of what a person looks like is the same.
A function is a combination of instructions that are combined to achieve some result. A function typically requires some input (called arguments) and returns some results. For example, consider the example of driving a car. To determine the mileage, you need to perform a calculation using the distance driven and the amount of fuel used. You could write a function to do this calculation. The arguments going into the function would be distance and fuel consumption, and the result would be mileage. Anytime you want to determine the mileage, you simply call the function to perform the calculation.
A function is independent and not associated with a class. You can use this function anywhere in your code, and you don't need to have an object to use it.
Now, what if you were to associate the function with an object of the type 'car?' For example, you want to be able display the mileage of the car on the dashboard. In this case, the mileage calculation has become a method because it is a procedure associated with the car's class. Every time you create a new object of the type 'car' using the car class, this method will be part of the object. The action the car is now able to perform is to calculate mileage. It is the same calculation as performed by the stand-alone function but is now bound to the car.
Now we know the difference between a class and an object, lets define Object oriented programming, also known as OOP.
OOP is an approach to designing and building applications that are flexible, natural, well-crafted, and testable by focusing on objects that interact cleanly with one another. To build an application using object oriented programming principles, use the following techniques:
- Identifying classes
- Separating responsibilities
- Establishing relationships
- Leveraging reuses
- Separating responsibilities
- Establishing relationships
- Leveraging reuses
Identifying classes
When given a requirement for a new application or a feature, start by identifying the classes from the requirements or specification. OOP represents the entities and concepts of an application as a set of classes. Each class has properties that define the data each object will manage. Each class has methods, which are the actions and behaviours that each object can perform. In short,
When given a requirement for a new application or a feature, start by identifying the classes from the requirements or specification. OOP represents the entities and concepts of an application as a set of classes. Each class has properties that define the data each object will manage. Each class has methods, which are the actions and behaviours that each object can perform. In short,
- - Represents Business Entities and set of classes
- - Defines Properties (Data)
- - Defines methods (actions/behaviour)
Separating responsibilities
Analyse the classes you identified and separate responsibilities as needed. Minimise coupling by ensuring each class has a single purpose. Maximise cohesion by reviewing the properties and methods of each class to confirm each one belongs to that class. Single-focus classes simplify maintenance and improve test-ability. In short,
Analyse the classes you identified and separate responsibilities as needed. Minimise coupling by ensuring each class has a single purpose. Maximise cohesion by reviewing the properties and methods of each class to confirm each one belongs to that class. Single-focus classes simplify maintenance and improve test-ability. In short,
- - Minimises coupling
- - Maximises cohesion
- - Simplifies maintenance
- - Improves test-ability
Establishing relationships
The relationships between the classes define how the objects created from those classes work together to perform the operations of the application. In short,- - Define how objects work together to perform the operations of the application
Leveraging reuses
The power of OOP lies in its promise of reuse. By extracting commonality among a set of classes into a separate class, you have more reusable code. There are several ways to achieve reuse, including extracting common functionality into a base class using inheritance. Extensive reuse of existing classes not only shortens development time, it also leads to more robust applications. Reuse through a component library of general purpose classes and reuse through interfaces. In short
- - Involves extracting commonality
- - Building reusable classes and components
- - Defining Interfaces
These four pillars are the foundation of OOP. In short,
- Abstration : It describes an entity in simple terms, ignoring the irrelevant details. It reduces complexity by focusing only on what is important for the purpose of this specific application.
- Encapsulation : It allows for hiding the data and the implementation within the class. The data is stored in backing fields and accessible to the rest of the application through property getters and setters. The code is only accessed through the class interface.
- Inheritance: It allows derived, or child, classes to reuse all the code in the base or parent class.
- Polymorphism : It means many forms. A base class can define a method and any derived class can override that method to provide its own definition and implementation, basically, providing its own shape for the method.
Comments
Post a Comment