Abstract Class

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,
My customer has an abstract class with some interface A . he wants me to
Inherit his Class and implement the intrface with some functionality in a new
class B.

I need to know how he would access my implementation without a Reference .

Can a base class generates his derived classes ? Like when i will use some
method it will be happen in others derived classes ?

Br
Ornez
 
I don't know if I understand, but, If your customer has an Abstract class
and wants you to implement an interface then all of the functionality will
come from the interface implementation plus any additional functionality you
design; an abstract class cannot be instantiated directly (MustInherit).
Consider the System.IO.Stream object. You can declare an object as a stream
but a derived class must be assigned to it.

'Cannot do
Dim Stream as NEW System.IO.Stream

'Can do
Dim Stream as System.IO.Stream
Stream = New System.IO.Memory

So, your customer can access your implementation by instantiating his
abstract class with a value of your derived class.

I could be way off here, someone will correct me shortly if that is the
case.

Hope this helps,
Jared
 
It was supposed to read
Stream = New System.IO.MemoryStream, I think my spell checker got a hold of
it.
 
Hi Jared,

First,thank you for your quick respond.

Let say that Class A has MustOverride Sub OnMessage(ByVal message As string)
and i need to implement in derived class, to popup a message box when the
sub is called . you wrote "So, your customer can access your implementation
by instantiating his abstract class with a value of your derived class. "
..What value of my derived Class ?

Thanks
Ornez
 
* "=?Utf-8?B?T3JuZXo=?= said:
Let say that Class A has MustOverride Sub OnMessage(ByVal message As string)
and i need to implement in derived class, to popup a message box when the
sub is called . you wrote "So, your customer can access your implementation
by instantiating his abstract class with a value of your derived class. "
.What value of my derived Class ?

An instance of the derived class can be seen as an instance of its base
class too. You can instantiate the base class by deriving a class from
it and instantiating the derived class.
 
Ornez,
Consider the following example: In block one, we have a declaration for
an object of type "AbstractClass", notice how you have to declare it without
the new keyword; we have initialized it with the value of a different class,
one of type "DerivedClass"
AbstractClass doesn't do anything except define the base functionality
(those elements that every derived class will have in common). The rest of
the functions and properties are DEFINED in the Interface. All of the actual
work is performed in the DerivedClass.

To see how this works do the following:
Create a new form, switch to code view, paste all the code in block two at
the very end of the file. Now, switch back to design view, add a button, in
the click event paste the contents of block one. Set a breakpoint on block
one and step through the code to see what happens. Herfried is my Newsgroup
Idol, but, he is way above most of us and it's hard to understand his points
at times. This small example should give you an idea of Inheritenance.
Microsoft has several good webcasts and videos; I recommend watching the
webcasts on Inheritance and Interfaces
http://msdn.microsoft.com/vbasic/atthemovies/
http://msdn.microsoft.com/vbtv/
http://msdn.microsoft.com/showsandwebcasts/default.aspx

Jared

'<Block One>
Dim Abstract As AbstractClass = New DerivedClass
With Abstract
.DoSomething("DoingSomething")
.MyProperty = "Setting the Property of myvalue"
.OnMessage()
End With
'</Block One>

<Block Two>
Public MustInherit Class AbstractClass
Implements MyInterface

MustOverride Function DoSomething(ByVal Something As String) As String
Implements MyInterface.DoSomething
MustOverride Property MyProperty() As String Implements
MyInterface.MyProperty
MustOverride Sub OnMessage() Implements MyInterface.OnMessage

End Class

Public Interface MyInterface

Sub OnMessage()
Function DoSomething(ByVal Something As String) As String
Property MyProperty() As String

End Interface

Public Class DerivedClass
Inherits AbstractClass

Private _mMyproperty As String
Public Overrides Function DoSomething(ByVal Something As String) As
String
MessageBox.Show("Something with a value of: " & Something & "
happened")
End Function
Public Overrides Property MyProperty() As String
Get
Return Me._mMyproperty
End Get
Set(ByVal Value As String)
Me._mMyproperty = Value
End Set
End Property
Public Overrides Sub OnMessage()
MessageBox.Show("OnMessage Handled")
End Sub

End Class
</Block Two>
 
Hi,
My customer has an abstract class with some interface A . he wants me to
Inherit his Class and implement the intrface with some functionality in a new
class B.

I need to know how he would access my implementation without a Reference .

Can a base class generates his derived classes ? Like when i will use some
method it will be happen in others derived classes ?

I'm guessing here a bit, but mostly likely your customer wants you to
implement MustOverride functions which will then be called automatically
from the base class when your derived class is instantiated. The base
class doesn't need to generate the derived class in this case, or know
very much about the derived class other than the fact that it implements
the MustOverride functions.

Public MustOverride Class AbstractBaseClass

Protected MustOverride Foo(ByVal sb as StringBuilder)

Public Sub Bar()
Dim sb as New StringBuilder
Foo(sb)
Console.WriteLine(sb.ToString())
End Sub

End Class

Public Class DerivedClass
Inherits AbstractBaseClass

Protected Overrides Sub Foo(ByVal sb As System.Text.StringBuilder)
sb.Append("Hello World")
End Sub
End Class


Dim d as New DerivedClass
d.Bar()

The Bar function will call the derived class's Foo method, even though
the base class doesn't even know the name of the derived class, what
assembly it exists it, and quite possible doesn't even have a reference
to the appropriate assembly.

It's all in the nature of virtual functions and how they work.
 
I must have a few things since I've been posting, in the past Herfried used
to "tear through" almost every post I made, now he is supporting my
response. ;-)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top