Skip to main content

Inner classes

Inner Classes:-class inside class or interface called inner class or nested class. Advantage of inner classes is grouping multiple classes and interfaces into a single class. Another big advantage of inner class is that inner class can happily access all the data or variables of outer class directly.
Types of Nested classes:-
There are two types of nested classes static and non-static nested classes.

(A)Non –static inner classes:-
1)    Member inner class
2)    Anonymous inner class
3)    Local inner class

(B)Static nested class

Example1:-Outer.java

class Outer
{
class Inner
{
void disp()
{
System.out.println("Hello This is Inner class Method");
}
}
public static void main(String[]args)
{
Inner in=new Inner();
in.disp();
}
}
Output:-
C:\JAVATECH>javac Outer.java
Outer.java:12: error: non-static variable this cannot be referenced from a static context
Inner in=new Inner();
         ^
1 error

Example2:-Outer.java

class Outer
{
class Inner
{
void disp()
{
System.out.println("Hello This is Inner class Method");
}
}
public static void main(String[]args)
{
Outer out=new Outer();
Outer.Inner in=out.new Inner();
in.disp();
}
}
Output:-
C:\JAVATECH>javac Outer.java
C:\JAVATECH>java Outer
Hello This is Inner class Method

Example3:-Outer.java

class Outer
{
private String name="Sachin";
private int roll=101;
class Inner
{
void disp()
{
System.out.println("Hello This is Inner class Method");
System.out.println(name+"  your roll number is : "+roll);
}
}
public static void main(String[]args)
{
Outer out=new Outer();
Outer.Inner in=out.new Inner();
in.disp();
}
}
Output:-
C:\JAVATECH>javac Outer.java
C:\JAVATECH>java Outer
Hello This is Inner class Method
Sachin  your roll number is : 101

Example4:-Outer.java

class Outer
{
private String name="Sachin";
private int roll=101;
class Inner
{
private String name="Amit";
private int roll=102;
void disp()
{
System.out.println("Hello This is Inner class Method");
System.out.println(name+"  your roll number is : "+roll);
System.out.println(Outer.this.name+"  your roll number is : "+Outer.this.roll);
}
}
public static void main(String[]args)
{
Outer out=new Outer();
Outer.Inner in=out.new Inner();
in.disp();
}
}
Output:-
C:\JAVATECH>javac Outer.java
C:\JAVATECH>java Outer
Hello This is Inner class Method
Amit  your roll number is : 102
Sachin  your roll number is : 101

Anonymous Inner class:-Anonymous class is class having no name, compiler decide the name of this class at compile time and it used for abstract class and interface for method implementation inside class body. In the case of Anonymous class object created by the subclass and return to the abstract class reference or to interface reference variable.

Example1:-TestM.java

abstract class Car
{
abstract void run();
}
class TestM
{
public static void main(String[]args)
{
Car v=new Car(){
public void run()
{
System.out.println("Run method implemented");
}
};
v.run();
}
}
Output:-
C:\JAVATECH>javac TestM.java
C:\JAVATECH>java TestM
Run method implemented

Example1:-TestM.java

interface Car
{
 void run();
}
class TestM
{
public static void main(String[]args)
{
Car v=new Car(){
public void run()
{
System.out.println("Run method implemented");
}
};
v.run();
}
}
Output:-
C:\JAVATECH>javac TestM.java
C:\JAVATECH>java TestM
Run method implemented


Local Inner class:-Local inner class is declared inside class inside method. Local class object must be created inside method where local class is declared.

Example1:-Outer.java
class Outer
{
private String name="Amit";
void local()
{
class Local
{
void lm()
{
System.out.println("This is local class method   "+name);
}
}
Local l=new Local();
l.lm();
}
public static void main(String[]args)
{
Outer o=new Outer();
o.local();
}
}
Output:-
 C:\JAVATECH>javac Outer.java
C:\JAVATECH>java Outer
This is local class method   Amit


Static Inner class:-static inner class is class which is declared inside class with static modifier and static class uses only static data of outer class only.

Example1:-Outer.java

