Logo

Navigation
  • Home
  • Services
    • ERP Solutions
    • Implementation Solutions
    • Support and Maintenance Solutions
    • Custom Solutions
    • Upgrade Solutions
    • Training and Mentoring
    • Web Solutions
    • Production Support
    • Architecture Designing
    • Independent Validation and Testing Services
    • Infrastructure Management
  • Expertise
    • Microsoft Development Expertise
    • Mobile Development
    • SQL Server Database and BI
    • SAP BI, SAP Hana, SAP BO
    • Oracle and BI
    • Oracle RAC
  • Technical Training
    • Learn Data Management
      • Business Intelligence
      • Data Mining
      • Data Modeling
      • Data Warehousing
      • Disaster Recovery
    • Learn Concepts
      • Application Development
      • Client Server
      • Cloud Computing Tutorials
      • Cluster Computing
      • CRM Tutorial
      • EDI Tutorials
      • ERP Tutorials
      • NLP
      • OOPS
      • Concepts
      • SOA Tutorial
      • Supply Chain
      • Technology Trends
      • UML
      • Virtualization
      • Web 2.0
    • Learn Java
      • JavaScript Tutorial
      • JSP Tutorials
      • J2EE
    • Learn Microsoft
      • MSAS
      • ASP.NET
      • ASP.NET 2.0
      • C Sharp
      • MS Project Training
      • Silverlight
      • SQL Server 2005
      • VB.NET 2005
    • Learn Networking
      • Networking
      • Wireless
    • Learn Oracle
      • Oracle 10g
      • PL/SQL
      • Oracle 11g Tutorials
      • Oracle 9i
      • Oracle Apps
    • Learn Programming
      • Ajax Tutorial
      • C Language
      • C++ Tutorials
      • CSS Tutorial
      • CSS3 Tutorial
      • JavaScript Tutorial
      • jQuery Tutorial
      • MainFrame
      • PHP Tutorial
      • VBScript Tutorial
      • XML Tutorial
    • Learn Software Testing
      • Software Testing Types
      • SQA
      • Testing
  • Career Training
    • Career Improvement
      • Career Articles
      • Certification Articles
      • Conflict Management
      • Core Skills
      • Decision Making
      • Entrepreneurship
      • Goal Setting
      • Life Skills
      • Performance Development
      • Personal Excellence
      • Personality Development
      • Problem Solving
      • Relationship Management
      • Self Confidence
      • Self Supervision
      • Social Networking
      • Strategic Planning
      • Time Management
    • Education Help
      • Career Tracks
      • Essay Writing
      • Internship Tips
      • Online Education
      • Scholarships
      • Student Loans
    • Managerial Skills
      • Business Communication
      • Business Networking
      • Facilitator Skills
      • Managing Change
      • Marketing Management
      • Meeting Management
      • Process Management
      • Project Management
      • Project Management Life Cycle
      • Project Management Process
      • Project Risk Management
      • Relationship Management
      • Task Management
      • Team Building
      • Virtual Team Management
    • Essential Life Skills
      • Anger Management
      • Anxiety Management
      • Attitude Development
      • Coaching and Mentoring
      • Emotional Intelligence
      • Stress Management
      • Positive Thinking
    • Communication Skills
      • Conversation Skills
      • Cross Culture Competence
      • English Vocabulary
      • Listening Skills
      • Public Speaking Skills
      • Questioning Skills
    • Soft Skills
      • Assertive Skills
      • Influence Skills
      • Leadership Skills
      • Memory Skills
      • People Skills
      • Presentation Skills
    • Finding a Job
      • Etiquette Tips
      • Group Discussions
      • HR Interviews
      • Interview Notes
      • Job Search Tips
      • Resume Tips
      • Sample Resumes
 

Implementing Inheritance

By Exforsys | on June 5, 2005 |
VB.NET 2005

Introduction and Implementing Inheritance

The usefulness of inheritance is depnding on the choice of its usage. You can use inheritance if the derived class is a kind of base class but not has a relationship with the base class. Such a situation empowers you to reuse the code from the base class. It is also more useful if the hierarchy is very shallow. The developer can effect global changed to the derived class by changing the base class.

