Monday, December 21, 2009

Inheritance

Inheritance is a process to create new class from an exiting class.

Or

Inheritance is the process that allows the reuse of an exiting class to build a new class.

Mainly,Inheritance is based on the principal of code reuse.Instead of creating a class from scratch,it is possible to add new methods,properties and events (events are responce to user actions.)to exiting classes,thus saving time and efforts.The class on which the new class is created is called Base class or Parent class and the class that is created is called Derived, Sub or Child class.

Example:

using System;

namespace InheritanceDemo1
{
//Base class
public class Person
{
private string_name;
private int_age;
public void GetInfo()
{
Console.WriteLine("Enter your name = ");
_name = Console.ReadLine();
Console.WriteLine("Enter your age = ");
_age = int.Parse(Console.ReadLine());
}
public void DispInfo()
{
Console.WriteLine("Dear {0},Your age is {1}",_name,_age) ;
}
}

//Derived class
public class Student:Person
{
private string_school;
private int_english;
private int_math;
private int_science;
public void GetMarks()
{
Console.WriteLine("Enter school names = ");
_school = Console.ReadLine();
Console.WriteLine("Enter marks for English,Maths,Science");
_english = int.Parse(Console.ReadLine());
_math = int.Parse(Console.ReadLine());
_science = int.Parse(Console.ReadLine());
Console.WriteLine("Total Marks obtained : {0}",_english+_math+_science);
}
}
class Class1
{
[STAThread]
static void Main(string[] args)
{
Student objstudent = new Student();
objstudent.GetInfo(); //Accessing base member
objstudent.DispInfo(); //Accessing base member
objstudent.GetMarks();
}
}
}

Base Keyword
The keyword base is used to access members of the base class from inside a derived class.To call a method of the base class is possible even if it has been overridden in the derived class by using the keyword base.Also,creating an instance of a derived class
The constructor of the base classes can be called using base keyword
Constructor, an instance method of an instance property accessory only of the base class can be accessed using the keyword base.

Example:

using System;

namespace InheritanceDemo1
{
public class CA
{
private int priA;
public CA()
{ priA =100;
}
protected void fun(){
Console.WriteLine("From CA"); }
}
public class CB:CA {
private int priB;
public CB(){
priB = 200; }
public void foo(){
base.fun();
Console.WriteLine("From CB");
}
public class UsingBaseKeyword
{
statci void Main(string[] args) {
CB b = new CB();
b.foo(); }}}

* Calling base class constructor by using derived class using base keyword.

Example:

using System;
namespace InheritanceDemo1
{
//Base class
public class Person
{
private string_name;
private int_age;
//base supplies constructor to perform initialization
public Person(string name,int age)
{
this._name = name;
this._age = age;
Console.WriteLine("Name = {0} ",name);
Console.WriteLine("Age = {0} ",age);
}
}


//Derived class
public class Student:Person
{
private int _id;
public Student(String name,int age,int id):base (name,age)
{
this._id=id;
Console.WriteLine("Id = {0} ",id);
}
}
class Class1
{
[STAThread]
static void Main(string[] args)
{
Student objstudent = new Student("Raj",24,4);
}
}
}


Method Overriding in C#

Overriding :- Overriding a method of base class means changing its implementation or overwriting it in the derived class.
*Override keyword:
The override keyword is used to modify a method.An override method provides a new implementation of the base method.For this the base class method has to be declared virtual.Adding virtual to a base class method marks it to for overriding its implementation in the derived class.By default, methods are non-virtual in C# and hence they cannot be overridden.

Note:-
1. The overridden method in a derived class should have the same signature as the base class method it is overriding.
2. The keywords new,static,virtual cannot be used along wirg the override modifier.

*Virtual keyword :
C# provides a keyword virtual that is used the definition of a method to support polymorphism.The virtual keyword is used to modify a method declaration in a class.Such method is called a virtual method.The child classes are now free to implement their own versions of thr virtual method

using override keyword.
The Syntax:-
[Access modifier]virtual[return tyoe]name([parameter list])
{
//Virtual method implementation.
}

Note: The virtual modifier cannot be used with modifier like static and override.
Ex:-
Public virual void function_name()
{
Console.WriteLine("This is a virtual method and can be overridden");
}


* When a virtual method is called ,a runtime check is made to identify the object and the appropriate method is invoked.In case of non-virtual methods,this information is available at compile time,so no runtime check to identify the object is done.
* "new" keyword:- The new keywordmis used as an operator or as a modifier,This section is discussingthe new modifier.The new modifier is used to explicitly hide a member that is inherited from the base class.This means in case if the derived class has a member with the same name as a member in the parent class,then new distinguishes the derived class member as a completely new member.

* Note:- It is an error to use both new an override on the same method.
* Note:- To conclude ,the real benefit of "new modifier" is to be sure that the intention is to hide the base method and this did not happen by accident or misspelling
* Conversely,if a method is declared as new and it does not really hide a base method ,the compiler will also issue a warning that can be suppressed by removing the new modifier.

No comments:

Post a Comment