Solved

inheritance and overloading class methods in the subclass

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

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 33

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

828 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