Professional Documents
Culture Documents
6 2 Interface
6 2 Interface
Part 2
1
Java
Contents
1 Revision
2 Local Inner class
3 Local Inner class defined
4 Characteristics
5 Solution to our problem
6 Anonymous Inner Classes
7 Characteristics
8 Calling Parameterized Constructor
9 Summary
2
Java
Know
• Local and Anonymous Inner classes
3
Java
Be Able To
• Use Local and Anonymous Inner classes
4
Java
Revision
What is an inner class ? Why do we need them ?
How to create a regular inner class and what are
their features?
What are the various types of inner class ?
What is a static inner class ? How is it different from
regular inner class ?
5
Java
Local Inner class
QuestionPaper Examination
<<interface>> degree
String getFirstQuestion() QuestionPaper getQuestions(){
String getLastQuestion() }
String getNextQuestion()
defined inside
String getPrevQuestion() QuestionPaper
String getQuestion(int index)
int getTotalQuestions()
MyQuestionPaper
6
Java
Local Inner class defined
An inner class that is defined inside a method is
called local inner class (or method local inner class).
Example:
class OuterClass {
void someMethod(){
class InnerClass{}
}}
7
Java
Characteristics
A local inner class can be instantiated only by the
method which defined it.
Therefore no access specifier is applicable for the
local inner class declaration. Only abstract and
final modifiers are allowed.
Also like other inner classes, local inner class can
access all the members of the outer class including
private members.
Apart from the above, the local inner class can also
access local variables which are final.
8
Java
Solution to our problem
Examination
QuestionPaper getQuestions(){
class MyQuestionPaper implements QuestionPaper{
//implement all the interface method
}
// create an instance of MyQuestionPaper
// return the MyQuestionPaper object
}
9
Java
package exam;
public interface QuestionPaper {
String getFirstQuestion();
String getLastQuestion();
String getNextQuestion();
String getPrevQuestion();
String getQuestion(int index);
int getTotalQuestions();
}
10
Java
package exam;
public class Examination {
private String subCode;
private static final int
totNoOfQuestions=5;
private int noOfQuestions;
public Examination(String subCode,int
noOfQuestions){
this. subCode = subCode;
this.noOfQuestions= noOfQuestions ; }
11
Java
QuestionPaper getQuestions(){
class MyQuestionPaper implements
QuestionPaper{
Local
private int currentPosition=-1; inner class
16
Java
What outer class
variables can the local
inner class, defined
inside a static method,
access?
17
Java
Anonymous Inner Classes
• Inner class without a class name is an anonymous
inner class.
• General way to create an anonymous inner class:
class OuterClass{
…
SomeClassOrInterface s
= new SomeClassOrInterface(){
// mostly methods overridden
}; }
The above syntax is …
18
Java
a shortcut of :
class OuterClass{
… Or implements
19
Java
Characteristics
Anonymous inner class can be created either inside
a method or outside a method. It is implicitly final.
And we obviously see there is no need for a modifier
for an anonymous inner class. And of course,
constructors also do not make sense.
Note that an anonymous inner class is always an
inherited class (either inherited from an interface or
from a class) and so polymorphism is applicable.
Also note that an anonymous inner class directly can
either implement an interface or extend a class.
20
Java
public class Examination {
…
QuestionPaper getQuestions(){
class MyQuestionPaper implements
QuestionPaper{ Notice the ‘()’
22
}} Java
Summary
Fill out the table:
Can access all Can access all static syntax to create syntax to create
instance variables variables of outer Can access all instances inside the instances outside the
Inner ClassType of outer class class local variables outer class outer class
23
Java