Solved

Inner Classes

Posted on 1998-11-20
6
234 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
Comment Utility
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
Comment Utility
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_
Comment Utility
>> 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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

Author Comment

by:seena
Comment Utility
Hey_hey

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

Seena
0
 

Author Comment

by:seena
Comment Utility
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_
Comment Utility

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
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 scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
The viewer will learn how to implement Singleton Design Pattern in Java.

772 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

12 Experts available now in Live!

Get 1:1 Help Now