class Outer
{
private String name="Amit";
static class Inner
{
void lm()
{
System.out.println("This is static inner class method   "+name);
}
}

public static void main(String[]args)
{
Outer.Inner ob=new Outer.Inner();
ob.lm();
}
}
Output:-
C:\JAVATECH>javac Outer.java
Outer.java:8: error: non-static variable name cannot be referenced from a static context
System.out.println("This is static inner class method   "+name);
                                                          ^1 error
Example2:-Outer.java

class Outer
{
private static String name="Amit";
static class Inner
{
void lm()
{
System.out.println("This is static inner class method   "+name);
}
}
public static void main(String[]args)
{
Outer.Inner ob=new Outer.Inner();
ob.lm();
}
}
Output:-
C:\JAVATECH>javac Outer.java
C:\JAVATECH>java Outer
This is static inner class method   Amit

Interface Nesting:-

Example1:-TestInterface.java
interface Outer
{
void get();
interface Inner
{
void show();
}
}
class TestInterface implements Outer.Inner
{
public void show()
{
System.out.println("This is show method implemented");
}
public static void main(String[]args)
{
Outer.Inner in=new TestInterface();
in.show();
}
}
Output:-
 C:\JAVATECH>javac TestInterface.java
C:\JAVATECH>java TestInterface
This is show method implemented

Example2:-TestInterface.java

interface Outer
{
void get();
interface Inner
{
void show();
}
}
class TestInterface implements Outer
{
public void get()
{
System.out.println("This is get method implemented");
}
public static void main(String[]args)
{
Outer in=new TestInterface();
in.get();
}
}
Output:-
 C:\JAVATECH>javac TestInterface.java
C:\JAVATECH>java TestInterface
This is get method implemented

Example3:-TestInterface.java

class Outer
{
interface Inner
{
void show();
}
}
class TestInterface implements Outer.Inner
{
public void show()
{
System.out.println("This is show method implemented");
}
public static void main(String[]args)
{
Outer.Inner in=new TestInterface();
in.show();
}
}
Output:-
 C:\JAVATECH>javac TestInterface.java
C:\JAVATECH>java TestInterface

This is show method implemented

Comments

Popular posts from this blog

Introduction of Java

Java is a high level programming language which is used for creating various types of software's or applications. Types of java applications:- 1)     Stand alone application 2)     Desktop based application          3)     Web based application 4)     Enterprise application etc. Stand alone application:- Stand alone application run on single machine without user interface and every stand alone application having main method. Desktop based application:- Desktop based application also run on single machine with user interface and every Desktop based application having main method. Web based application:- Web based application run on web, web based application not required main method but for such types of application need server. Enterprise application:- Enterprise application is category of web application where doing some business or involve some money related issues. Java Editions: - There are four Java platforms. ·       

Small concept about OOPs

Some small concept about OOPs:- In many books and website write about OOPs concepts as hundred percent object oriented, pure object oriented and partial object oriented language. Hundred percent object oriented:- In the hundred percent object oriented movie hero is object, without object program is incomplete that means can not execute program without object. Without class and object programming or application development is not possible. Example of hundred percent object oriented language is  Smalltalk . Pure object oriented:- In the object oriented cinema obviously object is the hero. In pure object oriented language object having same importance but in this having some relax that is without creating object we can successfully execute our application. So in the object oriented programming without class we cannot create or run our application, class is must. Example of pure object oriented language is  Java . Partial object oriented:- In this type of language cl

java print

Syntax of main method in java is    public static void main(String[]args)  before understanding about main method we have to understand that who is responsible for main method.if we write a simple program or write a simple java class without main method like      Test.java           class Test      {      } above program will compile successfully.for compilation of  java file use  javac tool  or  javac.exe  file. javac Test.java it means compiler checks only syntax of class that means compiler does not check the main method in the file or program,so that compiler is not responsible for main method in java. After successfully compilation of above program we have to run the generated byte code. java Test Error: Main method not found in class Test, please define the main method as:    public static void main(String[] args) So JVM (java virtual machine) or java interpreter is responsible for main method,first jvm check that the main method is pres