Understanding Inheritance

Inheritance is an important feature of any OOP Language. Let us examine the following lines of code:

Public Class DemoForm

Inherits System.Windows.Forms.Form

…………

End Class

Note the use of public in the above code. You can use any of the four access specifiers namely Public, Private, Protected, and private, while declaring the classes. This usage depends upon the need of the program to make the other classes capable of inheriting from this class.

Note the use of public in the above code. You can use any of the four access specifiers namely Public, Private, Protected, and private, while declaring the classes. This usage depends upon the need of the program to make the other classes capable of inheriting from this class.

In the above code we have created a new class DemoForm based on the class System.Windows.Forms.Form. The new class DemoForm is the derived class and the class Form is the base class.

Some of the points of interest about inheritances are given below:

  • In Visual Basic .NET only single inheritance is permitted; that is, any derived class can have only one base class
  • You can inherit all classes except those marked with keywork NotInheritable.
  • You can restrict certain items in base class from being exposed by using access specifiers. Access type of a derived class must be equal or more restrictive than its base class for the inheritance to succeed.

Inherits keyword is used before the base class to specify the base class. MustInherit modifier is used to specify that the class is intended for use as a base class only.

The derived class inherits all the methods defined in the base class by default. You may come across some instances that a particular method needs to behave differently in the derived class. In such a case, the method can be overridden in the derived class. In this case you have to specify a new implementation of the method in the derived class. This is possible only if the method in the base class is marked with keywork overridable.

The method in the derived class should be declared as overrides. Declaring a method as NotOverridable prevents you from overriding the method in a derived class. Public mehtods are NotOverridable by default. You can also declare a method as MustOverride which makes it mandatory for you to override the method in a derived class. Any method that is declared as MustInherit should not contain any body in the method statement.

You can refer to any method in the base class by using the keyword MyBase as illustrated in the code fragment given below.

Public Class BaseClass1

Public Overridable Sub someMethod()

‘… your code

End Sub

End Class

Class DerivedClass1

Inherits BaseClass1

Public Overrides Sub someMethod()

‘The codes for the Sub …

End Sub

Public Sub baseClassMethod()

MyBase.someMethod()

End Sub

End Class

Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Dim DerivedClass As New DerivedClass1

DerivedClass.someMethod()

DerivedClass.baseClassMethod()

End Sub

End Class

In the above illustration calling the sub someMethod calls the overridden method in the derived class. However the statement MyBase.someMethod calls the Sub defined in the base class. MyBase keyword can be used to access the immediate base class and its inherited members. Private members of the base class cannot be accessed using MyBase keyword.

It is not an object and therefore cannot be assigned to variables or passed on procedure or used for Is comparison. However MyBase keyword cannot be used to access a Sub declared as MustOverride in the base class.MyBase cannot be used in modules and to access base class members that are marked as friend if the base class is in a different assembly.

MyClass keyword

This keyword allows you to call an overridable method implemented in your clas and make sure that implementation of the method in the specified class is called instead of the overridden method in the derived class.Like MyBase MyClass is also a keyword and not a class. MyClass keyword can be used to refer the containing class and its inherited members.

MyClass can be used to qualify the shared members and it cannot be used in standard modules. In case the method has no implementation in the derived class MyClass keyword can be used to refer the method in the base class and therefore the effect of such usage is the same as the use of MyBase.

Implementing Inhertance

Class BaseClass1

Sub Method1()

MessageBox.Show("This is a method in the base class.")

End Sub

Overridable Sub Method2()

MessageBox.Show("This is another method in the base class.")

End Sub

End Class

Class DerivedClass2

Inherits BaseClass1

Public Field2 As Integer

Overrides Sub Method2()

Messagebox.Show("This is a method in a derived class.")

End Sub

Protected Sub TestInheritance()

Dim C1 As New BaseClass1()

Dim C2 As New DerivedClass2()

C1.Method1() ‘ Calls a method in the base class.

