Solved

Inner Classes

Posted on 1998-11-20
6
240 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
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…

910 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now