Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Private nested classes

I want to created a nested class that can only be visible to and instantiated from the parent class.
But I also want to be able to use an instance of the nested class through a public variable of the parent class.
I tried making the nested class private, or making the nested class' constructor private, but it won't compile.
Is it possible to do this in .NET?

This compiles and works, but the nested class can be used by anybody:

 Public Class OuterClass
        Public X As Integer = 123
        Public NestedClassInstance As New NestedClass(Me)

        Public Class NestedClass
            Private Parent As OuterClass

            Public Sub New(ByVal _Parent As OuterClass)
                Parent = _Parent
            End Sub

            Public Sub GetParentX()
                Debug.WriteLine("X = " & Parent.X.ToString)
            End Sub
        End Class
    End Class

    Sub Main()
        Dim OuterClassInstance As New OuterClass
        OuterClassInstance.NestedClassInstance.GetParentX()
    End Sub
like image 557
mcu Avatar asked Jun 07 '12 18:06

mcu


People also ask

How do I access private nested classes?

Accessing the Private Members Write an inner class in it, return the private members from a method within the inner class, say, getValue(), and finally from another class (from which you want to access the private members) call the getValue() method of the inner class.

Can I override the private class in nested class?

Core Java bootcamp program with Hands on practiceNo, we cannot override private or static methods in Java. Private methods in Java are not visible to any other class which limits their scope to the class in which they are declared.

Can nested classes access private members C#?

A nested type has access to all of the members that are accessible to its containing type. It can access private and protected members of the containing type, including any inherited protected members.


2 Answers

Typically the Private nested class implements an interface or inherits another class, and the interface/base class is exposed. This is useful when you want to hide the implementation to the parent class. Something like this:

Module EntryPoint
    Sub Main()
        Dim OuterClassInstance As New OuterClass
        OuterClassInstance.NestedClassInstance.GetParentX()
    End Sub
End Module

Public Class OuterClass
    Public X As Integer = 123
    Public NestedClassInstance As ISomeImplementation = New NestedClass(Me)

    Private Class NestedClass
        Implements ISomeImplementation
        Private Parent As OuterClass

        Public Sub New(ByVal _Parent As OuterClass)
            Parent = _Parent
        End Sub

        Public Sub GetParentX() Implements ISomeImplementation.GetParentX
            Debug.WriteLine("X = " & Parent.X.ToString)
        End Sub
    End Class
End Class

Public Interface ISomeImplementation
    Sub GetParentX()
End Interface
like image 75
vcsjones Avatar answered Sep 21 '22 02:09

vcsjones


You can't have a private nested class that is also exposed through a public interface.

It is an either or proposition - either the nested class is private, or it is not.

An option is to declare an interface with the contract you want the nested class to implement (with some refactoring tools you can extract an interface from a class) and expose the interface, while keeping the nested class private.

like image 40
Oded Avatar answered Sep 21 '22 02:09

Oded