Solved

Inner Classes

Posted on 1998-11-20
6
266 Views
Last Modified: 2010-03-30
Hello,

  Consider the following code please go thro it.  I have few questions at the end.

class Egg2 {
  protected class Yolk {
    public Yolk() {
      System.out.println("Egg2.Yolk()");
    }
    public void f() {
      System.out.println("Egg2.Yolk.f()");
    }
  }
  private Yolk y = new Yolk();
  public Egg2() {
    System.out.println("New Egg2()");
  }
  public void insertYolk(Yolk yy) { y = yy; }
  public void g() { y.f(); }
}

public class BigEgg2 extends Egg2 {
  public class Yolk extends Egg2.Yolk {
    public Yolk() {
      System.out.println("BigEgg2.Yolk()");
    }
    public void f() {
      System.out.println("BigEgg2.Yolk.f()");
    }
  }
  public BigEgg2() { insertYolk(new Yolk()); }
  public static void main(String[] args) {
    Egg2 e2 = new BigEgg2();
    e2.g();
  }
}

The output for this is
-----------------------
Egg2.Yolk()
New Egg2()
Egg2.Yolk()
BigEgg2.Yolk()
BigEgg2.Yolk.f()

I understood from the above is that in the following lines

public class BigEgg2 extends Egg2 {
  public class Yolk extends Egg2.Yolk {

since BigEgg2 is extending Egg2 the constructor for Egg2 will be first executed, and also Egg2 is a out class the inner class inheritance willwork out first i.e. the Constructor for Egg.Yolk will be exeuted first so the statement

Egg2.Yolk is executed am i right in this??

Second thing is that after the inner class the outer class constructor will be executed i.e. the Egg2 constructor will be exeuted so the statement

New Egg2 is executed. am i right upto this??

My questions are:
-----------------

1.  How come the next Egg2.Yolk output arrives and what exactly will the insetYolk method both in the Egg2 and in the BigEgg2 will do.  I am confused in that working of that method and in the main also it has bee given as
 
  Egg e2 = new BigEgg2() what does this mean actually??

when i say Egg e2 = new Egg2() also i am getting the same output.  

2.  And also the constructor for Yolk class is inherited as method in the BigEgg2 can is this possible or my interception of that Yolk is incorrect??

Please explain me in detail abt this one.


Thanks in advance,
Seena
0
Comment
Question by:seena
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 16

Accepted Solution

by:
heyhey_ earned 20 total points
ID: 1227966
sorry, but it seems that i don't understand you problems ...

>> ... the constructor for Egg2 will be first executed, and also Egg2 is a out class the inner class inheritance willwork out first i.e. the Constructor for Egg.Yolk will be exeuted first so the statement

you don't have any inner class inheritance ... constructors are called only when you explicitly create new object !!!
so first try this
  public static void main(String[] args) {
        Egg2 e2 = new Egg2();
      }

and look at the dump
- first  new Egg2.Yolk()  is created (because you have initialization of a private member in Egg2 :
  private Yolk y = new Yolk();
- second new Egg2() is created

so let's look at your example
you are creating BigEgg2. BigEgg2 constructor implicitly calls the Egg2 constructor so you get
- new Egg2.Yolk();
- new Egg2();

than you continue with BigEgg2 constructor
- new BigEgg2
you execute
 insertYolk(new Yolk()); so a BigEgg2.Yolk constructor is called, but since BigEgg2.Yolk  extends Egg2.Yolk , Egg2.Yolk's constructor is implicitly called - so you see
- new Egg2.Yolk();
- new BigEgg2.Yolk();

and at last you call  
        e2.g();
which of course will print
- BigEgg2.Yolk.f();

so everything is normal
Hint:
you can use this statement  
   Thread.currentThread().dumpStack();
to see who has called your routine (so you will see who is calling the Egg2.Yolk constructor twice ...

so check the output of this code
---
class Egg2 {
        protected class Yolk {
          public Yolk() {
             System.out.println("new Egg2.Yolk()");
             Thread.currentThread().dumpStack();
          }
        public void f() {
          System.out.println("Egg2.Yolk.f()");
        }
      }
      private Yolk y = new Yolk();
      public Egg2() {
        System.out.println("New Egg2()");
      }
      public void insertYolk(Yolk yy) { y = yy; }
      public void g() { y.f(); }
    }

    public class BigEgg2 extends Egg2 {
      public class Yolk extends Egg2.Yolk {
        public Yolk() {
          System.out.println("new BigEgg2.Yolk()");
          Thread.currentThread().dumpStack();

        }
        public void f() {
          System.out.println("BigEgg2.Yolk.f()");
        }
      }
      public BigEgg2() {
          System.out.println("new BigEgg2()");
        insertYolk(new Yolk()); }
      public static void main(String[] args) {
        Egg2 e2 = new BigEgg2();
        e2.g();
      }
    }
---

More info needed ?
  heyhey


0
 

Author Comment

by:seena
ID: 1227967
Hi Hey-Hey,

   It was an excellent piece of work thank u very much for ur help.  But just explain how it is impliciting calling the base class does inheritance is going to do something with this one??  Please hey_hey.

Thanks Again,
Seena
0
 
LVL 16

Expert Comment

by:heyhey_
ID: 1227968
>> But just explain how it is impliciting calling
>> the base class does inheritance is going to do something with this one??

sorry, but i don't understand your question.
Every constructor calls (by default) the empty constructor of the base class (that is implicitly) ...
or ??? what's the problem ....

0
Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

 

Author Comment

by:seena
ID: 1227969
Hey_hey

   Sorry i think i have confused a bit.  Now i understood thanks

Seena
0
 

Author Comment

by:seena
ID: 1227970
Hey_hey

Just explain me the main part alone Egg2 e2 = new BigEgg2() what this will exact do my interception is that First the Egg2 class (i.e. the default constructors and the private intialization calls will be made)etc., and then the new BiggEgg2() will be called.  Am i right ?? which class handle does the object e2 will have??  Either Egg2 or BigEgg2().

Thanks
Seena
       

Seena
0
 
LVL 16

Expert Comment

by:heyhey_
ID: 1227971

Egg2 e2 = new BigEgg2()

creates a new object from class BigEgg2() (this means calling constructor of BiggEgg2 that calls the constructor of Egg2 ...) and than ... to the variable e2 so e2 is just a variable of type Egg2 that points to object of class BigEgg2

you can add these lines
Object o = (Object)e2;
BigEgg2 big = (BigEgg2)o;
and you'll have one object and three variables (e2, o and big of types Egg2, Object and BigEgg2 that point to this object)

and if you execute this line
System.out.println(o.getClass().getName());
you'll see that the object that variable o points to is sitll of class BigEgg2

more problems ?
  heyhey

0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

695 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question