Solved

inheritance and overloading class methods in the subclass

Posted on 2014-10-24
5
216 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 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 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

726 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