what is inner class in c++ or java ? [Resolved]

Posted by Yugandhar under C# on 8/23/2012 | Points: 10 | Views : 1076 | Status : [Member] | Replies : 6
what is inner class in c++ or java ?
and what is it's purpose




Responses

Posted by: Muhsinathk on: 9/17/2012 [Member] Bronze | Points: 50

Up
0
Down

Resolved
"An InnerClass is a NestedClass that is not explicitly or implicitly declared static." -- Java Language Specification, Second Edition [1]

An inner class, as used in JavaLanguage, is a class nested within another class:

class C {
class D {
}
}
Unlike a C++ nested class, an instance of a Java inner class contains an implicit, hidden reference to an instance of the outer class. You can actually obtain this reference from within the inner class, with syntax "OuterClassName.this", though this is rarely necessary:

C.this


An instance of an inner class can only live attached to an instance of the outer class. Within instance methods of the outer class, you can simply use "new D()" to create it and it will implicitly take that current outer class instance as the enclosing instance. However, if you want to create an instance of the inner class from outside the outer class, or from within a static method, you need to explicitly give the enclosing instance reference:

C c = new C();
C.D d = c.new D();

Because the inner class is considered part of the implementation of the outer class, it has access to all of the outer class's instance variables and methods.

When name conflicts arise (i.e., the inner class declares a variable with the same name as a variable in the outer class, or when using the this keyword), prefix the identifier with the name of the outer class for static variables, and prefix it with the name of the outer class plus .this. for non-static variables:

class A {
private boolean fuzzy;
class B {
private boolean fuzzy;


//These two methods act on this object's variable.
public boolean isFuzzy() {
return fuzzy;
}
public void makeFuzzy() {
fuzzy=true;
}


//These two methods act on whatever instance of A to which this
//object of type B was attached.
public boolean isAFuzzy() {
return A.this.fuzzy;
}
public void makeAFuzzy() {
A.this.fuzzy=true;
getOuter().fuzzy=true; //equivalent; see next method
}


//This method returns the object to which "A." refers, above.
public A getOuter() {
return A.this;
}
}
}

Types of Inner Class

1.Local Inner Class
2.Static Inner Classes

Yugandhar, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Deepraj on: 8/24/2012 [Member] Starter | Points: 25

Up
0
Down
The class as a member of another class is called inner class.
Eg:In java

public class Myclass
{
private int count; //member(property)
public void displayCount(); //member(property)
public class MyInnerClass
{
int count2;
public void display();
}
}

In above sample code MyInnerClass is inner Class which is in the MyClass.

These are used in situations where we need a member as a class and they serve as structures in java and we can prevent the access of inner class to the objects of the class holding inner classes,which provides security.


Yugandhar, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Hariinakoti on: 9/17/2012 [Member] Starter | Points: 25

Up
0
Down
Good Work Muhsinathk.Where u learned Ms.U giving reply to any question.

Thanks & Regards
Hari

Yugandhar, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Muhsinathk on: 9/17/2012 [Member] Bronze | Points: 25

Up
0
Down
Hi Yugandhar,
Please mark as answer if it helpful to u..That helps others who search the same..

Yugandhar, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Muhsinathk on: 9/17/2012 [Member] Bronze | Points: 25

Up
0
Down
Thanks Harii.

Yugandhar, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Hariinakoti on: 9/17/2012 [Member] Starter | Points: 25

Up
0
Down
Welcome MS.

Thanks & Regards
Hari

Yugandhar, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response