ARTICLE

Inheritance in Object-oriented Programming.

Posted by Bhawana Nagar Articles | VB.NET Articles August 27, 2011
In this article, I will explain you about Inheritance in Object-oriented Programming.
 
Reader Level:

In this article, I will explain you about Inheritance in Object-oriented Programming.

 

INHERITANCE:

 

It is a primary feature of object-oriented programming,

Just like encapsulation and polymorphism. Inheritance is an element of Object-Oriented Programming by which we can drive the members of one class "base class" to the other class "child class". When we derive a class from a base class, the derived class will inherit all members of the base class except constructors. With the help of inheritance we can create an object that can reuse code. Once a method is defined in a super class, automatically inherited by all subclasses, they share common set of properties. Object-oriented programming allows classes to inherit commonly used state and behavior from other classes. By using inheritance we can reduce the testing and developing efforts. The behavior of the Base class can be changed by writing code in the Derived class. This technique is called overriding. With the new implementations of Derived class inherited methods can also be override. Inheritance allows you to build a hierarchy of related classes and to reuse functionality defined in existing classes. All classes created with Visual Basic are inheritable by default. In Visual Basic we use the Inherits keyword to inherit one class from other.


This code show you how to declare the inherit class:

Public Class Base
    ----
        ----
End Class
 
Public Class Derived
    Inherits Base
    'Derived class inherits the Base class
    ----
        ----
End Class

Derived classes inherit, and can extend the methods, properties, events of the Base class. With the use of inheritance we can use the variables, methods, properties, events etc, from the Base class and add more functionality to it in the Derived class.

Imports System. Console
Module Module1
 
    Sub Main()
        Dim Obj As New Subclass()
        WriteLine(Obj.sum())
        Read()
    End Sub
 
End Module
 
Public Class Superclass
    'base class
    Public X As Integer = 5
    Public Y As Integer = 15
 
    Public Function add() As Integer
        Return X + Y
    End Function
 
End Class
 
Public Class Subclass
    Inherits Base
    'derived class.Class Subclass inherited from class Superclass
    Public Z As Integer = 25
 
    Public Function sum() As Integer
        'using the variables, function from superclass and adding more functionality
        Return X + Y + Z
    End Function
 
End Class

Summary

Hope this article help you to understand Inheritance in Object-Oriented Programming.

Login to add your contents and source code to this article
share this article :
post comment
 
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Nevron Diagram
Become a Sponsor