-
Mar 15th, 2023, 10:07 AM
#1
Thread Starter
Frenzied Member
[RESOLVED] Will the child class inherit the parent class's Friend methods?
I know that outside the project, the Friend methods are not visible, while within the project, the Friend methods can be accessed by other classes (objects). But because .Net has some special regulations for AccessModifiers, such as one class(object) can access the private members of another class(object) in the same code file. So I'm wondering, in the same project, whether the child-class will inherit the Friend methods of the parent class, for example:
Code:
Class MyBaseClass1
Friend Sub Method1()
End Sub
Public Sub Method2()
End Sub
End Class
Code:
Class MyClass2
Inherits MyBaseClass1
Public Sub Method3()
End Sub
End Class
Code:
Module Module1
Sub Main()
Dim objClass2 As New MyClass2
objClass2.Method1()
ObjClass2.Method2()
ObjClass2.Method3()
End Sub
End Module
I wonder if the redline code can execute normally, thanks.
Last edited by SearchingDataOnly; Mar 15th, 2023 at 10:42 AM.
-
Mar 15th, 2023, 10:14 AM
#2
Re: Will the child class inherit the parent class's Friend methods?
Did you test this yourself?
-
Mar 15th, 2023, 10:14 AM
#3
Re: Will the child class inherit the parent class's Friend methods?
Well, I'd say that you should just try it out, but in your example, you highlighted a call to Method2 in red, but it was Method1 that was Friend. In other words, if you want to try it out, you need to call Method1, not Method2.
What you will find is that Friend is the same as Public, it's just that the method will only be visible within the same assembly. That mostly just matters when creating class libraries where you want objects within the library to be able to access certain methods, while not exposing the methods outside of the library. That makes testing this a bit tricky, as it would best be tested using a dll along with some other test project that referenced the dll. Not terribly difficult, but a little tricky.
My usual boring signature: Nothing
-
Mar 15th, 2023, 10:23 AM
#4
Thread Starter
Frenzied Member
Re: Will the child class inherit the parent class's Friend methods?
Originally Posted by wqweto
Did you test this yourself?
Originally Posted by Shaggy Hiker
Well, I'd say that you should just try it out, but in your example, you highlighted a call to Method2 in red, but it was Method1 that was Friend. In other words, if you want to try it out, you need to call Method1, not Method2.
What you will find is that Friend is the same as Public, it's just that the method will only be visible within the same assembly. That mostly just matters when creating class libraries where you want objects within the library to be able to access certain methods, while not exposing the methods outside of the library. That makes testing this a bit tricky, as it would best be tested using a dll along with some other test project that referenced the dll. Not terribly difficult, but a little tricky.
@wqweto, @Shaggy Hiker,
Sorry for asking such a childish question. Although I often use vscode to browse C# and VB.NET code, I don't have VisualStudio.Net installed on my computer and I can't test it myself.
-
Mar 15th, 2023, 11:57 AM
#5
Re: Will the child class inherit the parent class's Friend methods?
So install it .. .the community edition is free... install, test, learn resutls...
-tg
-
Mar 15th, 2023, 12:03 PM
#6
Re: Will the child class inherit the parent class's Friend methods?
Originally Posted by SearchingDataOnly
@wqweto, @Shaggy Hiker,
Sorry for asking such a childish question. Although I often use vscode to browse C# and VB.NET code, I don't have VisualStudio.Net installed on my computer and I can't test it myself.
You can use VSCode to run and Debug C# code
-
Mar 15th, 2023, 12:46 PM
#7
Re: Will the child class inherit the parent class's Friend methods?
I said you should try it out not because I thought the question was childish, but because I felt there are likely nuances that I'm not aware of. I use Friend in this way in some class libraries for the reasons I mentioned. There are aspects of how it will behave that I am not familiar with, as I have not tried them. I use it for functionality that makes sense within a class library, but which I don't want to expose outside of the class library. Sometimes, though rarely, this is part of an inheritance relationship for objects within the class library, but I've largely moved away from inheritance.
My usual boring signature: Nothing
-
Mar 15th, 2023, 08:39 PM
#8
Thread Starter
Frenzied Member
Re: Will the child class inherit the parent class's Friend methods?
Thank you techgnome, PlausiblyDamp and Shaggy Hiker for your suggestions.
I tested it on the dotnetfiddle.net as suggested by dday9. The test results show that "Friend Methods" in a parent class are also inherited by the child class. The test code is as follows:
Code:
Imports System
Public Module Module1
Public Sub Main()
Dim objClass2 As New MyClass2
objClass2.Method1()
objClass2.Method2()
objClass2.Method3()
End Sub
End Module
Class MyClass1
Friend Sub Method1()
Console.WriteLine("Method1")
End Sub
Public Sub Method2()
Console.WriteLine("Method2")
End Sub
End Class
Class MyClass2
Inherits MyClass1
Public Sub Method3()
Console.WriteLine("Method3")
End Sub
End Class
Last edited by SearchingDataOnly; Mar 15th, 2023 at 08:52 PM.
-
Mar 15th, 2023, 08:43 PM
#9
Thread Starter
Frenzied Member
Re: Will the child class inherit the parent class's Friend methods?
@techgnome,
Can "Friend Methods" in a parent class be inherited by the child class in Java?
-
Mar 15th, 2023, 08:50 PM
#10
Thread Starter
Frenzied Member
Re: Will the child class inherit the parent class's Friend methods?
Originally Posted by Shaggy Hiker
Sometimes, though rarely, this is part of an inheritance relationship for objects within the class library, but I've largely moved away from inheritance.
Olaf seems to have said that in modern programming patterns, interfaces are a better choice than inheritance. But I still don't understand what he means. That said, I haven't figured out how to replace inheritance with interfaces.
-
Mar 16th, 2023, 03:17 AM
#11
Re: Will the child class inherit the parent class's Friend methods?
Originally Posted by SearchingDataOnly
Olaf seems to have said that in modern programming patterns, interfaces are a better choice than inheritance. But I still don't understand what he means. That said, I haven't figured out how to replace inheritance with interfaces.
It really depends on what you're trying to achieve. Inheritance is pretty much required throughout the base class library but there's much less call for it in developing an application. That doesn't mean that it doesn't have a place though. Interfaces, on the other hand, are pretty much required for remotely large-scale projects and especially if you want to be able to test them effectively.
-
Mar 16th, 2023, 09:19 AM
#12
Thread Starter
Frenzied Member
Re: Will the child class inherit the parent class's Friend methods?
Originally Posted by jmcilhinney
It really depends on what you're trying to achieve. Inheritance is pretty much required throughout the base class library but there's much less call for it in developing an application. That doesn't mean that it doesn't have a place though. Interfaces, on the other hand, are pretty much required for remotely large-scale projects and especially if you want to be able to test them effectively.
I mean using interfaces to achieve the effect of inheritance. Golang has no classes, but it implements object-oriented programming through interfaces and anonymous types embedded in structs.
Last edited by SearchingDataOnly; Mar 16th, 2023 at 09:32 AM.
-
Mar 16th, 2023, 07:35 PM
#13
Re: Will the child class inherit the parent class's Friend methods?
Originally Posted by SearchingDataOnly
I mean using interfaces to achieve the effect of inheritance. Golang has no classes, but it implements object-oriented programming through interfaces and anonymous types embedded in structs.
The term "interface" can mean different things in different languages. Until recently, .NET interfaces couldn't have any implementation at all, so it was impossible to inherit any actual functionality from an interface. I think it's still impossible in VB.NET, although interfaces defined in C# can have default implementations these days. .NET interfaces can provide the appearance of doing the same thing as inheritance from the outside: there's a single type with a bunch of members, then there's one or more other types that can be treated as that original type and has the same members. That looks basically the same to the consuming code and adds testability and some other advantages, but the person writing that code in the first place would need to implement each of those common members in each of the additional classes, even if the implementation was the same in many of them or all of them in some cases. Think of the Object.ToString method. Many types simply inherit that method implementation from the Object class but, if Object were an interface, each of those types would have to provide that same implementation itself.
-
Mar 17th, 2023, 08:51 AM
#14
Thread Starter
Frenzied Member
Re: [RESOLVED] Will the child class inherit the parent class's Friend methods?
What you said makes sense
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|