This site "java learning concept" is very useful for programming languages like JAVA,PYTHON,C,C++, Data Structures etc.Java learning Concept also provides technical materials , which are related to latest technologies. We provides very easy lectures to our viewers from beginning to advance level.We focus logical content instead of theoretical content.Our coding lecture are just fun and hope you will enjoy.

Friday, 24 May 2019

INNER CLASSES IN JAVA

ANONYMOUS INNER CLASSES IN JAVA

Inner classes concept  in java  is one of the most  important concept in java programming.It is not a new concept in java.but most of programmers not that much comfortable in inner classes concept.

So basically anonymous inner class is a  class which does not have any name, yes without name. It is mostly used in the instantly purpose.If our requirement is just use the code only ones and not use that anymore than we should go for anonymous inner classes.


syntax:

{

};

Example:  

Without anonymous class approch:

Take a class Icecream and a method test. if you are  not satisfy with test then override the test function according to your requirement.
class Icecream
{
public void test()
{
System.out.println("stobarry");
}
}

class Desert extends Icecream
{
public void item()
{
System.out.println("vanila");
}
public static void main(String[] args)
{
Desert t=new Desert();
t.item();
}
}

output:

Vanila


With anonymous inner class aproch:


class Icecream
{
publi
System.out.println("stobarry");
}
public static void main(String[] args)
{
Icecream i=new Icecream()
{
public void test()
{
System.out.println("choclate");
}
};
i.test();
}
}

output:

choclate

There are three types of anonymous inner classes:

1. Anonymous inner class which extends a class
2. Anonymous inner class which implements an interface
3. Anonymous inner class inside arguments.

1. Anonymous inner class which extends a class:

Example:

class AnonymousDemo
{
public static void main(String[] args)
{
Thread t=new Thread()
{
public void run()
{
for(int i=0;i<10;i++)
{
System.out.println("child thread");
}
}
};
t.start();
for(int i=0;i<10;i++)
{
System.out.println("main thread");
}
}
}

output:

main thread
main thread
main thread
main thread
main thread
main thread
main thread
main thread
main thread
main thread
child thread
child thread
child thread
child thread
child thread
child thread
child thread
child thread
child thread
child thread

2. Anonymous inner class which implements an interface


class AnonymousDemo
{
public static void main(String[] args)
{
Runnable r=new Runnable()
{
public void run()
{
for(int i=0;i<10;i++)
{
System.out.println("child thread-1");
}
}
};
Thread t=new Thread(r);
t.start();
for(int i=0;i<10;i++)
{
System.out.println("main thread-1");
}
}
}

output:

main thread-1
main thread-1
main thread-1
main thread-1
main thread-1
main thread-1
main thread-1
main thread-1
main thread-1
main thread-1
child thread-1
child thread-1
child thread-1
child thread-1
child thread-1
child thread-1
child thread-1
child thread-1
child thread-1
child thread-1

3. Anonymous inner class inside arguments


class AnonymousDemo
{
public static void main(String[] args)
{
new Thread(new Runnable()
{
public void run()
{
for(int i=0;i<10;i++)
{
System.out.println("child");
}
}
}).start();
for(int i=0;i<10;i++)
{
System.out.println("main");
}
}
}

output:

main
main
main
main
main
main
main
main
main
main
child
child
child
child
child
child
child
child
child
child





No comments:

Post a Comment

Adbox