DotNetFunda.com on mobile Winners Win Prizes Winners & Prizes Announcements Like us on Facebook Top Articles Authors Mon, 25-Jun-2012 Authors All Time Authors SheoNarayan 31050 Karthikanbarasan 12250 Vishvvas 10000 Latest members | More ... (Statistics delayed by 5 minutes) Ads Articles Categories .NET Framework Articles ADO.NET Articles ASP.NET Articles ASP.NET AJAX Articles ASP.NET MVC Articles Azure Articles Best Practices Articles BizTalk Server Articles C# Articles CMS Articles CSS Articles Error and Resolution Articles F# Articles HTML 5 Articles IIS Articles JavaScript Articles jQuery Articles LightSwitch Articles LINQ Articles Management Articles OOPS Articles Others Articles Pattern and Practices Articles PowerShell Articles SEO Articles SharePoint Articles Silverlight Articles Sql Server Articles VB.NET Articles Visual Studio 2010 Articles WCF Articles Web Analytics Articles Windows Forms Articles Windows Phone Articles WPF Articles WWF Articles XML Articles Advertisements Basic Concepts of OOP: Encapsulation and Inheritence

In this article i would like to describe the people what are the basic concepts of Object Oriented Programming
The OOP stands on the following three pillars.

  1. Object &Class (Encapsulation)
  2. Inheritance
  3. Polymorphism

1 Object & Class (Encapsulation): 

       

         Object:


The Object is an Instance in programming language which is made by inspiring from the real world which contains some properties and methods.

Lets take an example of a person(object) from the real world.

A person is an object, as i told you before that an object could have methodes(behaviour)& properties so here person(object) could have some properties like his black hair,fair color,tall,and so on..Simillarly it could have the methodes like Eat,Run,Play,Sleep and so on..All these properties & methodes are kept stored in a seperate class.
          

         Class:


A class is basically a container where object lies..In programming we define all the properties&methodes in a seperate class while we make object of that class in main class... It means we generate the objects from main class, lets have a look..

class cat
{
   public void eat()
         {
           console.writeline("cat eats mouse");
          }
    public void walk()
          {
            console.writeline("It walks slowly...");
          }
}
Here we have defined a class and in 'class cat' " public void eat() " and " public void walk() " are methods.now we make an object in main class and call these methods...
static void main ()
{
   cat mycat = new cat();
    mycat.eat();
    mycat.walk();
    console.readline();
}
This is the main  class in which we make an object named mycat of the classs cat.. after making the cat's class object we can call its methods&properties by writting "mycat." when we put dot after the object it calls all the methodes & properties from the related class...

Encapsulation in OOP?


Encapsulation is the word which came from the word "CAPSULE" which to put some thing in a kind of shell. In OOP we are capsuling our code not in a shell but in "Objects" & "Classes".  So it means to hide the information into objects and classes is called Encapsulation.


2 Inheritance:


The Inheritance is one of the main concepts of oop.Inheritance is just to adobe the functionality (methods & properties) of one class to another. This simple term is known as Inheritance.  

Lets have a look of code
public class cat
{
   public void eat()
   {
      console.writeline("cat can eat");
    }
   public void walk()
   {
    console.writeline("cat can walk");
   }
}
now we make another class of another cat and we inherit the methods of the first class to the new class.Do remember in programming we inherit one class to another by coding " : " colon between the parent and child class.
Lets have a look.. 
public class EgyptianCat : cat
{
  public void jump()
  {
     console.writeline("Egyptian cat can jump"); 
  }
 public void bite()   {     console.writeline("Egyptian cat can not bite");     } }
static void main()
{
cat mycat=new cat();
mycat.eat();
mycat.walk();
EgyptianCat yourcat=new EgyptianCat();
yourcat.eat();
yourcat.walk();
yourcat.jump();
yourcat.bite();
console.readline();
}

this is the code in main class in which we are inheriting the class EgyptianCat to the class cat, so that the new class becomes the child class of the first class which is now called the parent class.

No comments:

Post a Comment