Simple OOP

OOP1What is OOP?

It refers to “Object-Oriented Programming”, OOP is a Design Philosophy/Programming Paradigm/Programming Methodology based on the concept of “objects“.

Before OOP

There was a procedure of programming was known as “Traditional/Procedural Programming“, e.g(C, Pascal, etc).

Traditional/Procedural Programming Disadvantages:

  • Difficult Maintenance
  • Slow Development

That’s why OOP came to us.

OOP Advantages

  • Re-usability
  • Fast Development
  • Organized Code allows Fast and Easy Maintenance

OOP Concepts

  • Objects and Classes
  • Encapsulation
  • Abstraction
  • Inheritance
  • Polymorphism

What is Object and Class?

Object:

An object can be considered a “thing” that can perform a set of related activities. And these activities define the Object’s Behavior.
e.g A hand is object, and it performs some activities like holding.

Class:

A class is a collection of objects of similar type.
Class is composed of three things: (Name, Attributes and Operations).

public class Customer
{
}
/*****************/
Customer object1 = new Customer();

This code demonstrates how to create a class which is Customer, and how to use this class by taking an Object which is object1.

 

Encapsulation

A set of elements have the same Properties and Behaviors collecting them together in one place which is Model.
Properties => Variables
Behaviors => Methods
Model => Class

That idea of encapsulation is to hide how a class does its business, while allowing other classes to make requests of it.

public class Person
{
    // Properties
    public string name;
    public string tall;
    // Method 
    public void Speak()
    {
        //Speaks
    }
}

Abstraction

Abstraction refers to the act of representing essential features without including the background details or explanations.
Classes use the concept of abstraction and are defined as a list of abstract attributes.
Storing data and functions in a single unit (class) is encapsulation.
Data cannot be accessible to the outside world and only those functions which are stored in the class can access it.

Inheritance

The ability of a new class to be created, from an existing class by extending it.
Inheritance provides reusability, like, adding additional features to an existing class without modifying it. This is achieved by deriving a new class from the existing one. The new class will have combined features of both the classes.

public class Vehicle
{
}
public class Car : vehicle
{
}

Car Class inherits from Vehicle Class, Which means that Car class will have all of the public properties and methods of Vehicle class, and also Car class can modify a specific implementation of a certain method in Vehicle class.

Polymorphism

Polymorphism is a generic term that means ‘many shapes’. More precisely Polymorphisms means the ability to request that the same operations be performed by a wide range of different types of things.

Polymorphisms is achieved by using two different techniques named method overloading and method overriding,

Method Overloading

The ability to define several methods all with the same name, but with different signatures.

public class Person
{
public void SetName(string lastName)
{
  //Implementation goes here
}
public string SetName(string firstName, string lastName)
{
  //Implementation goes here
}
}

The two methods here have the same name, but different signatures(No. of Parameters and Return type).

Method Overriding

Method overriding is a language feature that allows a subclass to override a specific implementation of a method that is already provided by one of its super-classes.


public class Person
{
    public virtual void SetName()
    {
        //Print "Person Name"
    }
}
public class Male : Person
{
    public override void SetName()
    {
        // Print "Male Name"
    }
}

To perform method overriding:

  • You have to use Inheritance
  • Method in Super-Class should have the keyword “virtual” to allow overriding

Finally, here’s a code sample demonstrating simply OOP LINK 😀

Advertisements

OOP Concepts

And Now,  We’re going to talk about the Concepts of Object-Oriented Programming, let’s at first know:

What Is an Object ?
An object is simply can be defined as a “thing” that can perform a set of related activities and these activities defines the Object’s Behavior.
For Example: A hand(Object) can grip something.

…and we should know also:

What Is a Class ?
class is simply a representation of a type of an object, It is the blueprint/ plan/ template that describe the details of an object, A class is the blueprint from which the individual objects are created, Class is composed of three things:
a name, attributes, and operations.

…and know let’s know what are the Concepts of OOP ?
They’re mainly four concepts:
Abstraction, Encapsulation, Inheritance, and Polymorphism.

and now let’s define each concept….

What is Abstraction ?

Abstraction is an emphasis on the idea, qualities and properties rather than the particulars (a suppression of detail). The importance of abstraction is derived from its ability to hide irrelevant details and from the use of names to reference objects. Abstraction is essential in the construction of programs. It places the emphasis on what an object is or does rather than how it is represented or how it works. Thus, it is the primary means of managing complexity in large programs.

What is Encapsulation ?

The encapsulation is the inclusion within a program object of all the resources need for the object to function – basically, the methods and the data. In OOP the encapsulation is mainly achieved by creating classes, the classes expose public methods and properties. The class is kind of a container or capsule or a cell, which encapsulate the set of methods, attribute and properties to provide its indented functionalities to other classes. In that sense, encapsulation also allows a class to change its internal implementation without hurting the overall functioning of the system. That idea of encapsulation is to hide how a class does it but to allow requesting what to do.

What is Inheritance ?

Ability of a new class to be created, from an existing class by extending it, is called inheritance.

What is Polymorphism ?

Polymorphism is a generic term that means ‘many shapes’. More precisely Polymorphism means the ability to request that the same operations be performed by a wide range of different types of things. At times, In OOP the polymorphisms is achieved by using many different techniques named method overloading, operator overloading and method overriding.

…and know we’ve already know the concepts of OOP.
and here’s a PPT brief about OOP Concepts. 🙂

Stay Tuned,  to know more 🙂

 

What is Object-Oriented Programming ?

13261349-chalk-writing-of-oop-for-object-oriented-programming-on-a-blackboard Object Oriented Programming (OOP) is a computer-programming methodology that emphases on data items rather than processes. Traditionally, software development models are based on a top-down approach, which starts with a functional description of a system and then refined until implementation is achieved. As Computers increase in processing power, the software they execute becomes more complex. This increase of complexity comes at cost of large programs with huge codebases that quickly become difficult to understand, maintain and keep bug-free. OOP tries to ease this problem by creating linkages between objects. It focuses first on data items (objects) that are being manipulated. The emphasis is on characterizing data items as active entities, which can perform operations on and for themselves. It then describes how system behavior is implemented through the interaction of the data items.

Stay tuned to learn more about  OOP. 🙂