OOPS Concepts with Brief explanation and example.asp.net and java

Object Oriented Programming
Object is an real world entity such as a chair, pen
etc..,
Object Oriented Programming is a methodology to
design a program using class and objects.


OOP has some concepts to develop a program
Object
Class
Inheritance
Polymorphism
Abstraction
Encapsulation


Object:
Object is a thing, object performs set of properties
and activities it defines the object behavior.
For example car is a object it performs some activity
like drive one place to another.
Object is known as instance of object.
BMW Car object=new BMW Car();


Class:
Class is representation of what is an object. (Or)
  class is a blue print or template that has details
  about object.
Class is a set methods and properties.
Public class Car
{
}


Example for Class and object:
Consider car as example
Car is a class it has properties like
Design
Number of wheels
Here BMW is an object. It’s comes under car class because it
has all car properties.
BMW object=new BMW();
Public class car
{
Void design();
Void number of wheels();
}


Inheritance:
When a class includes a property of another class is known as
   inheritance.
Here inheriting means receiving or deriving.
One class is having another class of property. Because it receiving
   (or) deriving properties from another class it is said to be
   inheritance.
Class class1
{
//content
}
Class class2:class1
{
//content
}


Real time example for inheritance:
Consider family as example.
In family the child get properties form father or mother.
Here child inheriting behavior from father.
Class father
{
Behavior;
}
Class child : Father
{
Father object=new Father.behavior();
}


Polymorphism:
Here polymorphism means many forms. Or one function
behaves different forms.
Polymorphism achieve by overloading and overriding.
Also known as compile time polymorphism and run time
polymorphism.
Overloading:
It is an ability to define same name for several methods.



Example for method overloading:
Class sum
{
int add(int a, int b)
    {
Return a+b;
     }
int add(int a, int b, int c)
     {
Return a+b+c;
      }
int add(int a, int b, int c, int d)
       {
Return a+b+c+d;
         }
}
Public static void Main()
{
Sum obj =new sum();
Console.writeline(“some of a+b”+obj.sum(10,20));
Console.writeline(“some of a+b+c”+obj.sum(10,20,30));
Console.writeline(“some of a+b+c+d”+obj.sum(10,20,30,40));
}
Output:
Sum of a+b=30
Sum of a+b+c=60
Sum of a+b+C+d=120


Overriding:
Method override occurs when child class declare a method with same arguments already in
parent class.
class CarClass
{
   public virtual int speedLimit()
   {
      return 100;
   }
}
class Ford : CarClass
{
   public override int speedLimit()
   {
      return 150;
   }
   public static void Main()
   {
   Ford obj = new Ford();
   int num= obj.speedLimit();
   Console.WriteLine("Speed Limit is: "+num);
      Console.ReadKey();
   }
}


Encapsulation:
Warping up data and methods in to a single unit is
 known as encapsulation.
Real time example:
Consider mobile as example.
The circuit board and all the connections combined
 into a single unit.
We don’t know the internal process. We just know
 how to make call, and taking pictures etc..,


public class School
  {
     private string Schooldepartname;
     public string SchoolDepartname
     {
        get
        {
           return Schooldepartname;
        }
        set
        {
           Schooldepartname = value;
        }
     }
  }
  public class Departmentmain
  {
     public static int Main(string[] args)
     {
        School d = new School();
        d.SchoolDepartname = "Communication";
        Console.WriteLine("The Dept. Name is :{0}", d.SchoolDepartname);
        Console.ReadKey();
        return 0;
}
}


Abstraction:
Abstraction shows the necessary data and hides the un
necessary data.
Example:
 A Laptop consists of many things such as processor,
motherboard, RAM, keyboard, LCD screen etc. To use it, you
don't need to know how internally LCD screens, keyboard, web
camera works. You just need to know how to operate the
laptop by switching it on.
public abstract class university
  {
    public abstract void BTech();
    public abstract void MBA();
  }


public class GBTU : university
 {
    public override void BTech()
    {
       Console.WriteLine("GBTU BTech Fee 50000/-");
    }
    public override void MBA()
    {
       Console.WriteLine("GBTU MBA Fee 100000/-");
    }
 }
 public class MTU : university
 {
    public override void BTech()
    {
       Console.WriteLine("MTU BTech Fee 40000/-");
    }
   public override void MBA()
    {
       Console.WriteLine("MTU MBA Fee 800000/-");
    }
 }


class Program
 {
static void Main(string[] args)
      {
        GBTU g = new GBTU();
        g.BTech();
        g.MBA();
        MTU m = new MTU();
        m.BTech();
        m.MBA();
        Console.ReadLine();
      }
   }
}
Output-
GBTU BTech Fee 50000/-
GBTU MBA Fee 100000/-
MTU BTech Fee 40000/-
MTU MBA Fee 800000

No comments