Solved

Inner Classes

Posted on 1998-11-20
6
252 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
  • 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

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: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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

Suggested Solutions

Title # Comments Views Activity
custom annotations 9 42
Maven Project: Hibernate Dependencies Conflict 10 38
Way to decrease size of apk file 9 86
Desingning Refactoring existing code 2 26
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

856 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