Object Oriented Programming Concepts (OOP) Introduction – Part 1

  • Facebook
  • Twitter
  • Delicious
  • StumbleUpon
  • Pinterest
  • Google Plus

Object Oriented Programming Language (OOPS). Problems in software development can be solved by Object Oriented Programming Concepts.

It is a programming pattern using “Objects”. Main concept of OOPS is its Re-Usability.

To understand this concept we can take a real world example of Students in a School.

  1. Lets take “Students” as a Class.
  2. Class can have any number of Objects. Here “Biology Students” & “Computer Students”.
  3. “Students” are controlled by “Teachers” which acts as an Interface.
  4. “Students” Class can be Re-Used to create “Biology Students” and “Computer Students”.

Object:

Object is an instance of a Class. Objects is a real time entity.

An object is an entity that has attributes, behavior and identity. An objects behavior is to perform different set of activities.

In our above example “Students” we can calculate each student percentage of marks.

Class:

Class is a collection of Objects. A class describes all the attributes of objects, as well as the methods that implement the behavior of member objects. It is a comprehensive data type, which represents a blue print of objects.

In our above example “Students” is a Class which has objects “Biology Students” & “Computer Students”.

public class Students
{
}
Students BiologyStd = new Students();
Students ComputerStd = new Students();

In above code the students object, named BiologyStd, has created out of Students class.

And students object, named ComputerStd, has created out of Students class. And here comes the Re-Usability.

Class is composed of 3 things: Name, Attributes and Operations.

A class and object may look same, but they are not. Class is a definition, while object is an instance of a class. We can create many objects from a single class. In our above example class “Students” has attributes and methods like Totalmarks and PercentageofMarks. Class “Students” is a prototype.

Encapsulation:

In OOP the encapsulation is done by creating classes, the classes expose methods and properties.

Encapsulation is a process of hiding all the internal details of an object from the outside world.

The Class looks like a container which has methods, properties, attributes, data structures to provide the required functionality.


In our “Students” Example lets Implement Encapsulation.

public class Students
{

public double totalMarks;
public double MarksScored;
public double GetPercentageMarks()
{
double Percentage = (MarksScored / totalMarks) * 100;
return Percentage;
}
}

In above code we have encapsulated totalMarks, MarksScored and method GetPercentageMarks.

While creating a “Biology Student” object, the implementation of GetPercentageMarks will not be shown.

Abstraction:

Abstraction means working on the overview and not worrying about the internal details. Making the complex system represented in simplified manner.

It is a prominence on the idea. Abstractions is achieved by Access Modifiers. It gives access only to required details.

A good real life example of Abstraction is Cameras. It has things like Lens, Mirror, Flash, LCD Display. For a normal user it is not necessary to know how it each and everything works separately. They just need to know how to operate the camera. Here the Camera is an object that is designed to hide the internal details.

Lets see our Students example and check on how to achieve Abstraction.

Public — The type or member can be accessed by any other code in the same assembly or another assembly that references it.

Private — The type or member can be accessed only by code in the same class or struct.

Protected — Just like private but Accessible in derived classes also through member functions.

Internal — The type or member can be accessed by any code in the same assembly, but not from another assembly.

Protected Internal — The type or member can be accessed by any code in the assembly in which it is declared, or from within a derived class in another assembly.

Let’s Implement with our Students Example:

public class Students

{

public int iPublic;                     // Public - Accessible everywhere.

private int iPrivate;                   // Private - Accessible only inside this class.

protected int iProtected;               // Protected - Like Private also accessible in derived class.

internal int iInternal;                 // Internal - Accessible everywhere in same assembly.

protected internal int iProtecteInt;    // Inside assembly as well as to derived classes outside assembly

static int iStatic;                     // Defualt private if no Access Modifier specified

public static int iPublicStatic;        // Static - Shared across objects

int iTotalMarks;                        // Defualt private if no Access Modifier specified

public void Marks()

{

/* Inside a class if we create an object of same class then you can access all members

* through object reference even private data can be accessed

* */

Students obj = new Students();

obj.iPublic = 10;

obj.iPrivate = 10;                  // Private are accessible only inside this Class

obj.iProtected = 10;

obj.iInternal = 10;

obj.iProtecteInt = 10;

//obj.iStatic =10;                  // Access denied as it is Static. Accessible by class names only

Students.iStatic = 10;

//obj.iPublicStatic = 10;           // Access denied as it is Static. Accessible by class names only

Students.iPublicStatic = 10;

obj.iTotalMarks = 10;

}

}

Let’s implement the student object in different class and Check :

public class NewStudent

{

public void NewStudent()

{

Students obj = new Students();

obj.iPublic = 10;

//obj.iPrivate = 10;                // Access denied. As Private are accessible only inside Student Class

//obj.iProtected = 10;              // Access denied. As Protected are accessible only inside Student Class & its Derived Class

obj.iInternal = 10;

obj.iProtecteInt = 10;

//Students.iStatic = 10;            // Access denied. As Private are accessible only inside Student Class.

Students.iPublicStatic = 10;

//obj.iTotalMarks = 10;             // Access denied. As Private are accessible only inside Student Class

}

}

Like(0)Dislike(0)

Share and Enjoy

prabaharancs@gmail.com'

SurFolks

SurFolks, the one stop destination to cater all your wants and needs.

Leave a Reply