PROGRAMMER DESK

June 16, 2009

Inheritance

Inheritance

Written by – Vinay Hatwal

Dt. 16-Jun-2009

                                                                               

Inheritance is the OOPs concept in which new classes form from the existing classes i.e. using the class functionality in the new classes. Inheritance was first introduced in 1967 for Simula, a language which was used for simulation so the name is Simula.

Inheritance is intended to help reuse existing code with little or no modification. Inheritance is also sometimes called generalization, because the is-a relationships represent a hierarchy between classes of objects.

For Example, A “vehicle” is the generalization of “Car”, “Scooter”, “Bus” etc and the “Car”, “Scooter”, ”Bus” are the specialization of the “Vehicle”. Because the “Car” is a Vehicle so “Car” naturally inherits properties common to the entire vehicle like Color, No of Wheels, and EnginPower etc.

Following example show the inheritance in action. Shape is the Base Class and Square , Rectangle amd Circle are the classes derived from Shape Class and inherits all the functionality of the Shape class the shows the specialisation of the Shape Class.

Inheritance

Inheritance

Inheritance in Action

—————————————————————

VB.NET

—————————————————————

Public Class BaseClass

 

End Class

 

 

Public Class DerivedClass

    Inherits BaseClass

 

End Class

—————————————————————

 

—————————————————————

C#.NET

—————————————————————

public class BaseClass

{

}

 

public class DerivedClass : BaseClass

{

}

 

—————————————————————

 

 

 

Above DerivedClass Will contain all the inheritable funtionality of the BaseClass. Inheritane can be used in

 

VB.NET – Inherits

C#.NET – :

 

 

 

Access Specifires

 

Functionality VB.NET C#.NET
Gives variable public access which means that there is no restriction on their accessibility Public public
Gives variable private access which means that they are accessible only within their declaration content Private private
Protected access gives a variable accessibility within their own class or a class derived from that class Protected protected
Gives variable friend access which means that they are accessible within the program that contains their declaration Friend internal
Gives a variable both protected and friend access Protected Friend protected internal
Makes a variable static which means that the variable will hold the value even the procedure in which they are declared ends Static static
Declares a variable that can be shared across many instances and which is not associated with a specific instance of a class or structure Shared static
Makes a variable only to be read and cannot be written ReadOnly -

 

 

Keywords used in Inheritance in VB.NET/C#

 

1)      MustInherit, MustOverride /abstract

2)      Overridable/virtual

3)      Overrides/override

4)      NonInheritable/sealed

5)      Shadows/New

 

Concepts Regarding Interitance

1)      Abstraction

Abstraction is “the process of identifying common patterns that have systematic variations; an abstraction represents the common pattern and provides a means for specifying which variation to use” (Richard Gabriel).

In Abstraction we will look only on the keywords

a)      MustInherit, MustOverride /abstract

 

Abstract Classes or MustInherit class can not me instantiated. The can only be inherited in the derived class.

For Example –

 

—————————————————————

VB.NET

—————————————————————

 

Public MustInherit Class Base

    Public Sub NormalMethod()

    End Sub

    Public MustOverride Sub MustOverridableMethod()

End Class

 

 

Public Class Derived

    Inherits Base

    Public Overrides Sub MustOverridableMethod()

 

    End Sub

End Class

 

 

—————————————————————

 

 

—————————————————————

C#.NET

—————————————————————

 

public abstract class Base

{

        public void NormalMethod()

        {

        }

        public abstract void MustOverridableMethod();

}

   

  public class Derived : Base

  {

 

      public override void MustOverridableMethod()

              {

              throw new NotImplementedException();

      }

}

 

—————————————————————

 

 

In the above example if you are declaring the method as MustOverride/ abstract then u have to give definition of that method in the derived class. But if you are defining the method in the MustInherit/Abstract class then you may or may not define this method in the derived class.

 

 

 

b)      Overridable/virtual and Overrides/override

 

Overridable/virtual is used to polimorphically declare the base class method. A non overridable/not virtual method can not me polymorfically declared in the base class because it will hide the base class method in the derived class (Refer to Shadowing).

When we declare a Overridable/virtual method, it must contain a method body. Other wise the compiler will generate an error. Remember that, since Overridable/virtual methods are used for achieving polymorphism and since polymorphism works only with objects, it not possible to declare a static method as virtual in C#. Similarly the private methods are also not possible to declare virtual, since they can’t override inside a derived class.

 

For Example –
—————————————————————

VB.NET

—————————————————————

 

Public Class PolyBase

    Public Overridable Function GetName() As String

        Return “Base Polymorphic Class”

    End Function

End Class

 

Public Class PolyDerived

    Inherits PolyBase

    Public Overrides Function GetName() As String

        Return “Derived Polymorphic Class”

    End Function

End Class

 

—————————————————————

 

 

—————————————————————

C#.NET

—————————————————————

 

public class PolyBase

    {

        public virtual string GetName()

        {

            return “Base Polymorphic Class”;

        }

    }

 

    public class PolyDerived : PolyBase

    {

        public override string GetName()

        {

            return “Derived Polymorphic Class”;

        }

    }

—————————————————————

 

 

2)      Sealed Classes –

 

Sealed Class can be instanciated but can not be inherited in another class .In VB.Net NotInheritable keyword is used to declare a class Seald and In C#.Net it is declared by sealed.

 

—————————————————————

VB.NET

—————————————————————

 

Public NotInheritable Class NotInheritableClass

    Public Function Fun1() As String

        Return “My class can not be inherited”

    End Function

End Class

—————————————————————

 

 

—————————————————————

C#.NET

—————————————————————

 

sealed class NonInheritableClass

       {

        public string Fun1()

        {

            return “this class can not be inherited”;

        }

}

—————————————————————

 

 

 

 

3)      Shadowing

In .Net it is possible to declare the member of the base class with the same name in the derived class. In this class we are actully hiding the base class member in the derived class , if u do so then .Net compiler will show a warning.

Consider the following example-

 

—————————————————————

VB.NET

—————————————————————

sh

—————————————————————

 

 

 

—————————————————————

C#.NET

—————————————————————

ssss

—————————————————————

 

 

See the compiler warning closely.

In VB.Net it is clearly advising to use the Shadows with the new Fun1() to hide the base class functionality. Here the correct code is -

—————————————————————

VB.NET

—————————————————————

 

Public Class Base

    Private x As Integer

    Private y As Integer

    Public Function Fun1() As Integer

        Return x + y

    End Function

End Class

 

 

Public Class Derived

    Inherits Base

    Public Shadows Function Fun1() As Integer

        Return 10

    End Function

End Class

—————————————————————

 

Shadows keyword hides the base class functionality and warning too.

 

And in the C# it is advising to use the new keyword to hide the base class method. Here the correct code is-

 

—————————————————————

C#.NET

—————————————————————

 

class Base

    {

        private  int x = 10;

        private int y = 20;

        public int Fun1()

        {

            return x + y;

        }

    }

   

    class Derived : Base

    {

        public new int Fun1()

        {

            return 10;

        }

    }

 

—————————————————————

 

__________________________________________________________________________

 

Blog at WordPress.com.