473,698 Members | 2,630 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Intricate Inner Class Question

4 New Member
Hello. I have an obscure question for y'all. Consider an inner class:

Expand|Select|Wrap|Line Numbers
  1. class Outer1 {
  2.     class Inner1 {
  3.     }
  4. }
  5.  
Now in a different inner class I extend the first one. Does anyone know how to test if <i is an inner class of o> in "aMethod", below?

Expand|Select|Wrap|Line Numbers
  1. class Outer2 {
  2.     Inner1 i;
  3.  
  4.     class Inner2 extends Outer1.Inner1{
  5.         Inner2(Outer1 o) {
  6.             o.super();
  7.         }
  8.     }
  9.  
  10.     public void aMethod(Outer1 o) {
  11.         if (<i is an inner class of o>) {
  12.             ....
  13.         }
  14.     }
  15. }
  16.  
Oct 9 '06 #1
6 2074
r035198x
13,262 MVP
Hello. I have an obscure question for y'all. Consider an inner class:

Expand|Select|Wrap|Line Numbers
  1. class Outer1 {
  2.     class Inner1 {
  3.     }
  4. }
  5.  
Now in a different inner class I extend the first one. Does anyone know how to test if <i is an inner class of o> in "aMethod", below?

Expand|Select|Wrap|Line Numbers
  1. class Outer2 {
  2.     Inner1 i;
  3.  
  4.     class Inner2 extends Outer1.Inner1{
  5.         Inner2(Outer1 o) {
  6.             o.super();
  7.         }
  8.     }
  9.  
  10.     public void aMethod(Outer1 o) {
  11.         if (<i is an inner class of o>) {
  12.             ....
  13.         }
  14.     }
  15. }
  16.  
Your code is not correct. Inner1 i; will give a compilation error. You can only declare objects of type Inner1 by getting in the context of Inner1 first which is Outer1. As in Outer1.Inner1 i;
Now your question does not make much sense since inner classes are members of classes not of objects. A class is an inner class of another class not of an object. If you change your question to how to test if a class is an inner of a given class, then such a question would be redundant since the syntax would require something like
if Outer.Inner isInnerOf Outer and you can see that it is both redundant and impractical to have such a comparison.
Oct 10 '06 #2
Xemnosyst
4 New Member
Here is a full working example which I just compiled:

Expand|Select|Wrap|Line Numbers
  1. public class Outter2 {
  2.     public static void main(String[] args) {
  3.         Outter1 o1 = new Outter1();
  4.         Outter2 o2 = new Outter2(o1);
  5.         o2.aMethod(new Outter1());
  6.     }
  7.  
  8.     Inner2 i2;
  9.  
  10.     Outter2(Outter1 innerParent) {
  11.         i2 = new Inner2(innerParent);
  12.     }
  13.  
  14.     void aMethod(Outter1 o) {
  15.         System.out.println("is o the parent object of i2?");
  16.     }
  17.  
  18.     class Inner2 extends Outter1.Inner1 {
  19.         Inner2(Outter1 parent) {
  20.             parent.super();
  21.             System.out.println("success!");
  22.         }
  23.     }
  24. }
  25.  
  26. class Outter1 {
  27.     class Inner1 {
  28.         Inner1() {
  29.             System.out.println("success?");
  30.         }
  31.     }
  32. }
  33.  
The output is:
success?
success!
is o the parent object of i2?
Oct 11 '06 #3
r035198x
13,262 MVP
Here is a full working example which I just compiled:

Expand|Select|Wrap|Line Numbers
  1. public class Outter2 {
  2.     public static void main(String[] args) {
  3.         Outter1 o1 = new Outter1();
  4.         Outter2 o2 = new Outter2(o1);
  5.         o2.aMethod(new Outter1());
  6.     }
  7.  
  8.     Inner2 i2;
  9.  
  10.     Outter2(Outter1 innerParent) {
  11.         i2 = new Inner2(innerParent);
  12.     }
  13.  
  14.     void aMethod(Outter1 o) {
  15.         System.out.println("is o the parent object of i2?");
  16.     }
  17.  
  18.     class Inner2 extends Outter1.Inner1 {
  19.         Inner2(Outter1 parent) {
  20.             parent.super();
  21.             System.out.println("success!");
  22.         }
  23.     }
  24. }
  25.  
  26. class Outter1 {
  27.     class Inner1 {
  28.         Inner1() {
  29.             System.out.println("success?");
  30.         }
  31.     }
  32. }
  33.  
