Pages

Sunday 3 April 2016

Object-Oriented Programming Concepts

1.Class
            It is a data structure that contains data members , member functions( methods), properties, constructor, destructor, indexers and nested type.Class is 
It is a user defined data type.
It is a reference type.
Infact class is a tag or template for object.

Ex:
Class Instantiation:
2.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 a software bundle of related variable and methods. 
An object is an instance of a class
A class will not occupy any memory space. Hence to work with the data represented by the class you must create a variable for the class, that is called an object. 

When an object is created using the new operator, memory is allocated for the class in the heap, the object is called an instance and its starting address will be stored in the object in stack memory.

Ex:

3.Encapsulation

4.Abstraction
                 Abstraction is one of the principle of object oriented programming. It is used to display only necessary and essential features of an object to ouside the world.Means displaying what is necessary and encapsulate the unnecessary things to outside the world.Hiding can be achieved by using "private" access modifiers.

5.Constructor
             Constructor is a special method of a class which will invoke automatically whenever instance or object of class is created. Constructors are responsible for object initialization and memory allocation of its class. If we create any class without constructor, the compiler will automatically create one default constructor for that class. There is always at least one constructor in every class.

Constructor Video Link


Types of Constructor:
1>Default Constructor
2>Parameterized Constructor
3>Copy Constructor
4>Private Constructor
5>Static Constructor

Default Constructor:

                                   A Constructor which does not have parameter is called default constructor.

If a class does not have a constructor, a default constructor is automatically generated and default values are used to initialize the object fields.

Example:Default Constructor Example
OUTPUT:


Parameterized Constructor :

                                     A constructor which has at least one parameter is called a parameterized constructor. By parametrized constructor you can initialize each instance of the class to different values.

Example:

OUTPUT:


Copy Constructor :
                               The constructor which creates an object by copying variables from another object is called a copy constructor. Copy constructor is for initializing  new instance to the values of an existing instance.

Example:

OUTPUT:




Static Constructor:

                            When we declared constructor as static it will be invoked only once for any number of instances of the class and it’s during the creation of first instance of the class or the first reference to a static member in the class. 

Features of static constructor:

  • Only one static constructor is allowed.
  • A static constructor does not take access modifiers.
  • A static constructor does not have parameters.
  • It can only access the static members of the class.
  • A static constructor is called first object of the class is created
  • A static constructor cannot be called directly.
  • A static constructor is to initialize any static data.
  • A static constructor is  to perform a particular action that needs performed once only.
  • The user has no control on when the static constructor is executed in the program.
  • A typical use of static constructors is when the class is using a log file and the constructor is used to write entries to this file.



Example:

OUTPUT:


Private Constructor:

                           Private constructor is a special instance constructor used in a class that contains static member only.Private constructors are used to prevent creating instances of a class when there are no instance fields or methods, If a class has one or more private constructor and no public constructor then other classes is not allowed to create instance of this class this mean we can neither create the object of the class nor it can be inherit by other class. 
The main purpose of creating private constructor is used to restrict the class from being instantiated when it contains every member as static.

Example:


OUTPUT:




10.Why is the StringBuilder class better for more string manipulations instead of the String class?

String:
Strings are play very important role in our programming world.A string is a sequential collection of Unicode characters that represent text. A string instance is immutable. You cannot change it after it was created. Any operation that appears to change the string instead returns a new instance.Any operation like insert, replace or append happened to change string simply it will discard the old value.we can say not updatable.
  • under System namespace
  • Immutable(readonly) instance
  • performance degrades when continuous change of value occures
  • thread safe
 Example
 
 string mynewstring = "test Blog";
 // It will create new string instance instead of changing  the old one.
  mynewstring += "Hello , ";
  
mynewstring += "U there ??";

StringBuilder:
StringBuilder is a class in which   System.Text is the namespace. This class cannot be inherited.will use same instance of object to perform any action.
  • under System.Text namespace
  • mutable instance
  • shows better performance since new changes are made to existing instance.

 Example

StringBuilder objsb = new StringBuilder("");
objsb.Append("HI");
objsb.Append("How are you? ");
string mystr = objsb.ToString();

Differences between String and StringBuilder:
Differences between String and StringBuilder:


namespace String 
namespace String.Text 
It is  immutable(not updatable)
Is is mutable(updatable)
Example of Assigning:
String string= "Everything isimportant";
Example of Assigning:
StringBuilder sbstring= new StringBuilder("Everything is important");
+ or Concat method
use Append method.
When we do  concatenation additional memory will be allocated.
Here When we do append memory will be allocated when the string buffer capacity exceeds only.




No comments:

Post a Comment