473,739 Members | 7,912 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 2076
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
10954
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
4339
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
4549
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
1861
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
3095
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
2430
by: Martijn Mulder | last post by:
A construction like this: class Outer { class Inner:Outer { } } compiles without problem but does it introduce infinity?
1
1616
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
1994
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
4688
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
8969
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8792
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9479
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...
0
9337
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9209
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
8215
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...
1
6754
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4570
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
4826
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.