Without existing one type of oject if there is no chance of existing another type of object then go for inner classses
Example university and department
University consists of several depts and hence dept is inner class of university
Example 2 : engine is inner class of car
Example 3:
Map is a group of key value pairs and each key value pair is called an Entry
With out existing map object there is no chance of existing entry object hence interface Entry is defined
inside Map interface
interface Map{
interface Entry{
}
}
Note :
1)With out existing outer class object there is no chance of existing inner class object
2)Relation b/w outer and inner class is has-A relation
Example university and department
University consists of several depts and hence dept is inner class of university
Example 2 : engine is inner class of car
Example 3:
Map is a group of key value pairs and each key value pair is called an Entry
With out existing map object there is no chance of existing entry object hence interface Entry is defined
inside Map interface
interface Map{
interface Entry{
}
}
Note :
1)With out existing outer class object there is no chance of existing inner class object
2)Relation b/w outer and inner class is has-A relation
Types
1)Normal or regular inner classes
2)Method local inner classes
3)Anonymous inner clases
4)Static nested class
Normal or regular inner classes
If we are declaring any named class directly inside a class with out static modifier such type of inner class is normal or regular inner class
When wee compile outer.java following files are generated
outer$inner.class and outer.class
Class outer{
class inner
{
}
psvm()
{
S.O.p("hello");
}
}
If we run outer class we get hello as output
If we run inner class java outer$inner no such method error main
Note:
staic memebers not allowed in inner class including main method
lass outer{
class inner
{
psvm()
{
S.O.p("hello");
}
}
}
Above we get compile time error as staic memebers not allowed in inner class including main method
Accessing inner class code from static area of outer class
Class outer{
class inner
{
public void m1()
{
S.O.p("hello");
}
}
psvm()
{
//To call m1 we need inner class obj and before that create outer class object
outer o = new outer();
outer.inner i = o.new inner();
i.m1();
}
}
accessing inner class code from instance area of outer class
Class outer{
class inner
{
public void m1()
{
S.O.p("hello");
}
}
public void m2()
{
S.O.p("hello m2");
//calling m1;
inner i = new inner();
i.m1();
}
psvm()
{
//call m2
outer o = new outer();
o.m2;
}
}
Accessing inner class method From outside outer class
class outer{
class inner
{
m1()
{
S.O.P("hello inner");
}
}
}
Class Test{
psvm()
{
// want to access m1
outer o = new outer();
outer.inner i = o.new inner();
i.m1();
}
}
Note
1)From normal inner class we can access both static or non static members of outer class
2)With in inner clas this always refers current inner class object if we want to refer current outer class object
we have to use Outer Class name ".this"
3)Mofifiers for outer class
public , default , final , abstract ,strictfp
4)Modifiers for inner class
public , default , final , abstract ,strictfp ,private , protected , static
Nesting of inner classes
Inside inner class we can declare another inner class i,e nesting of inner classes is possible
Example
Class A
{
Class B
{
Class C
{
public void m1()
{
S.O.P("hello");
}
}
}
}
Class Test{
P S V M()
{
A a = new A();
A.B b = a.new B();
A.B.C c = b.new C();
c.m1();
}
Method local inner classes
Main purpose of Method local inner class is to define method specific repeatedilty required functionality
Method local inner classes are best suitable to meet nested method requirements
Scope of method local inner classes is with in method only
These are rarely used
Note
1)Nested methods not alloewed in java
2)We can eclare method local inner class inside both instance and static methods
3)If we declare method local inner class in the instance method of outer class then we can access both instnce and static variables of outer class from the method local inner class
4)If we declare method local inner class in the static method of outer class then we can access only static variables of outer class from the method local inner class
5)From Method local inner class we can't access local variables of method in which we declared inner class , if local variable declared as final then we can access
Example
Class Test{
int i = 10;
static int j = 30;
public void m1()
{
int k =10;
final int m = 40;
Class inner{
public void m2()
{
Line 1;
}
}
}
}
From lne 1 which variables we can access
Except k we can access i , j and m variables
If we declare m2 as static then it will be a ompile time error since we cannot declare static in inner classes
Note
Only applicable modifiers for method local classes are final , abstract , strict fp
}
Anonymous inner classes
Sometimes we can declare inner class with out name these are anonymous inner class
Only one time use ,i.e instant use classes are anonymous inner class
Main purpose is one time usage
3 types are there
1)Anonymous inner class that extends a class
2)1)Anonymous inner class that implements an interface
3)1)Anonymous inner class that is defined inside arguments
1)Anonymous inner class that extends a class
only one time requirement or then use this
Eample
Class PopCorn
{
public void taste()
{
S.O.P("salty");
}
}
Class Test{
P S V M()
{
PopCorn p = new PopCorn()
{
public void taste()
{
S.O.P("spicy");
}
};
p.taste();//spicy
PopCorn p1 = new PopCorn();
p1.taste();//salty
PopCorn p2 = new PopCorn()
{
public void taste()
{
S.O.P("Sweet");
}
};
p2.taste();//Sweet
}
}
Note:
1)PopCorn p = new PopCorn(); // we are creating popcorn object
2)PopCorn p = new PopCorn()
{
};//we are declaring a class that extends popcorn witout name(anonymous inner class)
For that child class we are creating an object with parent reference
3)2)PopCorn p = new PopCorn()
{
public void taste()
{
S.O.P("gg");
}
};
//we are declaring a class that extends popcorn with out name (anonymous inner class)
In that child class we are overriding taste method
For that child class we are creating an object with parent reference
Normal java class Vs Anonymous inner class
1)Both can extend only one class
2)Anonymous inner class can implement only one interface
3)Anonymous inner class can extend a class and implement interface but not both simultaneously
4)constructor cannot be written in anonymous inner class since these classes do not have any name
Note ;
Some times we can declare inner class with static modifier such type of inner classes are called static nested classes.Thesse ra enot strogly associated with outer classes
To access static nested class method outer class object is not required
If we want to create nested class obj outside of outer class then we can create as follows
OuterClassname.Nested n = new OuterClassname.Nested();
In static nested classes We can declare static methods including main method and hence we an invoke static nested class directly from command prompt
From static nested class we cannot access instance members of outer class
Normal inner class vs static nested class
1)Neste class not closely associated with outer class , normal opposite
2)We cannot declare static members in normal inner class opp in nested
3)Can't declare main methid in normal inner classes , nested opp
4)From normal inner class we can access both static and non static members of outer class but in nested we can access only static members
Anonymous inner classes
Sometimes we can declare inner class with out name these are anonymous inner class
Only one time use ,i.e instant use classes are anonymous inner class
Main purpose is one time usage
3 types are there
1)Anonymous inner class that extends a class
2)1)Anonymous inner class that implements an interface
3)1)Anonymous inner class that is defined inside arguments
1)Anonymous inner class that extends a class
only one time requirement or then use this
Eample
Class PopCorn
{
public void taste()
{
S.O.P("salty");
}
}
Class Test{
P S V M()
{
PopCorn p = new PopCorn()
{
public void taste()
{
S.O.P("spicy");
}
};
p.taste();//spicy
PopCorn p1 = new PopCorn();
p1.taste();//salty
PopCorn p2 = new PopCorn()
{
public void taste()
{
S.O.P("Sweet");
}
};
p2.taste();//Sweet
}
}
Note:
1)PopCorn p = new PopCorn(); // we are creating popcorn object
2)PopCorn p = new PopCorn()
{
};//we are declaring a class that extends popcorn witout name(anonymous inner class)
For that child class we are creating an object with parent reference
3)2)PopCorn p = new PopCorn()
{
public void taste()
{
S.O.P("gg");
}
};
//we are declaring a class that extends popcorn with out name (anonymous inner class)
In that child class we are overriding taste method
For that child class we are creating an object with parent reference
Normal java class Vs Anonymous inner class
1)Both can extend only one class
2)Anonymous inner class can implement only one interface
3)Anonymous inner class can extend a class and implement interface but not both simultaneously
4)constructor cannot be written in anonymous inner class since these classes do not have any name
Note ;
Some times we can declare inner class with static modifier such type of inner classes are called static nested classes.Thesse ra enot strogly associated with outer classes
To access static nested class method outer class object is not required
If we want to create nested class obj outside of outer class then we can create as follows
OuterClassname.Nested n = new OuterClassname.Nested();
In static nested classes We can declare static methods including main method and hence we an invoke static nested class directly from command prompt
From static nested class we cannot access instance members of outer class
Normal inner class vs static nested class
1)Neste class not closely associated with outer class , normal opposite
2)We cannot declare static members in normal inner class opp in nested
3)Can't declare main methid in normal inner classes , nested opp
4)From normal inner class we can access both static and non static members of outer class but in nested we can access only static members
No comments:
Post a Comment