Unit 08 & 091 Nested Classes Introduction Inner Classes Local Classes Anonymous Classes Exercises.

Slides:



Advertisements
Similar presentations
Introduction to classes Sangeetha Parthasarathy 06/11/2001.
Advertisements

Programming With Java ICS201 University Of Hail1 Chapter 13 Inner Classes.
INHERITANCE BASICS Reusability is achieved by INHERITANCE
Nested Classes Yoshi Modified from:
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 Chapter 12 More OOP, Interfaces, and Inner Classes.
Slides prepared by Rose Williams, Binghamton University Chapter 13 Interfaces and Inner Classes.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Enhancing classes Visibility modifiers and encapsulation revisited
Slides prepared by Rose Williams, Binghamton University Chapter 13 Interfaces and Inner Classes.
Inner Classes. Lecture Objectives Learn about inner classes. Know the differences between static and non- static inner classes. Designing and using inner.
What is a class? a class definition is a blueprint to build objects its like you use the blueprint for a house to build many houses in the same way you.
1 Nested Classes  Learning Outcome  List and distinguish between the different categories of nested classes  List and explain four applications/benefits.
1 Inner Classes Overview  Java Inner Classes: A Definition.  Overview of Nested Classes.  Top level Nested Classes.  Non-static Inner Classes.  Non-static.
Unit 081 Introduction to Nested Classes Nested classes are classes defined within other classes The class that includes the nested class is called the.
Nested Classes OOP tirgul No Nested Classes Java allows you to define Nested Classes public class EnclosingClass { public int _dataMember = 7;
Slides prepared by Rose Williams, Binghamton University Chapter 13 Interfaces and Inner Classes.
1 Introduction to Searching and Sorting Comparable Interface -Reading p Comparator Interface.
Inner Classes. Inner classes All the classes so far have been “top level” It is possible (and useful) to define a class inside another class Inner classes.
1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,
Lecture From Chapter 6 & /8/10 1 Method of Classes.
CMSC 202 Inner Classes. Aug 7, Simple Uses of Inner Classes Inner classes are classes defined within other classes  The class that includes the.
Java Methods By J. W. Rider. Java Methods Modularity Declaring methods –Header, signature, prototype Static Void Local variables –this Return Reentrancy.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Objects and Classes Chapter 6 CSCI CSCI 1302 – Objects and Classes2 Outline Introduction Defining Classes for Objects Constructing Objects Accessing.
2-Dec-15 Inner Classes. 2 Inner classes All the classes so far have been “top level” It is possible (and useful) to define a class inside another class.
2-Dec-15 Inner Classes By Alguien Soy. 2 Inner classes All the classes so far have been “top level” It is possible (and useful) to define a class inside.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
Classes Modeling the Object. Objects model the world Classes are programmer defined types that model the parts of a system Class serve as blueprints for.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Interfaces and Inner Classes
Access Modifiers Control which classes use a feature Only class-level variables may be controlled by access modifiers Modifiers 1. public 2. protected.
CSI 3125, Preliminaries, page 1 Overloading Methods In Java it is possible to define two or more methods within the same class that share the same name,
Nested Classes CompSci 230 S Software Construction.
1 Enhanced Class Design -- Introduction  We now examine several features of class design and organization that can improve reusability and system elegance.
Access Specifier. Anything declared public can be accessed from anywhere. Anything declared private cannot be seen outside of its class. When a member.
Chapter 4: More Object Concepts. Objectives Understand blocks and scope Overload a method Avoid ambiguity Create and call constructors with parameters.
Topics Instance variables, set and get methods Encapsulation
Object Oriented Programming in Java Habib Rostami Lecture 10.
Advanced Java class Nested Classes & Interfaces. Types of Nested Classes & Interfaces top-level nested –classes –interfaces inner classes –member –local.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
N ESTED C LASSES -1. T YPES OF N ESTED C LASSES & I NTERFACES top-level nested classes interfaces inner classes member local named anonymous.
Inner Classes.
Topic: Inner Classes Course : JAVA PROGRAMMING Paper Code: ETCS-307 Faculty : Dr. Prabhjot Kaur Reader, Dept. of IT 1.
Inner Classes 27-Dec-17.
Chapter 5: Enhancing Classes
CompSci 230 S Software Construction
Classes (Part 1) Lecture 3
Inheritance and Polymorphism
CompSci 230 S Programming Techniques
Nested class.
Interfaces and Inner Classes
Interface.
Inner Classes 29-Nov-18.
Interfaces.
Inner Classes.
Method of Classes Chapter 7, page 155 Lecture /4/6.
Inner Classes 17-Apr-19.
Inner Classes 21-Apr-19.
Inner Classes 1-May-19.
Inner Classes 11-May-19.
Inner Classes 18-May-19.
Inner Classes.
ITE “A” GROUP 2 ENCAPSULATION.
CSG2H3 Object Oriented Programming
Inner Classes 25-Oct-19.
Presentation transcript:

Unit 08 & 091 Nested Classes Introduction Inner Classes Local Classes Anonymous Classes Exercises

Unit 08 & 092 Introduction to Nested Classes So far, our classes and interfaces were defined at top-level. These top-level classes and interfaces are grouped into packages. Classes in the same package are accessible to each other. With access modifiers, classes can have inter-package access. Newer versions of Java enable defining classes and interfaces inside other classes. Benefits of nested classes will be discussed at the end after they become familiar.

Unit 08 & 093 Introduction to Nested Classes (cont’d) There are four categories of nested classes in Java: 1.Static member classes and interfaces. 2.Inner classes, defined inside a class (also called Non-Static Member Classes) 3.Local classes ( defined inside a block of Java code) 4.Anonymous classes ( defined inside a block of Java code) Only the last three categories will be covered in this course.

Unit 08 & 094 Nested Classes at a Glance interface AnInterface { void f(); } class AClass{ class InnerClass { //... } public void myMethod1(){ class LocalClass { //... } public AnInterface myMethod2(){ return new AnInterface(){ // Anonymous public void f(){} }; }

Unit 08 & 095 Inner Classes A Inner class is analogous to an instance field or instance method. Like other instance members, an inner class can have any access modifier. Every instance of a inner class is linked with an instance of the containing class. A nested class produces a separate byte code file. –If a nested class called Inside is declared in an outer class called Outside, two byte code files are produced: Outside.class Outside$Inside.class Inner classes cannot have static fields or static methods Interfaces cannot be defined as inner classes. Why?

Unit 08 & 096 Example 1: Inner Classes class Outer{ private int i = 10; private String s = "hello"; private class Inner{ //private instance variables of the enclosing class object can be accessed public void test(){ i += 10; System.out.println(i); System.out.println(s); } //creates a Inner class object which has access to the private state of //the object it is called for. public void test(){ Inner n = new Inner(); n.test(); } public static void main(String[] args){ Outer obj = new Outer(); obj.test(); }

Unit 08 & 097 Using this Keyword in Inner Classes The this reference is applicable inside inner classes. From within an instance of an inner class we can refer to two objects, the inner object, and the outer object associated with it. How do we distinguish the two objects? The next example demonstrates how this can be done. –To refer to the outer class object, use the notation OuterClass.this

Unit 08 & 098 Example 2: Keyword this in Inner Classes 1 class InnerClassesAndThis { 2 String x = "ICS 201"; 3 class B { 4 int x = 5; 5 void f(int x){ 6 System.out.println(x); 7 System.out.println(this.x); 8 System.out.println(InnerClassesAndThis.this.x); 9 } 10 } 11 public static void main(String [] s){ 12 InnerClassesAndThis outer = new InnerClassesAndThis (); 13 B b = outer.new B(); 14 b.f(7); 15 } 16 }

Unit 08 & 099 Local Classes When a class name is used only within a block it can be defined locally. A local class is defined within a block of Java code. Like a local variable a local class is visible only within its enclosing block. Local classes share many of the features of inner classes. An instance of a local class is associated with an instance of its enclosing class. Local classes are completely hidden in their containing block. Interfaces cannot be defined locally. Why?

Unit 08 & 0910 Example 3: Local Classes class LocalClassExample{ private String name = "KFUPM"; public void method(){ int j = 20; final int k = 30; class Local{ public void test(){ //System.out.println(j); //Error as j is not final System.out.println(k); //OK k is final //Like an inner class, instance variables of //the enclosing object can be accessed. System.out.println(name); } Local l = new Local(); l.test(); } public static void main(String[] args){ LocalClassExample obj = new LocalClassExample(); obj.method(); }

Unit 08 & 0911 Example 4: Local Classes interface Thing{ public void test(); } class LocalClassExample2{ private String name = "KFUPM"; public Thing f(){ int j = 20; final int k = 30; class Local implements Thing{ public void test(){ //System.out.println(j); //Error as j is not final System.out.println(k); //OK k is final //the enclosing object instance variables can be accessed. System.out.println(name); } Local l = new Local(); return l; } public static void main(String[] args){ LocalClassExample2 obj1 = new LocalClassExample2(); Thing obj2 = obj1.f(); obj2.test(); }

Unit 08 & 0912 Anonymous Classes When a local class is used only once, it can be defined anonymously. An anonymous class is a local class without a name. An anonymous class is defined and instantiated in a single expression. An anonymous class has no constructors. Construction parameters can be given through the superclass constructor. When an anonymous class implements an interface, it cannot have any construction parameters. Why? Interfaces cannot be defined anonymously. Why?

Unit 08 & 0913 Example 5: Anonymous Classes interface Thing{ public void test(); } class LocalClassExample2{ private String name = "KFUPM"; public Thing f(){ int j = 20; final int k = 30; return new Thing() { // equivalent to Example 4 but using anonymous class public void test(){ //System.out.println(j); //Error as j is not final System.out.println(k); //OK k is final //the enclosing object instance variables can be accessed. System.out.println(name); } }; } public static void main(String[] args){ LocalClassExample2 obj1 = new LocalClassExample2(); Thing obj2 = obj1.f(); obj2.test(); }

Unit 08 & 0914 Example 6: Anonymous Classes interface Thing{ public void test(); } class AnonymousClassExample{ private String name = "KFUPM"; private Thing method1() { Thing obj = new Thing(){ public void test(){ //instance variables of enclosing object can be accessed. System.out.println(name); } }; return obj; } private Thing method2(){ Thing obj = new Thing(){ public void test(){ System.out.println(name+name+name); } }; return obj; } public static void main(String[] args){ AnonymousClassExample obj = new AnonymousClassExample(); Thing t; t = obj.method1(); t.test(); t = obj.method2(); t.test(); }

Unit 08 & 0915 Benefits of Nested Classes 1.Object-orientation: Separation of definition from functionality. 2.Code organization: Namespace control and access control. Inner classes can be hidden from other classes in the same package. 3.Multiple inheritance: Inner classes of a class can each inherit from a different class.

Unit 08 & 0916 Benefits of Nested Classes (Cont’d) 4. Code Simplification: 1.Inner classes access outer class variables automatically ( even if they are declared private). 2.In certain situations this makes the implementation of the classes easier because they can share information easily. Eg., in the following code, method f method can access both x and y directly: public class A { int y; public class B { int x; void f () {….} }

Unit 08 & 0917 Review Exercises 1. Use appropriate examples to explain how nested classes provide addition support for object orientation, code organization and multiple implementation inheritance. 2. Can an interface be defined inside an inner class? 3. Write a complete program to illustrate how to access object of an outer class from an object of the inner class. 4. Mention two similarities and two differences between inner classes and anonymous classes