• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Inheritance Doubt??

 
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Guys,

The code below is from Dan Chisholm,


The o/p is given as B,SuperB,A,SuperA. I want to know what it means when we say new A().new B().m1();??

Please help me guys.
 
Ranch Hand
Posts: 381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jothi Shankar wrotes
what it means when we say new A().new B().m1();?



It means creating an instance of regular inner class and invoing the method m1().
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are doing a great job mentioning where the questions come from Jothi

B is an inner class (non-static nested class) of A so it needs an instance of A to exist before an instance of B can exist. So that's what the new A() is for - to get the outer instance of A. Now we can use our instance of A to create an instance of B. The Java syntax for that is new A().new B(). So now we have our instance of B together with its containing instance of A. We can now call B's methods on this instance of B. So new A().new B().m1() calls method m1 on an instance of inner class B contained in an instance of A.

I feel rather dizzy now.
 
Joe San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Guys,

To be frank and honest, I know a good deal of information with respect to inner classes and nested classes. But when I was answering the question from Dan's website, I didn't notice that class B is inside class A. I failed to notice it and I was wondering how we can do new A().new B() on normal classes other than innser classes. In some of the mock exams that I took from whizlabs, I have comitted similiar mistakes where I forget to see the code throughly. So I leant that I should see the question with my both eyes wide open.

Thanks for the help ranchers.
 
Roses are red, violets are blue. Some poems rhyme and some don't. And some poems are a tiny ad.
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic