Solved

inheritance and overloading class methods in the subclass

Posted on 2014-10-24
5
187 Views
Last Modified: 2014-10-24
Is it possible to declare a method in a class as overloadable and then overload it in a subclass?
Is there a 'mustoverride' equivalent for 'mustloverload' to force an implementation in a subclass?

example

Public MustInherit Class Beverage2

    Public description As String

    Overridable Function getDescription() As String
        Return description
    End Function

End Class

Public Class Pepsi
    Inherits Beverage2

    Sub New()
        description = "pepsi"
    End Sub

    Public Shadows Function getDescription(v As String) As String
        Return description & " "
    End Function

End Class
0
Comment
Question by:glenn_r
  • 2
  • 2
5 Comments
 
LVL 32

Expert Comment

by:it_saige
ID: 40403001
Did you mean something like this -
Public MustInherit Class Beverage
	Public MustOverride Property Description() As String

	Public Overridable Shadows Function ToString() As String
		Return Description
	End Function
End Class

Public Class Pepsi : Inherits Beverage
	Private _description As String
	Public Overrides Property Description() As String
		Get
			Return _description
		End Get
		Set(ByVal value As String)
			If Not value.Equals(_description) Then
				_description = value
			End If
		End Set
	End Property

	Public Sub New(ByVal Description As String)
		_description = Description
	End Sub

	Public Overrides Function ToString() As String
		Return MyBase.ToString()
	End Function
End Class

Module Module1
	Sub Main(ByVal args As String())
		Dim drink As Pepsi = New Pepsi("Cool refreshing cola")
		Console.WriteLine(drink.ToString())
		Console.ReadLine()
	End Sub
End Module

Open in new window

Produces the following output:Capture.JPG
-saige-
0
 

Author Comment

by:glenn_r
ID: 40403017
i want to do what you've illustrated but with overloading a method
Is it possible to overload a method of a derived class?
0
 
LVL 15

Accepted Solution

by:
ChloesDad earned 500 total points
ID: 40403031
As long as you declare the method in the base class as overloads then you can add overloaded methods in the child class.

eg

public overloads sub SomeSub()

End sub

in Child Class

Public Overloads Sub SomeSub(paramlist)

End Sub

There is no Must Overload construct
0
 

Author Comment

by:glenn_r
ID: 40403049
got it
in all my years doing this i never thought about that one
thanks again
i need to find a pepsi in a vintage glass bottle those are the best!!
0
 
LVL 32

Expert Comment

by:it_saige
ID: 40403051
As ChloesDad stated, there is no way to force overloading of a method.  The reason for this is because overloading is optional and overloading a method basically says to the compiler, "I am defining a method that my parent contains.  I may choose to use the parent method or just provide a different set of instructions alltogether."

-saige-
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
visual Basic Net vs c# net 8 56
Expando 4 36
Clone Visual studio 2013 solution 5 17
How to open a link from vb.net app 3 10
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now