Object Oriented
Programming (OOP) is a programming model where programs are organized around
objects and data rather than action and logic.
OOP has the following
important features.
Class
A class is the core of
any modern Object Oriented Programming language such as C#.
In OOP languages it is
mandatory to create a class for representing data.
A class is a blueprint
of an object that contains variables for storing data and functions to perform
operations on the data.
A class will not
occupy any memory space and hence it is only a logical representation of data.
To create a class, you
simply use the keyword "class" followed by the class name:
class Employee
{
}
Object
Objects are the basic
run-time entities of an object oriented system. They may represent a person, a
place or any item that the program must handle.
"An object is an
instance of a class"
Example
class Employee
{
}
Syntax to create an
object of class Employee:
Abstraction
Abstraction is
"To represent the essential feature without representing the background
details."
Abstraction lets you
focus on what the object does instead of how it does it.
Abstraction provides
you a generalized view of your classes or objects by providing relevant
information.
Abstraction is the
process of hiding the working style of an object, and showing the information
of an object in an understandable manner.
Real-world Example of Abstraction
Suppose you have an
object Mobile Phone.
Suppose you have 3
mobile phones as in the following:
Nokia 1400 (Features:
Calling, SMS)
Nokia 2700 (Features:
Calling, SMS, FM Radio, MP3, Camera)
Black Berry
(Features:Calling, SMS, FM Radio, MP3, Camera, Video Recording, Reading
E-mails)
Abstract information
(necessary and common information) for the object "Mobile Phone" is
that it makes a call to any number and can send SMS.
So that, for a mobile
phone object you will have the abstract class as in the following:
Encapsulation
Wrapping up a data
member and a method together into a single unit (in other words class) is
called Encapsulation.
Encapsulation is like
enclosing in a capsule. That is enclosing the related operations and data
related to an object into that object.
Encapsulation is like
your bag in which you can keep your pen, book etcetera. It means this is the
property of encapsulating members and functions.
class Bag
{
book;
pen;
ReadBook();
}
Example
Inheritance
When a class includes
a property of another class it is known as inheritance.
Inheritance is a
process of object reusability.
For example
Polymorphism
Polymorphism means one
name, many forms.
One function behaves
in different forms.
In other words,
"Many forms of a single object is called Polymorphism."
Real-world Example of
Polymorphism
Example
Constructor
A special method of the class that will be
automatically invoked when an instance of the class is created is called a
constructor.
The main use of constructors is to initialize private fields of the class while creating an instance for the class.
When you have not created a constructor in the class, the compiler will automatically create a default constructor in the class. The default constructor initializes all numeric fields in the class to zero and all string and object fields to null.
The main use of constructors is to initialize private fields of the class while creating an instance for the class.
When you have not created a constructor in the class, the compiler will automatically create a default constructor in the class. The default constructor initializes all numeric fields in the class to zero and all string and object fields to null.
Destructors
As we all know, ‘Destructors’ are used to destruct instances of classes. When we are using destructors in C#, we have to keep in mind the following things:
- A class can only have one destructor.
- Destructors cannot be inherited or overloaded.
- Destructors cannot be called. They are invoked automatically.
- A destructor does not take modifiers or have parameters.
The following is a declaration of a destructor for the
class MyClass:
~ MyClass()
{
// Cleaning up code goes here
}
{
// Cleaning up code goes here
}
String Functions | Definitions |
Clone() | Make clone of string. |
CompareTo() | Compare two strings and returns integer value as output. It returns 0 for true and 1 for false. |
Contains() | The C# Contains method checks whether specified character or string is exists or not in the string value. |
EndsWith() | This EndsWith Method checks whether specified character is the last character of string or not. |
Equals() | The Equals Method in C# compares two string and returns Boolean value as output. |
GetHashCode() | This method returns HashValue of specified string. |
GetType() | It returns the System.Type of current instance. |
GetTypeCode() | It returns the Stystem.TypeCode for class System.String. |
IndexOf() | Returns the index position of first occurrence of specified character. |
ToLower() | Converts String into lower case based on rules of the current culture. |
ToUpper() | Converts String into Upper case based on rules of the current culture. |
Insert() | Insert the string or character in the string at the specified position. |
IsNormalized() | This method checks whether this string is in Unicode normalization form C. |
LastIndexOf() | Returns the index position of last occurrence of specified character. |
Length | It is a string property that returns length of string. |
Remove() | This method deletes all the characters from beginning to specified index position. |
Replace() | This method replaces the character. |
Split() | This method splits the string based on specified value. |
StartsWith() | It checks whether the first character of string is same as specified character. |
Substring() | This method returns substring. |
ToCharArray() | Converts string into char array. |
Trim() | It removes extra whitespaces from beginning and ending of string. |
C# OOPS Concept
Reviewed by Sandeep Kumar Jha
on
05:40:00
Rating:
No comments: