please send code example of inner classes?

Answers were Sorted based on User's Feedback



please send code example of inner classes?..

Answer / 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

please send code example of inner classes?..

Answer / shaik baji

In Java we have 4 types of inner classes

1)Regular Inner Classes
2)Method local Inner Classes
3)Static Inner Classes
4)Anonymous Inner Classes

Regular Inner Class:
---------------------

public class RegularInnerClassDemo
{
RegularInnerClassDemo()
{
System.out.println("RegularInnerClassDemo");
}
class InnerClass
{

InnerClass()
{
System.out.println("InnerClass");
}
public void go()
{
System.out.println("hi");
}
}
public static void main (String [] args)
{
RegularInnerClassDemo f = new
RegularInnerClassDemo();
f.makeBar();
}
void makeBar()
{
(new InnerClass() {}).go();
}
}

Method Local Inner Class:
--------------------------
public class MethodLocalInnerClassDemo
{
public static void main (String [] args)
{
class InnerClass
{
public String name;
public InnerClass(String s)
{
name = s;
}
}
Object obj = new InnerClass("Zippo");
InnerClass h = (InnerClass) obj;
System.out.println(h.name);
}
}

Anonymous Inner Class:
----------------------

Anonymous inner class comes in two forms

1)Normal Anonymous Inner Class
2)Parametrized Anonymous Inner Class

1) Normal Anonymous Inner Class:

Again the Normal Anonymous Inner class is two types

a)Extending the class by Anonymous Inner Class
b)Implementing the interface by Anonymous Inner
Class

a)Extending the class by Anonymous Inner Class

class One
{
void printOne()
{
System.out.println("One");
}
}
class AnonymousDemoByClass
{
public static void main(String Arg[])
{
One obj = new One(){
void printOne()
{
printTwo();
}
void printTwo()
{
System.out.println
("Two");
}
};
obj.printOne();
}
}



Output: Two







b) Implementing the interface by Anonymous Inner
Class

interface One
{
void printOne();
}
class AnonymousDemoByInterface
{
public static void main(String Arg[])
{
One obj = new One(){
public void printOne()
{
printTwo();
}
void printTwo()
{
System.out.println
("Two");
}
};
obj.printOne();
}
}

Output: Two

2)Parametrized Anonymous Inner Class:

Here we are implementing our Anonymous inner class as a
paramer to any method.

interface One
{
void printOne();
}

class ParameterizedAnonymousDemo
{
public static void main(String Arg[])
{
ParameterizedAnonymousDemo obj =
new ParameterizedAnonymousDemo();
obj.doSomething(new One(){
public void printOne
()
{

System.out.println("One");
}
});

}
public void doSomething(One objOne)
{
objOne.printOne();
}
}

Is This Answer Correct ?    1 Yes 0 No

Post New Answer

More Core Java Interview Questions

What is a Transient Object?

0 Answers   InfoAxon Technologies,


what do you understand by synchronization? : Java thread

0 Answers  


What is keyset in java?

0 Answers  


What is meant by data hiding/encapsulation?

0 Answers   Cyient,


How HashMap implemented in java? how it internally works when values are added or searched from hashMap?What is the difference betweenthe implementation of hashmap and Linked Hashmap?

3 Answers   IBM,






What are the benefits of operations?

0 Answers  


Can a lock be acquired on a class?

3 Answers  


What is static in java?

0 Answers  


Why is java so popular?

0 Answers  


which class to use when concatenating strings in a loop.

3 Answers   IBM,


What is the right data type to represent a price in java?

0 Answers  


What is the difference between final, finally and finalize()?

0 Answers  


Categories