Group Discounts available for 3+ students and Corporate Clients

Working with Inheritance in vb.net

Inheritance:

It is a collection of members without any definitions.

 

Syntax:

Interface <interface Name>

“Member(s) without definition

– – – – – – –

End interface

The members of the interface are by default “public” and hence the members present within the interface should not be defined using any modifier.

 

Note:

In order to use the members of the interface it is mandatory to implement the interface using the class.

 

Syntax to define a class for implementing interface(s):

Class <class Name> implements interface Name(s)

Member(s)

–  – – – – – –

End interface

 

Note:

  • A single class can implement any number of interfaces.
  • The class which implements an interface has to provide the complete definition for the members present within the interface else an object can’t be created for class.

 

 

Syntax to provide the definition for interface member:

Modifier sub method name ([arginfo]) implements

InterfaceName.memberName

Statement(s)

– – – – – – – –

End sub

 

Note:

It is not mandatory to have a same name for interface member and method which provides implementation at the class.

A single method can provide the implementation for any number of interface members.

 

Screenshot_33

 

(Multiple Inheritances is possible in JAVA & in JAVA JNH & implementation is same)

But VB.Net people think JNH & implements are different

JNH à Means taking the members & property of parent

Ex: Parents property to child if 3 Assets

JMP à Means giving the def for members

Ex: Parents loan clearing by a child.

‘‘Demo on Interface

Imports system

Module Interface ‘‘Demo1

Interface I Father

Sub My Property ( )

End Interface

Interface I Mother

Sub My Property ( )

End Interface

Class Child

Implements I Father, I Mother

Public sub My Property ( )

Implements I Father.My Property, I Mother.My Property ( )

Console.WriteLine (“Use property for business and charity”)

End sub

End Class

Sub main ( )

Dim C as Child

C.My Property ( )

End sub

End Module

Screenshot_34

 

‘‘Demo on Interface

Imports system

Module Interface ‘‘Demo2

Interface I Father

Sub My Property ( )

End Interface

Interface I Mother

Sub My Property ( )

End Interface

Class Child

Implements I Father, I Mother

Public sub Father Property ( )

Implements I Father.My Property

Console.WriteLine (“Use property for business”)

End sub

Public sub Mother Property ( )

Implements I Mother.My Property

Console.WriteLine (“Use property for charity”)

End sub

End Class

Sub main ( )

Dim C as New Child

C.Father Property ( )

C.Mother Property ( )

End sub

End Module

 

Must Inherit Class:

It is a class with a collection of members without any definitions.

Syntax:

Must Inherit class <class Name>

Member(s)

– – – – – – –

End class

 

Note:

Objects can’t be defined for Must Inherit class

In order to use the members of the Must inherit class it must be inherited by the other class and derived class should provide definition for members not defined within the must inherit class

Screenshot_35

 

 

 

Dim C as New abstract class () gives error ‘.’ We can’t create object for must inherit class

 

‘‘Demo on MustInherit Class

Imports system

Module MustInherit Class ‘‘Demo

MustInherit Class Abstract Class

Public sub Method Defined ()

Console.WriteLine (“Method Defined at Abstract Class”)

End sub

Public MustOverride sub

Method Undefined ()

End Class

Class Derived Class

Inherits Abstract Class

Public Overrides sub

Method Undefined ()

Console.WriteLine (“Method Defined at Abstract Class”)

End sub

End Class

Sub main ( )

Dim ac as New Abstract Class //error

Dim dc as new Derived Class

dc.MethodDefined ( )

dc.MethodUnDefined ( )

End sub

End Module

 

Not Inheritable Class:

It is used to define a class as the last class in the hierarchy of inheritance.

i.e.; NotInheritable class can’t be inherited further.

Note:

A NotInheritable class can inherit other class and can implement any no. of inheritances and objects can also be defined for the class.

 

 

Ex:

1

 

 

‘‘Demo on NotInheritable Class

Imports system

Module NotInheritable Class ‘‘Demo

Interface I Face

Sub My Method ()

End interface

Class BaseClass

Public sub says ()

Console.WriteLine (End of VB.Net Fundamentals”)

End sub

End Class

NotInheritable class FinalClass

Inherits BaseClass

Implements I Face

Public sub MyMethod () Implements IFace.MyMethod

Console.Write (“MyMethod says”)

End sub

End class

Sub main ()

Dim fc as New FinalClass

fc.MyMethod ()

fc.says ()

End sub

End Module

 

“At TekSlate, we are trying to create high quality tutorials and articles, if you think any information is incorrect or want to add anything to the article, please feel free to get in touch with us at info@tekslate.com, we will update the article in 24 hours.”

0 Responses on Working with Inheritance in vb.net"

    Leave a Message

    Your email address will not be published. Required fields are marked *

    Support


    Please Enter Your Details and Query.
    Three + 6