C1.Method2() ‘ Calls another method from the base class.

C2.Method1() ‘ Calls an inherited method from the base class.

C2.Method2() ‘ Calls a method from the derived class.

End Sub

End Class

When you run the procedure TestInheritance, you see the following messages:

"This is a method in the base class."

"This is another method in the base class."

"This is a method in the base class."

"This is a method in a derived class."

When to use inhertance

The usefulness of inheritance is depnding on the choice of its usage. You can use inheritance if the derived class is a kind of base class but not has a relationship with the base class. Such a situation empowers you to reuse the code from the base class. It is also more useful if the hierarchy is very shallow. The developer can effect global changed to the derived class by changing the base class.

Inheritance and .NET Framework

In .NET Framework the inheritance is implemented with additional functionality. Any class created in any of the programing language in the Framework can be inherited in another language. Thus cross language inheritance is also implemented in this Framework.

The System.Object class serves as a common base class for all objects in the .NET framework, that ensures interoperability of objects developed using Visual Studio .NET. New classes implicitly inherit the System.Object class; therefore it is never necessary to explicitly name this class with an Inherits statement. Among the methods that objects inherit from System.Object, one of the most useful is Object.GetType, which is used to return the exact type of the current object.

« « Implementing Class Library Object
Do You Really Want to be an IT Consultant: The Pros and Cons of Being Your Own Boss » »

Author Description

Avatar

Editorial Team at Exforsys is a team of IT Consulting and Training team led by Chandra Vennapoosa.

Free Training

RSSSubscribe 394 Followers
  • Popular
  • Recent
  • Working with Menu Controls

    July 3, 2005 - 0 Comment
  • Web Services – SOAP, WSDL, Disco and UDDI

    July 16, 2005 - 0 Comment
  • Deploying Windows Applications In Visual Studio.NET 2005

    August 3, 2005 - 0 Comment
  • Application Class and Message Class

    June 8, 2005 - 0 Comment
  • .NET Complex Data Binding

    July 7, 2005 - 0 Comment
  • VB.NET MDI Applications

    July 3, 2005 - 0 Comment
  • Creating Web Service

    July 16, 2005 - 0 Comment
  • The Registry Editor in Visual Studio.NET 2005

    August 4, 2005 - 0 Comment
  • Setting and Adding Properties to Windows Form

    June 8, 2005 - 0 Comment
  • .NET Data Form Wizard

    July 9, 2005 - 0 Comment
  • Microsoft .NET Creating Installation Components

    August 10, 2005 - 0 Comment
  • Shared Assembly

    August 9, 2005 - 0 Comment
  • The File Types Editor

    August 9, 2005 - 0 Comment
  • Tracing VB.NET Windows Application

    August 9, 2005 - 0 Comment
  • VB.NET Windows Application Testing

    August 9, 2005 - 0 Comment
  • The Registry Editor in Visual Studio.NET 2005

    August 4, 2005 - 0 Comment
  • Customizing Setup Project in Visual Studio.NET 2005

    August 4, 2005 - 0 Comment
  • Deploying Windows Applications In Visual Studio.NET 2005

    August 3, 2005 - 0 Comment
  • Debugging Windows Applications In Visual Studio.NET 2005

    August 3, 2005 - 0 Comment
  • Working with Legacy Code and COM Components

    July 30, 2005 - 0 Comment

Exforsys e-Newsletter

ebook
 

Related Articles

  • Microsoft .NET Creating Installation Components
  • Shared Assembly
  • The File Types Editor
  • Tracing VB.NET Windows Application
  • VB.NET Windows Application Testing

Latest Articles

  • Project Management Techniques
  • Product Development Best Practices
  • Importance of Quality Data Management
  • How to Maximize Quality Assurance
  • Utilizing Effective Quality Assurance Strategies
  • Sitemap
  • Privacy Policy
  • DMCA
  • Trademark Information
  • Contact Us
© 2023. All Rights Reserved.IT Training and Consulting
This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish.AcceptReject Read More
Privacy & Cookies Policy

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
Non-necessary
Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.
SAVE & ACCEPT