please send code example of inner classes?

Answer Posted / amit singh

here is the code of inner classes in which you will get all
inner classes in a one code


//interface for anonymous class

interface Aishwarya
{
public static final int lm = 10;
void intmeth();
}



//class for cretaing anonymous class
class Amitabh
{

void isit()
{
System.out.println("in superclass Amitabh");
}

}



//outer class for the regular inner class
class Outer
{
private static int i;
static int j;
Outer()
{

}


Outer(int i,int j)
{

this.i = i;
this.j = j;

}



public int add()
{

return i+j;

}


private int substract()
{
return (j-i);
}



public static void show2()
{
System.out.println("inner show in static method");
Outer.Inner j = new Outer().new Inner();
j.show();
}


public void show1()
{
Inner f = new Inner();
f.show();
}


//just to define how argument define anonymous class work
public void argument(Aishwarya a1)
{
Aishwarya a2 = a1;
System.out.println("value of interface constant " + a2.lm);
a2.intmeth();
}



//simple regular inner class
class Inner
{

public void show()
{
System.out.println("show add in inner" + add());
System.out.println("show substarct in inner" + substract
());
System.out.println("value of i of outer" + i);
System.out.println("value of j of outer" + j);
System.out.println("value of i of outer through this" +
Outer.this.i);
System.out.println("value of j of oter through this" +
Outer.this.j);

}
}

//end of the inner class




//static nested class is not real inner class
static class Bye
{
void statmeth()
{
System.out.println("hi im static only acess the statis var
method of outer class");
System.out.println("don't need for me to create the outer
instance");
System.out.println("because i'm Outerclass type");
System.out.println("value of i from outer which is
static :" + i);

//don't do it

//System.out.println("value " + add());
}
}//end of static class





//anonymous class
Amitabh a = new Amitabh(){
public void isit()
{
System.out.println("isit
abs bay borned in this");

}
public void isntpoosible()
{
System.out.println("isnot
in superclass");
}


};




public void did()
{
a.isit();
//a.isntpoosible();
}


//main method of outer
public static void main(String []args)
{

Outer o1 = new Outer(4,5);
o1.did();

//here is how to define anonymous class in argument

o1.argument(new Aishwarya(){
public void intmeth()
{
System.out.println("hi
this is interface method implement");
}


});

System.out.println("in main " + o1.add());
System.out.println("substract in main" + o1.substract());
Outer.Inner i1 = new Outer().new Inner();
System.out.println("show in main");
i1.show();



// create the object of amit
Amit a112 = new Amit();
a112.hi();
a112.dosome();

//create the object of static class
Bye bbb = new Bye();
bbb.statmeth();
}
}
// end of outer






//just for manupulating the code
class Amit
{

public int j = 45;
public static int l = 10;
private String s1 = "amit";
public String s2 = "sumit";



public static void hi()
{

System.out.println("hi this is amit");
Outer.Inner j1 = new Outer().new Inner();

j1.show();

Outer o22 = new Outer();

System.out.println("outer add " + o22.add());
System.out.println("is there any requirement of method
local class so see it");
System.out.println("in static context you only able to
acess the static var from the enclose class");
System.out.println("don't get then do some differ");




//local method class in static method

class LocalInner1
{


public void seeall1()
{
System.out.println("print :" + l);
//System.out.println("string concat : " + s1+s2);
System.out.println("string concat :" + new Amit().s1 +
new Amit().s2);
//System.out.println("strig concat" + this.s1 + this.s2);


}


}
LocalInner1 i1 = new LocalInner1();
i1.seeall1();

}


public void dosome()
{

//String z = "i'm in mood to be access if you can";


final String z = "yes finallly access";
System.out.println("you only apply on mlc just abstract and
final if not get so do diifer ");
System.out.println("in Voidsome");


//local method inner class in instance method
class LocalInner2
{
public void seeall2()
{
System.out.println(j);
System.out.println("access the static :" + l);
System.out.println("acess private and public :" +
s1+s2+z);
}

}
LocalInner2 i2 = new LocalInner2();
i2.seeall2();

}
}




run this code you will get all aspect in this code in which
class what restricted what no .wvery inner classes you will
get in it
don't arg without run the code
thanks amit singh
amitsing2008@gmail.com
amit09mca

Is This Answer Correct ?    6 Yes 1 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is thread synchronization in java?

479


How to use arraylist in java netbeans?

508


What is a stringbuilder?

509


What is thread life cycle in java?

578


What are different exception types exceptions available in java ?

473






Can we overload final method in java?

549


Is java se free?

524


What is token in java?

533


How do you write a conditional statement?

517


Are arrays passed by reference in java?

481


Can the garbage collection be forced by any means?

528


What is null object in java?

572


What are scalar data types?

524


What technique is carried out to find out if a particular string is empty?

571


What are the differences between throw and throws?

527