Questions tagged [nested-class]

In Object Oriented programming, a class defined inside another class. Also called "inner class".

In there are times when a class A is used only as a component of another class B and should not be instanced outside B. In those cases proper encapsulation suggest defining that class as a nested or inner class.

Several languages support this feature (C++, Java, .Net languages, D, python, for example) although with minor differences in where nested classes can be defined, how can they be used; and some languages support more than one variety of nested class.

574 questions
249
votes
8 answers

Why Would I Ever Need to Use C# Nested Classes

I'm trying to understand about nested classes in C#. I understand that a nested class is a class that is defined within another class, what I don't get is why I would ever need to do this.
Dan Norton
85
votes
5 answers

Are inner classes in C++ automatically friends?

If I define an inner class in C++, is it automatically a friend of the class that contains it? For example, is this legal: class Outer { public: class Inner { public: void mutateOuter(Outer& o); }; private: int…
templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
75
votes
7 answers

How to restrict access to nested class member to enclosing class?

Is it possible to specify that members of a nested class can be accessed by the enclosing class, but not other classes ? Here's an illustration of the problem (of course my actual code is a bit more complex...) : public class Journal { public…
Thomas Levesque
  • 286,951
  • 70
  • 623
  • 758
51
votes
6 answers

Eclipse warning about synthetic accessor for private static nested classes in Java?

My coworker suggested making several of the Eclipse code-formatting and warning settings to be more rigorous. The majority of these changes make sense, but I get this one weird warning in Java. Here's some test code to reproduce the…
Jason S
  • 184,598
  • 164
  • 608
  • 970
48
votes
6 answers

Nested/Inner class in external file

I have a class MyClass and an inner class MyNestedClass like this: public class MyClass { ... public class MyNestedClass { ... } } Both classes are very long. Because of that i'd like to seperate them in two different files, without…
Alp
  • 29,274
  • 27
  • 120
  • 198
39
votes
4 answers

Using nested enum types in Java

I have a data structure in mind that involves nested enums, such that I could do something like the following: Drink.COFFEE.getGroupName(); Drink.COFFEE.COLUMBIAN.getLabel(); And if there were method declarations: someMethod(Drink…
Chris
  • 515
  • 1
  • 4
  • 5
39
votes
3 answers

Creating an instance of a nested class in XAML

in a XAML file (a WPF UserControl), is there a way to reference an inner class "B" defined in another class "A" ? public class A { public class B { } } Something like : This syntax does not work because "B" is…
Pragmateek
  • 13,174
  • 9
  • 74
  • 108
35
votes
6 answers

C++ nested class/forward declaration issue

Is it possible to forward-declare a nested class, then use it as the type for a concrete (not pointer to/reference to) data member of the outer class? I.E. class Outer; class Outer::MaybeThisWay // Error: Outer is undefined { }; class Outer { …
uj2
  • 1,391
  • 2
  • 15
  • 15
32
votes
6 answers

Why can't a class extend its own nested class in C#?

For example: public class A : A.B { public class B { } } Which generates this error from the compiler: Circular base class dependency involving 'A' and 'A.B' I always figured a nested class behaved just like a regular class except with…
Tinister
  • 11,097
  • 6
  • 35
  • 36
28
votes
6 answers

Inner class in interface vs in class

What is the difference between these two innerclass declarations? Also comment on advantages/disadvantages? case A: class within a class. public class Levels { static public class Items { public String value; public String path; …
Blessed Geek
  • 21,058
  • 23
  • 106
  • 176
27
votes
5 answers

Can a nested C++ class inherit its enclosing class?

I’m trying to do the following: class Animal { class Bear : public Animal { // … }; class Giraffe : public Animal { // … }; }; … but my compiler appears to choke on this. Is this legal C++, and if not, is…
Tony the Pony
  • 40,327
  • 71
  • 187
  • 281
25
votes
13 answers

What are reasons why one would want to use nested classes?

In this stackoverflow answer a commenter mentioned that "private nested classes" can be quite useful so I was reading about them in articles such as this one which tend to explain how nested classes function technically, but not why you would use…
Edward Tanguay
  • 189,012
  • 314
  • 712
  • 1,047
25
votes
5 answers

Nested Class member function can't access function of enclosing class. Why?

Please see the example code below: class A { private: class B { public: foobar(); }; public: foo(); bar(); }; Within class A & B implementation: A::foo() { //do something } A::bar() { //some code foo(); …
Rahul
  • 353
  • 1
  • 3
  • 5
25
votes
6 answers

Can a Static Nested Class be Instantiated Multiple Times?

Given what I know of every other type of static feature of programming––I would think the answer is 'no'. However, seeing statements like OuterClass.StaticNestedClass nestedObject = new OuterClass.StaticNestedClass(); makes me wonder.
stormin986
  • 7,672
  • 15
  • 42
  • 54
25
votes
3 answers

Private nested Java class in UML diagram

I have a question regarding UML. I have a class which simply contains an inner class with the private access modifier - cannot be accessed from anywhere else... Normally in order to present an inner class relation I can use a (+) relation like here…
radekEm
  • 4,617
  • 6
  • 32
  • 45
1
2 3
38 39