Monday 28 November 2011

About Inheritance


1.      inheritance is  a concept of deriving the features from one class into another class
2.      inheritance leads to code reasability and also saves memory.
3.      inheritance increases the performance of an application
4.      Using inheritance, you can create a general class that defines a common set of attributes and behaviors for a set of related items.
5.      A derived class is a specialized version of the base class and inherits all the fields, properties, operators and indexers defined by base class
6.      Protected members are private to the class but can be inherited and accessed by the derived class
 Types of inheritance
1.      single inheritance
2.      multi level inheritance
3.      hierarchical
4.      multiple
5.      hybrid
if the class is inherited from form class then it is called as windows form.

Overriding:

1.      It is a concept of having two methods with same nameand args in parent class and child class.
2.      in overriding, always the priority will be given to local variables
3.      in order to call parent class methods  “base” keyword is required
4.      base keyword can be used with methods and variables also
5.      overriding is also called as runtime polymorphism or late binding

example:

class Employee //base class
{
  protected double basic;
  protected double gross;
  public virtual void CalcSal()
  {
    gross = basic + 0.5*basic;
  }
}
class Manager : Employee
{
  protected double allow;
  public override void CalcSal()
  {
    gross = basic + 0.5*basic + allow;
  }
}
types of classes
1.      general class
2.      static class
3.      sealed class
4.      partial classes
5.      generic classes
6.      abstract classes
working withsealed classes::

1.      sealed is a keyword,which can be used with classes and methods.
2.      sealed classes are not inheritable.
3.      sealed methods are not overridable.
4.      when a class is providing full functionality then it can be sealed.

Example on sealed
class class1
{
public override sealed int F1()
{
…….
}
}
class class2 : class1
{
public override int F1()
{
………
}
}
the above program raise an error message.

Abstract classes:

       Abstract is a keyword
       When a class is not providing full functionality , then it can be declared as abstract class
       A method without body is called as abstract method
       Methods cannot have implementation in base class
       Must be implemented in derived class and marked “override”
       The class containing at least one abstract method must be declared abstract
       A class with methods body and methods without body is called as partial abstract class
       To provide only methods with out body, then interfaces are required
       Partial abstract class and interfaces are not instantiatable
       Abstract methods must be over ridded in  derived classes.
       Reference works based on child class memory.
       Abstract classes are not instatiatable but a reference can be created
                        Test t= new test  // object
                              Test t; // reference
Example

//base class
abstract class Employee
{
  protected double basic;
  protected double gross;
  public abstract void CalcSal();
}
class Manager : Employee
{
  protected double allow;
  public override void CalcSal()
  {
    gross = basic + 0.5*basic + allow;
  }
}




No comments:

Post a Comment