Results 1 to 14 of 14

Thread: [RESOLVED] Will the child class inherit the parent class's Friend methods?

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2020
    Posts
    1,421

    Resolved [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.

  2. #2

  3. #3
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    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

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2020
    Posts
    1,421

    Re: Will the child class inherit the parent class's Friend methods?

    Quote Originally Posted by wqweto View Post
    Did you test this yourself?
    Quote Originally Posted by Shaggy Hiker View Post
    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.

  5. #5
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,531

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  6. #6
    PowerPoster PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Pontypool, Wales
    Posts
    2,458

    Re: Will the child class inherit the parent class's Friend methods?

    Quote Originally Posted by SearchingDataOnly View Post
    @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

  7. #7
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    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

  8. #8

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2020
    Posts
    1,421

    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.

  9. #9

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2020
    Posts
    1,421

    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?

  10. #10

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2020
    Posts
    1,421

    Re: Will the child class inherit the parent class's Friend methods?

    Quote Originally Posted by Shaggy Hiker View Post
    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.

  11. #11
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: Will the child class inherit the parent class's Friend methods?

    Quote Originally Posted by SearchingDataOnly View Post
    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.

  12. #12

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2020
    Posts
    1,421

    Re: Will the child class inherit the parent class's Friend methods?

    Quote Originally Posted by jmcilhinney View Post
    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.

  13. #13
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: Will the child class inherit the parent class's Friend methods?

    Quote Originally Posted by SearchingDataOnly View Post
    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.

  14. #14

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2020
    Posts
    1,421

    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
  •  



Click Here to Expand Forum to Full Width