• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Inner classes

 
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys..
Please take a look at this example:

class base { void method_1() {} }

class sub extends base {
void method_1() {} // overrides the base class method.

class inner_class {..}
}

Is it true or not that I cannot invoke method_1() in "base" class from "inner" class ? Because I have read that inner classes cannot access super class methods if the outer class overrides it. Is it true?

Thanks guys..
Regards,
Maduranga.
 
author
Posts: 119
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're right: you can't invoke method_1() in "base" class from "inner".

An instance of an inner class has a reference to the instance of the containing class that created the inner class instance. At the moment the inner class instance is created, a copy of the "this" reference of the creating containg class instance is stored in the newborn inner class instance. (Though you and I can't use that reference explicitly inside the inner class, and sometimes that's annoying.)

The creating containing class instance has a "super" as well as a "this". But there's no way for the inner class to access that "super". So in your example, there's no way for the inner class to say that it wants method_1 in the base class and not in the subclass.

Within the inner class, "super" works with the superclass of the inner class, not the superclass of the containing class. So it does no good to try to invoke super.method_1() ... the compiler will think "super" means the superclass of the inner class, which is Object. Since Object doesn't have a method_1, compilation will fail.

By the way, I highly recommend having all class names begin with upper-case letters, and avoiding underscores in most identifier names. It's not inherently better, but it's standard. The Programmer Exam doesn't test for nowledge of commonly-accepted coding conventions, but the Developer Exam cares about readability, so it's a good habit to get into. So for example, instead of class big_river, the conventional name would be BigRiver.
 
Maduranga Liyanage
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi..
Thank you very much for the reply Philip.

But, When I used the following code inside "inner_class";

sub.super.method_1(); // This invoked the "base" class method_1() !!!
sub.this.method_1(); // This invoked the "sub" class method_1() !!!

If method_1()is not overriden in "sub" class;
sub.this.method_1(); // invokes the "base" class method_1().

Isn't this contradictory???

Thanks you.
Regards,
Maduranga.
 
Ranch Hand
Posts: 1026
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is because if you don't override "method_1()" in "sub" class then
the same will be inherited by "sub" class as you extend "base" class.
Hence

sub.this.method_1();

will invoke "base" class version of "method_1()"
 
Maduranga Liyanage
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi..

If I have overridden in "sub" class, then:

sub.super.method_1(); // This invokes the "base" class method.

I'm confused about this guys. What am I missing here?

Thank you.
 
Vishnu Prakash
Ranch Hand
Posts: 1026
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"super" always refers to super class version. Since you extend base class in your sub class "sub.super" always refers to base class and hence base class version of "method_1()" is invoked.
 
F is for finger. Can you stick your finger in your nose? Doesn't that feel nice? Now try this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic