aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Inner Classes inheritance Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Inner Classes inheritance" Watch "Inner Classes inheritance" New topic
Author

Inner Classes inheritance

Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9389
    
    2

Hi all,

Can a non static inner class be a sublass of its outerclass?

Consider the following code as,

class Outer{
private int a = 10;
class Inner extends Outer{
...........
...........
...........
}
}

Will the above be logical? Please anyone give me explanation in terms of real time programming.

Regards,
Jothi Shankar Kumar. S


SCJP 1.4, SCWCD 1.4 - Hints for you, Certified Scrum Master
Did a rm -R / to find out that I lost my entire Linux installation!
Ali Gohar
Ranch Hand

Joined: Mar 18, 2004
Posts: 572
Yes i think its legal
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
What happens when you write some java code, compile, and run it?
That's the way to find the answer to such questions if you do not want to read the Java Language Specification.


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9389
    
    2

Hi Barry,

Here is the code that I had doubt,

class outer {
private int a = 10;
class inner {
public void myMethod(){
System.out.println(this.a); //throws error here but works good when using the special this operator like outer.this.a
}
}
}

Why the above throws error at the line mentioned but when said
class inner extends outer this.a works fine. Reason?

Regards,
Jothi Shankar Kumar. S
wise owen
Ranch Hand

Joined: Feb 02, 2006
Posts: 2023
The name this.x refers to a variable named x in this instance of the object. It is pretty obvious that if you refer to this in an inner class, it refers to fields or variables defined in the inner class itself.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Why the above throws error at the line mentioned but when said
class inner extends outer this.a works fine. Reason?


Simply because it cannot inherit x from its superclass because it is private. If you make x accessible using this.x (or just x) in the println will work.

Compile and run this code:


Can you now see what is happening?

Make x private again and compile. Then comment out the line that does not compile and compile and run it again. Can you see the difference?
[ October 06, 2006: Message edited by: Barry Gaunt ]
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9389
    
    2

Hi Barry,

Thanks for the reply and you gave a reasonable explanation. Thanks for it. I understood.

Regards,
Jothi Shankar Kumar. S
 
Consider Paul's rocket mass heater.
 
subject: Inner Classes inheritance