Professional Documents
Culture Documents
Inner Class by Durga Sir
Inner Class by Durga Sir
Exapmple:
package innerClass;
class Departement{
package innerClass;
class Engine{
}
3. Map is a group of key value pairs and each key value pair is
called an entry. Without existing map object, there is no chance
of existing entry object. Hence interface entry is defined inside
map interface.
package innerClass;
interface Entry{
Note:
1. Without existing outer class object, there is no chance of
existing inner class object.
2. The relation between outer class and inner class is not IS-A
relation but it is HAS-A (composition or aggregation)
relationship.
Example1:
package innerClass;
class Inner {
}}
Java.outer-----------RE: Main method not found in class
innerClass.Outer
Example2:
package innerClass;
class Inner {
Output:
main method of outer class
package innerClass;
class Inner {
public static void main(String[] args) {---Error
System.out.println("main method of outer class");
}
}
Compile time error: inner class can not have static declaration
Case1:
Accessing inner class code from static area of outer class:
package innerClass;
class Inner {
public void m1() {
System.out.println("inner class instance method");
}
}
Output:
inner class instance method
package innerClass;
class Inner {
public Inner m1() {
System.out.println("inner class instance method");
return null;
}
}
}
Case2:
package innerClass;
class Inner {
public void m1() {
System.out.println("inner class instance method");
}
}
}
Output:
inner class instance method
case3:
package innerClass;
class Inner {
public void m1() {
System.out.println("inner class instance method");
}
}
}
package innerClass;
case4:
From normal regular inner class we can access both static and
non-static members from outer class directly.
package innerClass;
import innerClass.Outer.Inner;
Within the class “this” always refers current inner class object.
If we want to refer current outer class object. We have to use
outer class name.this.
package innerClass;
class Inner {
int x = 100;
public void m1() {
int x = 1000;
System.out.println(x);
System.out.println(this.x);
System.out.println(Inner.this.x);
System.out.println(Outer.this.x);
Output:
1000
100
100
10
class Inner {
class Innermost{
public void m1() {
System.out.println("Innermost class method");
}
}
package innerClass;
class Inner {
public void sum(int x, int y) {
System.out.println(x+y);
}
}
Inner i = new Inner();
i.sum(10,20);
i.sum(100, 200);
i.sum(1, 20);
5. We can declare method local inner class inside both static and
instance methods.
6. If we declare inner class inside instance method then from that
method local inner class we can access both static and non-static
members of outer class directly.
7. If we declare inner class inside static method then from we can
access only static members of outer class directly from that
method local inner class.
package innerClass;
int x = 10;
static int y = 20;
class Inner {
public void m2() {
System.out.println(x);-------1
System.out.println(y);
}
}
Inner i = new Inner();
i.m2();
}
public static void main(String[] args) {
Outer o = new Outer();
o.m1();
}
}
If we declare m1() method as static then at line 1 we will get compile time
error saying non static variable x cannot be referenced from a static
context.
From method local inner class we can’t access local variables of the method
in which we declare inner class. If the local variable declared as final then
we can access.
package innerClass;
}
Inner i = new Inner();
i.m2();
}
public static void main(String[] args) {
Outer o = new Outer();
o.m1();
}
}
Compile time error: local variable x is accessed from within inner class;
needs to be declared final.
package innerClass;
}
Inner i = new Inner();
i.m2();
}
public static void main(String[] args) {
Outer o = new Outer();
o.m1();
}
}
package innerClass;
}
Inner i = new Inner();
i.m2();
}
public static void main(String[] args) {
Outer o = new Outer();
o.m1();
}
}
We can access only static members inside static methods.
}
Inner i = new Inner();
i.m2();
}
public static void main(String[] args) {
Outer o = new Outer();
o.m1();
}
}
We will get compile time error because we can’t declare static members inside
inner classes.
8. The only applicable modifiers for method local inner classes are
final, abstract and strictfp.
package innerClass;
System.out.println(p.getClass().getName());
System.out.println(p1.getClass().getName());
System.out.println(p2.getClass().getName());
}
}
Output:
spicy
salty
sweet
innerClass.Test$1
innerClass.PopCorn
innerClass.Test$2
analysis:
PopCorn p = new PopCorn();----just we are creating PopCorn object.
PopCorn p = new PopCorn{};----we are declaring a class that extends
PopCorn without name (anonymous inner class). For that child class we
are creating an object with parent reference.
PopCorn p2 = new PopCorn() {
public void taste() {
System.out.println("sweet");
}
};
We are declaring a class that extends PopCorn without name (anonymous
inner class). For that child class we are creating an object with
parent reference.
In that child class we are overriding taste() method.
t.start();
package innerClass;
package innerClass;
}
public static void main(String[] args) {
Nested n = new Nested();
n.m1();
}
}
Example:
package innerClass;
package innerClass;
interface Vehicle{
public int getNoWheels();
}
Map is a group of key value pairs and each key value pair is called
an entry. Without existing map object, there is no chance of
existing entry object. Hence interface entry is defined inside map
interface.
package innerClass;
interface Entry{
package innerClass;
interface Outer {
interface inner{
public void m2();
}
package innerClass;
import innerClass.Outer.Test1;
import innerClass.Outer.Test2;
package innerClass;
interface EmailService {
class EmailDetails {
String toDoList;
String cCList;
String subject;
String body;
}
}
In the above EmailDetails is required only for EmailService and we are not
using anywhere else. Hence EmailDetails class is recommended to declare
EmailService interface.
We can also define a class inside interface to provide default implementation
for that interface.
package innerClass;
interface Vehicle {
package innerClass;
import innerClass.Vehicle.Bus;
Note:
The class which is declared inside interface is always public static whether
we are declaring or not. Hence we can create class object directly without
having outer interface type object.