Inner classes9 cm604.28

202 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
202
On SlideShare
0
From Embeds
0
Number of Embeds
8
Actions
Shares
0
Downloads
2
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Inner classes9 cm604.28

  1. 1. ObjectivesOn completion of this period, you would beable to learn• Inner classes• Using classes from other packages• Importing packages 1
  2. 2. RecapAccess protection• To protect the access of members of an object • Done with the help of access modifiers • private, public, protected 2
  3. 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. 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. 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. 6. Examplepublic 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. 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. 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. 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. 10. 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 10
  11. 11. Frequently Asked Questions1. What is inner class ?2. List the types of inner classes3. 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 11
  12. 12. Quiz1. Among the following which is NOT an inner class ? 1. Member Class 2. Local Class 3. Implicit Class 4. Anonymous Class 12
  13. 13. Quiz Contd..2. Which package is imported by default ? 1. java. util.* 2. java. lang.* 3. java. sql.* 4. java. net.* 13
  14. 14. Quiz Contd..3. How many import statements can a Java class contain? 1. Only one 2. Two 3. Three 4. Zero or more 14
  15. 15. Quiz Contd..4. Which of the following class has no name ? Member Class Anonymous Class Local Class Inner Class 15

×