Wednesday, February 3, 2016

JAVA INNER CLASS

Introduction:

In this post we will discuss what is inner class with examples.Every Java Programmer
should have knowledge on inner classes in java. I had already post what is class and
object in java with examples.Inner class topic is mostly useful in written Test Exams who are
attending as a java programmer.

 
Definition: 

we can declare a class inside another class such type of classes are called inner classes. This inner class concept is introduced in java 1.1 version as the part of event handling. After so much observing utilities and functionalities also using in regular coding also.

Here are the some real time example of inner class:

Syntax: 

class Outerclass{
class InnerClass{
}
}

Example1:

Without existing car object there is no chance of existing wheel object.Hence we have to declare wheel object inside car class.

Ex:

class car{
class wheel{
}
}

Example 2:

A map is collection of entry  object,without existing map object there is no chance of existing entry object.Hence we have define entry interface   inside map interface.

Ex:

interface Map{
interface entry{
}
}


Advantages of Inner classes in java:


  • Inner classes uses when you design  collection of cooperating classes
  • Inner classes enables you to write professional looking code to handle GUI Events.
  • By using inner classes or nested classes we can increase more readable and maintainable.

Types of Nested classes:

Nested classes are divided into two types.They are

a) static nested classes
b) Non-static nested classes

                In static nested classes static members are available in class whereas non-static nested class is nothing but inner classes  such as method local inner class,anonymous inner class,local inner class.

So we can mention inner classes are three types. They are

a. Inner class
b.method local inner class
c. anonymous inner class

method local  Inner class:

If we create a class inside another class is called Inner class. In this inner class can declare as private.If you declare inner class as private it can not be access from an object outside the class.

Here is an example of Inner class:

class outer{
private int num=80;
class inner{
void display()
{
System.out.println("num is"+num);
}
public static void main(String args[])
{
outer o=new outer();
outer.inner in=o.new inner();
in.display();
}
}

Output :

num is 80


Anonymous Inner class:


If class is declared with no name such type of classes are called anonymous inner class. This type of classes used in such a case where you need to override a method or interface.


Example:

abstract class Book{
abstract void java();
}
class Anonymous{
public static void main(String args[])
{
Book b=new Book();
void java()
{
System.out.println("this is java book");
}
b.book();
}
}

Output:

this is java book

c. Local Inner class:

we can declare a class inside another class is called inner class. To invoke methods of inner class you should instantiate this class inside the method.

Example:


class LocalInner{
private int num=80;
class inner{
void show()
{
class Local
{
void display()
{
System.out.println(num);
}
}
Local la=new Local();
la.display();
}

public static void main(String args[])
{
LocalInner li=LocalInner();
li.show();
}
}

Output:

80







No comments:

Post a Comment

High Paying Jobs after Learning Python

Everyone knows Python is one of the most demand Programming Language. It is a computer programming language to build web applications and sc...