?
Solved

Inner Classes

Posted on 1998-11-20
6
Medium Priority
?
272 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 80 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Suggested Courses
Course of the Month7 days, 23 hours left to enroll

765 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