Subscribe Now!

Enter your email address:

Thursday, September 6, 2012

BASIC CONCEPTS OF OOPS


BASIC CONCEPTS OF OBJECT ORIENTED PROGRAMMING:

1.Class
2.Object
3.Inheritance
4.Interface
5.Abstract Class
6.Overriding
7.Polymorphism (or ) Overloading

1.Class:

A class is simply a representation of a type of object. It is the blueprint/ plan/ template that describe the details of an object. A class is the blueprint from which the individual objects are created. Class is composed of three things: a name, attributes, and operations.

Examples 1

public class Student
{
}

2.Object:

An object can be considered a thing that can perform a set of related activities. The set of activities that the object performs defines the object’s behavior.
In pure OOP terms an object is an instance of a class.
Examples 2
Student objectStudent = new Student();
In Example 2 we can say that the student object, named objectStudent, has created out of the Student class.

3.Inheritance:

Ability of a new class to be created, from an existing class by extending it, is called inheritance.
Examples 3
public class Exception
{
}
public class IOException : Exception
{
}
In Example 3 the new class (IOException), which is called the derived class or subclass, inherits the members of an existing class (Exception), which is called the base class or super-class. The class IOException can extend the functionality of the class Exception by adding new types and methods.

4.Interface:

An interface is type definition similar to a class except that it purely represents a contract between an object and a user of the object. An interface cannot be directly instantiated as an object. No data members can be defined in an interface. Methods and properties can only be declared, not defined. To prefix the name interfaces with’I’ like IDisposable, ISerializable, ICloneable, IEnumerator, etc.
Examples 4
interface IShape
{
void Draw();
double X { get; set; }
double Y { get; set; }
}
Implementing an interface is simply done by inheriting off the interface and then defining all the methods and properties declared by the interface.
Examples 5
class Square : IShape
{
private double mX, mY;
public void Draw() { … }
public double X
{
set { mX = value; }
get { return mX; }
}
public double Y
{
set { mY = value; }
get { return mY; }
}
}

5.Abstract Class:

Abstract class can be simply defined as incomplete classes. Abstract classes contain one or more incomplete methods called abstract methods. The abstract class only provides the signature or declaration of the abstract methods and leaves the implementation of these methods to derived or sub-classes. Abstract method and abstract classes are marked with the abstract keyword. An abstract class itself must be marked with the abstract keyword. Since abstract classes are incomplete, they cannot be instantiated. The class inheriting an abstract class and implementing all its abstract methods is called the concrete of the abstract class.
Examples 6
abstract class TaxCalculator
{
protected double itemPrice;
protected double tax;
public abstract double CalculatorTax();
public double Tax
{
get { return tax };
}
public double ItemPrice
{
get { return itemPrice };
}
}

6.Overriding:

Override modifier is to modify a method, a property, an indexer, or an event. An override method provides a new implementation of a member inherited from a base class. The method overridden by an override declaration is known as the overridden base method. The overridden base method must have the same signature as the override method. You cannot override a non-virtual or static method. The overridden base method must be virtual, abstract, or override. An override declaration cannot change the accessibility of the virtual method. Both the override method and the virtual method must have the same access level modifier.
Examples 7
public class Employee
{
public virtual void GetPayCheck()
{
}
public void Work()
{
}
}
public class Executive : Employee
{
GetPayCheck method;
public override void GetPayCheck()
{
}
public void AdministerEmployee()
{
}
}

7.Polymorphism (OR) Overloading:

Polymorphism is the ability for classes to provide different implementations of methods that are called by the same name. In example 8 how polymorphism would work, here’s a different class that implements the same interface but has different code. Polymorphism allows us to define methods in a base class and override them with derived class implementations.
Examples 8
public class MyLogger
{
public void LogError(Exception e)
{
}
public bool LogError(Exception e, string message)
{
}
}

1 comments:

Nice Articles i found in this site

Post a Comment

Related Posts Plugin for WordPress, Blogger...