Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

inner class - subclass????

 
Nasir Khan
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

This statement is from jxam:
"An inner class can actually be a subclass of the outer class"
Please tell me if it's correct or not.
 
Junaid Bhatra
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes an inner class can be a subclass of the outer class. You can write a simple test program for yourself, compile it and see the results.
 
Nasir Khan
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Junaid
I tried with this code
__________________________
class out {
out(){System.out.println("out constructor");}
void method(){
class inner extends out{
inner(){System.out.println("I'm in inner constructor"); }
}
new inner(); }
}

class tes extends out{
public static void main(String [] arg){
new tes().method();}
}
_________________________________
As expected tes().method(); invokes out's constructor twice and inner's costructor once.
Does anyone know any advantage of extending the outer class to inner class.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic