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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s