dr85
dr85

Reputation: 743

Nesting Classes

If I nest a class inside another, does the nested class automatically become a subclass of the parent class? Would the nested class have access to all the public methods, vairables of the parent class?

Oh sorry - the programming language i am referring mainly to is Java!

Upvotes: 1

Views: 185

Answers (3)

Ted Hopp
Ted Hopp

Reputation: 234795

What language is this in reference to? For most languages, an inner class and a subclass are completely different things. An inner class is a "member" of the outer class, just as fields and methods are members of the outer class. And just like any (static) method of the outer class can access all (static) class members, usually inner classes have access, as members of the outer class, to all other members of the outer class. (This is definitely not true of all OO languages, however.)

Upvotes: 1

Gursel Koca
Gursel Koca

Reputation: 21290

Nested class is not subclass of parent class. If nested class is not static , it can access all methods and variables of the parent class. If nested class is static, then it can access only static fields and methods.

Upvotes: 2

Mark Peters
Mark Peters

Reputation: 81064

If I nest a class inside another, does the nested class automatically become a subclass of the parent class?

No

Would the nested class have access to all the public methods, vairables of the parent class?

If the inner class is not static, yes. It also has access to any other members, static or not, and public or not. If the nested class is declared static then there is no enclosing instance, so it would only have access to the static members of the outer ("parent") class.

Upvotes: 5

Related Questions