The output is:
success?
success!
is o the parent object of i2?
What is this supposed to prove? The code now compiles because Inner2 i2; is done in the correct context (inside the class where it has been defined) and the reference to Inner1 inside Outter2 has been done through Outter1 which is where it has been declared.
Oct 11 '06 #4
Xemnosyst
4 New Member
What is this supposed to prove? The code now compiles because Inner2 i2; is done in the correct context (inside the class where it has been defined) and the reference to Inner1 inside Outter2 has been done through Outter1 which is where it has been declared.
Oh!! I see now. Yes, you are right, my original code was incorrect. That member variable should have been "Inner2 i2", like in the version that compiles. Sorry about that. But the questions remains: how can I test whether "o" is the outter object for "i2" in "aMethod"?
Oct 11 '06 #5
r035198x
13,262 MVP
Oh!! I see now. Yes, you are right, my original code was incorrect. That member variable should have been "Inner2 i2", like in the version that compiles. Sorry about that. But the questions remains: how can I test whether "o" is the outter object for "i2" in "aMethod"?
Like I said this not neccessary to test and there probably is not a way to do it without messing with lots of reflection. Inner classes only allow grouping of classes together and splitting of a class into smaller more manageable classes. Trying to test for being an inner class is like trying to test if two classes are in the same package
Oct 12 '06 #6
Xemnosyst
4 New Member
Like I said this not neccessary to test and there probably is not a way to do it without messing with lots of reflection. Inner classes only allow grouping of classes together and splitting of a class into smaller more manageable classes. Trying to test for being an inner class is like trying to test if two classes are in the same package
Is anyone sure whether I have to use reflection to accomplish this test? I am in a situation to need that information in a project I'm working on.
Oct 13 '06 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

1
10952
by: Murat Tasan | last post by:
hi, i am having a small problem with constructing an inner class. i am using an inner class (and not a static nested class) because the methods of the inner class need access to the enclosing object's members. now, i have an enclosing class, which has a static factory method (loads an instance of the enclosing class from a serialized file, and returns it). during the loading, some initialization takes place. one of the initialization...
5
4335
by: devu | last post by:
Currently I'm working on a class that performs a batch process as per scheduling for all employees in a company. Being a batch process load is obviously a huge factor. The process first needs to calculate the eligibility of each employee before proceeding to the next stage. I need to cache some of the employee information if an employee is found eligible for later use in the next stage. One option could be a javabean style value object...
10
4544
by: Paul Morrow | last post by:
I'm hoping that someone can explain why I get the following exception. When I execute the code... ###################################### class Parent(object): class Foo(object): baz = 'hello from Parent.Foo' class Child(Parent): #Foo.baz = 'hello from Child.Foo'
9
1858
by: MariusI | last post by:
Consider the following class layout public class Order { public ProductOrder AddProductOrder(/* variables required to create a product order */) { /* Check if the product order can be added to the order */ }
12
3089
by: Mark E. Fenner | last post by:
Hello all, I have a code where my inner loop looks like: allNew = for params in cases: newObj = copy(initialObject) newObj.modify(params) allNew.append(newObj) return allNew
5
2426
by: Martijn Mulder | last post by:
A construction like this: class Outer { class Inner:Outer { } } compiles without problem but does it introduce infinity?
1
1611
by: mrstephengross | last post by:
I'm making progress on mixing templates with friends (sounds like a drinking game, huh?). Anyway, here's the situation. I've got an "Outer" class with a private "Inner" class (sub-class, technically). I want to declaring an unrelated "Thing" class to be a friend of the inner class. However, since the inner class is private, the compiler complains. Here's what the code looks like: ====================================================...
2
1991
by: andrew_nuss | last post by:
Hi, I have an Array<Ttemplate that specializes on T's in my program that inherit from a base class that I've defined called CtorDtorLess. In my array template, I check if boost::is_convertible<T*, CtorDtorLess*> returns true, and then don't use placement new to construct. I've checked in the debugger and everything works with T's that are public classes. However, I also have some "inner classes" that are declared in the private...
9
4686
by: Matthias Buelow | last post by:
Hi folks, I've got something like: class Outer { int f(); friend class Inner; class Inner { int g() {
0
9170
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8901
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8871
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7739
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5862
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4371
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4622
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3052
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2007
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.