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
In java how do we copy objects?
how to create multithreaded program? Explain different ways of using thread? : Java thread
Is nan false?
Do you know why doesn't the java library use a randomized version of quicksort?
What is java abstraction with example?
What is lambda in java?
What are some examples of variable costs?
What is the difference between declaration and definition in java?
What is object of class in java?
What is the finalize method do?
Is the milky way in a void?
Why static functions are used?
What is equals method in java?
Can private class be extended java?
Why spring singleton is not thread safe?