1. Objectives
On completion of this period, you would be
able to learn
• Inner classes
• Using classes from other packages
• Importing packages
1
2. Recap
Access protection
• To protect the access of members of an object
• Done with the help of access modifiers
• private, public, protected
2
3. Inner Classes
• Inner classes ?
• We know what is nesting
• Nested if
• Nested looping
• Nested blocks
• Likewise, classes can also be nested
• And such a class is an example of inner class
3
4. Inner Classes Contd..
• Classes can be defined as members of other
classes
• Classes can also be defined within a block of
Java code
• Types
• Member class
• Local Class
• Anonymous Class
4
5. Member Class
• A class defined as a member (non-static) of
another
• Can use members of enclosing classes
• Cannot have static members
• Cannot have same name as a containing class
5
6. Example
public class A {
public String name = "a";
public class B {
public String name = "b";
public class C {
public String name = "c";
public void print_names() {
System.out.println(name); // "c": name field of class C
System.out.println(this.name);// "c": name field of class C
System.out.println(C.this.name);// "c": name field of class
C
System.out.println(B.this.name); // "b": name field of classB
System.out.println(A.this.name);// "a": name field of class A
}
}
}
}
6
7. Example Contd..
class Member Class Test {
public static void main (String[] args ) {
A a = new A(); // Create an instance of A.
A.B b = a. new B(); // Create an instance of B within the
instance of A.
A.B.C c = b. new C(); // Create an instance of C within the
instance of B.
c. print _ names(); // Invoke a method of the instance of c.
} Output
}
7
8. Local Class
• A class defined in a block of code
• Can use members of enclosing classes
• Cannot have static members
• Cannot have same name as a containing class
8
9. Anonymous Class
• Unnamed class defined within an expression
• Can use members of enclosing classes
• Cannot have static members
• Cannot have same name as a containing class
• Has no name or constructor
• Only one instance of the class is created
9
10. Using Classes from Other Packages
• One of the 3 ways can be used:
• Import the package member using import
statement
• Import the member's entire package using
import statement
• Refer to the member by its fully qualified
name (without using import statement)
10
11. Importing Classes from Packages
• To use classes from other package, you need
to import the package of those classes
• By default, all Java programs import the
java.lang.* package
• you can use classes like String and
Integer inside the program even though
you haven't imported any packages
• The import statement is used to import classes
from packages
• There can be any number of import statements
in a source file 11
12. Importing Classes from Packages Contd..
• The syntax of import statement is
import <nameOfPackage>;
• nameOfPackage is any user defined or
system defined package name
• eg. :
• // Importing a class a Date class
• import java. util. Date
• // Importing all classes in java. util package
• import java. util.*;
12
13. import javax.swing.*;
Example Program
public class SumArray {
public static void main( String Output
args[] ) {
int a[] = { 1, 2, 3, 4, 5, 6};
int total = 0;
for ( int i = 0; i < a.length; i+
+)
total += a[ i ];
OptionPane.showMessageDia Importing entire
log( null, "Total of array package
elements: " + total, "Sum the
Elements of an Array",
JOptionPane.INFORMATION
_MESSAGE );
System.exit( 0 );
}
13
}
14. Another Example Program
package MyPack; Package in
public class Balance { which Test is other
String name; Balance class package test; package
double bal; is defined import MyPack.Balance;
public Balance(String n, double b)
{ Importing a
class TestBalance { package member
name = n;
public static void main(String[ ] args)
bal = b;
{
}
Balance test Bal=
public void show() { new Balance("J.
if(bal<0) J. Jaspers", 99.88);
System .out.print("-->> "); testBal.show();
System.out.println(name + ": $" + }
}
bal);
} Output
}
14
15. Fully Qualified Name
• Another approach to use a class from other
packages
• No import statement is used
• Fully Qualify the name of the class
• The following example illustrates:
• public static void main(String[] args) {
• java.util.Date x = new java.util.Date();
• System.out.println(“Today’s Date : “+
x.toString()); Date class is
fully qualified
}
15
16. Example Program
package MyPack; package test;
Package in
public class Balance { which Test is other
String name; class TestBalance { package
Balance class
double bal; is defined public static void main(String[ ] args)
public Balance(String n, double b) {
{ MyPack.Balance testBal =
name = n; new MyPack.Balance("J.
bal = b; J. Jaspers", 99.88);
} testBal.show();
public void show() { }
if(bal<0) }
Fully qualifying class
System.out.print("-->> ");
name
System.out.println(name + ": $" +
Output
bal);}}
16
17. Summary
• In this class we have discussed
• Inner classes
• Types of inner classes
• Different ways of using a class from other
package
• Import statement
• Some example programs
17
18. Frequently Asked Questions
1. What is inner class ?
2. List the types of inner classes
3. What are the different ways of using a class
defined in other package?
4. Write a program in Java that illustrates the use of
import statement
18
19. Quiz
1. Among the following which is NOT an inner
class ?
1. Member Class
2. Local Class
3. Implicit Class
4. Anonymous Class
19
20. Quiz Contd..
2. Which package is imported by default ?
1. java. util.*
2. java. lang.*
3. java. sql.*
4. java. net.*
20
21. Quiz Contd..
3. How many import statements can a Java
class contain?
1. Only one
2. Two
3. Three
4. Zero or more
21
22. Quiz Contd..
4. Which of the following class has no name ?
Member Class
Anonymous Class
Local Class
Inner Class
22