?
Solved

inheritance and overloading class methods in the subclass

Posted on 2014-10-24
5
Medium Priority
?
224 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 34

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 2000 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 34

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

762 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