BASIC OF OBJECT ORIENTED PROGRAMMING (PART-I)

I am writing this article after a very long time, because I was busy in some other high priority stuff. I am writing this article for some of the students. In this article I will explain basic of object oriented programming and I will go through the important pillars of the Object Oriented Programming.
WHAT IS OBJECT ORINETED PROGRAMMING:-
Object Oriented Programming is a technique to think real world problem in terms of an object. These objects map the software model to real world concepts. These objects have the responsibility and provide services to other objects or application.
After all the purpose of computer program is to solve a real world problem. For that purpose, OOP says to create a mode of real world in the computer’s digital world. 
WHAT IS CLASS:-
Class is a set of objects sharing common structure and common behavior. It is the skeleton or a representation of real world thing. The world thing can be anything such as “Country, Human Being, and Engines etc.”. It is also called a template of an object, because it describes all the attributes of an object and also the operation and methods which can be performed on that object. It is a comprehensive/custom data type.
What IS AN OBJECT:-
Object is an instance of a class. As described earlier class is a skeleton of an object while object is the actual occurrence of a class. E.g.
Person is a Class
Imran Khan is an instance of class Person
RELATION BETWEEN CLASS AND OBJECT:-
  • Class is a definition while object is its instance
  • Class is a skeleton while object is existence in the real world.
FEATURES OF OBJECT ORIENTED PROGRAMMING:-
Features of object oriented programming are given below:
  • Abstraction
  • Encapsulation
  • Inheritance
  • Polymorphism
ABSTRACTION:-
Abstraction is the process of focusing on essential characteristics of an object (In terms of domain, application or in terms of user perspective), rather than looking at the whole object.
 
Example:-
Consider a class, “BOOK
From the perspective of a Shopkeeper the essential characteristic of a book are given below:
  • Item Number
  • Item Name
  • Item Price
  • Quantity in Stock
The same class BOOK, from the perspective of Library System will be something like:
  • Author Name
  • Book Number
  • Access Number
So we can say that Abstraction is to overlook the un-necessary attributes of an object by keeping in mind the domain or application which we are going to design.
ENCAPSULATION:-
Encapsulation is the process of hiding all the internal details from the outside world. In Object oriented programming encapsulation is implemented by using the concepts of class.
Encapsulation hides the details of how we implement the state behind the behavior which we present to the outside world.
Examples:-
  • Turn On TV
  • Start the CAR
  • Math.Abs(value)
  • etc
In the above points we do not know that what is happening internally. For example if we call a Math.Abs(value) method in .Net, it will round the given value, but we do not know that what happened internally to perform this operation.
DIFFERENCE BETWEEN ABTSRACTION AND ENCAPSULATION:-
Abstraction tells us what external face we should present to the world where as Encapsulation ensures that the implementation of the interface doesn’t leak out to the outside world.
INHERITANCE:-
Inheritance is the ability to create new abstraction from existing one. Inheritance can also be defined as creating new class from existing one. The new classes which are derived from the existing class are called derived classes and from which they are derived are called Super Class or base class.
Inheritance implements the concept of Code re-usability. In Inheritance classes are arranged like a TREE LIKE structure called hierarchy.
clip_image001
 
WHAT IS ENUMERATION:-
An enumeration type (also named an enumeration or an enum) provides an efficient way to define a set of named integral constants that may be assigned to a variable. For example, assume that you have to define a variable whose value will represent a day of the week. There are only seven meaningful values which that variable will ever store. To define those values, you can use an enumeration type, which is declared by using the enum keyword.
enum Days { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday };
 
OPERATOR OVERLOADING:-
Operator overloading permits user-defined operator implementations to be specified for operations where one or both of the operands are of a user-defined class or struct type. Different operators such as +,- etc do not work for the complex data type. For this purpose we can overload our desired operators in our classes.
SIMILARITY BETWEEN CLASS AND STRUCTURE:-
  • Both can have constructors, methods, properties, fields, constants, enumerations, events, and event handlers.
  • Structures and classes can implement interface.
  • Both of them can have constructors with and without parameter.
  • Both can have delegates and events.
 
DIFFERENCE BETWEEN CLASS AND STRUCTURE:-
  • Structures are value types and classes are reference types. So structures use stack and classes use heap.
  • Structures members cannot be declared as protected, but class members can be. You cannot do inheritance in structures.
  • Structures do not require constructors while classes require.
  • Objects created from classes are terminated using Garbage collector. Structures are not destroyed using GC.
ACCESS MODIFIERS:-
Access modifiers are an integral part of object-oriented programming. They support the concept of encapsulation, which promotes the idea of hiding functionality. Access modifiers allow you to define who does or doesn't have access to certain features.
Modifier
Description
public
There are no restrictions on accessing public members.
private
Access is limited to within the class definition. This is the default access modifier type if none is formally specified
protected
Access is limited to within the class definition and any class that inherits from the class
internal
Access is limited exclusively to classes defined within the current project assembly
protected internal
Access is limited to the current assembly and types derived from the containing class. All members in current project and all members in derived class can access the variables.
Public
The public keyword is an access modifier for types and type members. Public access is the most permissive access level.
There are no restrictions on accessing public members.
Accessibility:
  • Can be accessed by objects of the class
  • Can be accessed by derived classes
 
Private
Private access is the least permissive access level.
Private members are accessible only within the body of the class or the struct in which they are declared.
Accessibility:
  • Cannot be accessed by object
  • Cannot be accessed by derived classes
Protected
A protected member is accessible from within the class in which it is declared, and from within any class derived from the class that declared this member.
A protected member of a base class is accessible in a derived class only if the access takes place through the derived class type.
Accessibility:
  • Cannot be accessed by object
  • By derived classes
Internal
The internal keyword is an access modifier for types and type members. We can declare a class as internal or its member as internal. Internal members are accessible only within files in the same assembly (.dll).
In other words, access is limited exclusively to classes defined within the current project assembly.
Accessibility:
In same assembly (public)
  • Can be accessed by objects of the class
  • Can be accessed by derived classes
In other assembly (internal)
  • Cannot be accessed by object
  • Cannot be accessed by derived classes
 
 
protected internal
The protected internal accessibility means protected OR internal, not protected AND internal.
In other words, a protected internal member is accessible from any class in the same assembly, including derived classes.
The protected internal access modifier seems to be a confusing but is a union of protected and internal in terms of providing access but not restricting. It allows:
Inherited types, even though they belong to a different assembly, have access to the protected internal members.
Types that reside in the same assembly, even if they are not derived from the type, also have access to the protected internal members.
 

DELEGATE:-

Delegate is a data structure which points to a function or a method. Delegate class has a signature and it can only refer to those method or function whose signature is same to the signature of the delegate class.

 
Please download the ClassesExplained.rar file to understand class, object, fields, properties, constructor, constructor overloading and methods

 ClassesExplained.rar (26.63 kb)

Please download StructureExplained.rar to download the Structure, Constrcutor in Structure, Properties, Constructor overloading and methods.

StructureExplained.rar (23.51 kb)

Please download the following file to understand the basic of delegates

DelegateExplained.rar (22.55 kb)

To Understand the Operator overloading please download the following file.

OpOverloading.rar (21.60 kb)

Add comment

biuquote
  • Comment
  • Preview
Loading