Anda di halaman 1dari 23

More on Inner Classes

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

private String questions[]


={"What is an inner class?", "Why should
I use inner class?", "What is an
abstract class?",
"What is an interface?",
"What is a local class ?“
};
12
Java
// methods of interface implemented
public String getFirstQuestion(){
return questions[0];}
public String getLastQuestion(){
return questions[questions.length-1];
}
public String getNextQuestion(){
if(currentPosition>=totNoOfQuestions)
return null;
else return
questions[++currentPosition];} 13
Java
public String getPrevQuestion(){
if(currentPosition<0) return null;
else return questions[--currentPosition];}
public String getQuestion(int index){
currentPosition=index;
return questions[index]; }
public int getTotalQuestions(){
return questions.length;}}
// inner class ends here
return new MyQuestionPaper(); } 14
Java
public static void main(String str[]){
QuestionPaper q= new
Examination("M.C.A",5).getQuestions();
String s=q.getNextQuestion();
while(s!=null) {
System.out.println(s);
s=q.getNextQuestion();
}
}
} 15
Java
Of course that is what the
program prints!

What is an inner class?


Why should I use inner class?
What is an abstract class?
What is an interface?
What is a local 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

class XXX extends


SomeClassOrInterface{
// mostly methods overridden
}
SomeClass s= new XXX();
}

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 ‘()’

return new QuestionPaper(){


private int currentPosition=-1;
/*and data members and interface methods
which we had written earlier*/ };
Notice the ‘;’
}} //end of getQuestions and Examination
21
Java
Calling Parameterized
Constructor
package general;

Person class Test {

<<abstract>> static Person p=


Person() new Person("God"){
Person(String) public int getUID(){
return 0; } };
Person(String,
public static void main(String
String)
str[]){

System.out.println(p.getName());

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

Regular inner class


Static inner class
Local inner class in
static context
Local inner class in
non-static context
Anonymous inner
class in static
context
Anonymous inner
class in non-static
context

23
Java

Anda mungkin juga menyukai