Unit 081 Introduction to Nested Classes Nested classes are classes defined within other classes The class that includes the nested class is called the.

Slides:



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

Programming With Java ICS201 University Of Hail1 Chapter 13 Inner Classes.
IMPLEMENTING CLASSES Chapter 3. Black Box  Something that magically does its thing!  You know what it does but not how.  You really don’t care how.
Nested Classes Yoshi Modified from:
Unit 08 & 091 Nested Classes Introduction Inner Classes Local Classes Anonymous Classes Exercises.
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.
Chapter 13 Interfaces and Inner Classes Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
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.
CS102--Object Oriented Programming Lecture 16: – Inner classes + review Copyright © 2008 Xiaoyan Li.
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 Inner Classes Overview  Java Inner Classes: A Definition.  Overview of Nested Classes.  Top level Nested Classes.  Non-static Inner Classes.  Non-static.
Slides prepared by Rose Williams, Binghamton University Chapter 13 Interfaces and Inner Classes.
CS102--Object Oriented Programming Lecture 15: Interfaces Copyright © 2008 Xiaoyan Li.
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.
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.
220 FINAL TEST REVIEW SESSION Omar Abdelwahab. INHERITANCE AND POLYMORPHISM Suppose you have a class FunClass with public methods show, tell, and smile.
CS 355 – PROGRAMMING LANGUAGES Dr. X. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Topics Scope Scope and Lifetime Referencing Environments.
2000 Jordan Anastasiade. All rights reserved. 1 Class In this lesson you will be learning about: Class. Inheritance. Polymorphism. Nested and.
Objects and Classes Chapter 6 CSCI CSCI 1302 – Objects and Classes2 Outline Introduction Defining Classes for Objects Constructing Objects Accessing.
Java Interfaces. Interfaces An interface is something like an extreme case of an abstract class – However, an interface is not a class – It is a type.
Comp 249 Programming Methodology Chapter 13 Interfaces & Inner Classes Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
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.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
ACM/JETT Workshop - August 4-5, : Defining Classes in Java.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
SEEM Java – Basic Introduction, Classes and Objects.
Interfaces and Inner Classes
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.
Topics Instance variables, set and get methods Encapsulation
Basic Syntax อ. ยืนยง กันทะเนตร คณะเทคโนโลยีสารสนเทศและการสื่อสาร มหาวิทยาลัยพะเยา Chapter 2.
5.1 Basics of defining and using classes A review of class and object definitions A class is a template or blueprint for an object A class defines.
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.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
Lecture 9: Object and Classes Michael Hsu CSULA. 2 OO Programming Concepts Object-oriented programming (OOP) involves programming using objects. An object.
Basic Class Structure. Class vs. Object class - a template for building an object –defines the instance data that the object will hold –defines instance.
CLASSES IN JAVA Primitive Types Example of a Class Allocating Objects of a Class Protecting Class data Constructors Static data and Static Methods.
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.
CompSci 230 S Software Construction
Chapter 8 Classes and Objects
CompSci 230 S Programming Techniques
Nested class.
Comp 249 Programming Methodology
Interfaces and Inner Classes
Overloading and Overriding
Inner Classes 29-Nov-18.
Interfaces and Inner Classes
Inner Classes.
CMSC 202 Inner Classes.
Java 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.
Inner Classes 25-Oct-19.
Presentation transcript:

Unit 081 Introduction to Nested Classes Nested classes are classes defined within other classes The class that includes the nested class is called the outer class There are four categories of nested classes in Java: 1.Inner classes, also called Non-Static Nested Classes 2.Static nested classes 3.Local classes (defined inside a block of Java code) 4.Anonymous classes (defined inside a block of Java code)

Unit 082 Simple Uses of Inner Classes An inner class definition is a member of the outer class in the same way that the instance variables and methods of the outer class are members –An inner class is local to the outer class definition –If the inner class is private, then the inner class cannot be accessed by name outside the definition of the outer class Using an inner class as a helping class is one of the most useful applications of inner classes

Unit 083 Accessing Private Members Within the definition of a method of an inner class: –It is legal to reference a private instance variable of the outer class –It is legal to invoke a private method of the outer class Within the definition of a method of the outer class –It is legal to reference a private instance variable of the inner class on an object of the inner class –It is legal to invoke a method of the inner class on an object of the inner class

Unit 084 Class with an Inner Class

Unit 085 Class with an Inner Class

Unit 086 Class with an Inner Class

Unit 087 The.class File for an Inner Class Compiling any class in Java produces a.class file named ClassName.class Compiling a class with an inner classes causes both classes to be compiled, and produces two.class files –Such as ClassName.class and ClassName$InnerClassName.class

Unit 088 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. To access the inner class object use this To access the outer class object use OuterClassName.this

Unit 089 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 0810 Static Nested Classes An inner class (non-static nested class) has a connection between its objects and the outer class object that created the inner class object –This allows an inner class definition to reference an instance variable, or invoke a method of the outer class There are certain situations, however, when a nested class must be static –If an object of the nested class is created within a static method of the outer class –If the nested class must have static members

Unit 0811 Static Nested Classes Since a static nested class has no connection to an object of the outer class, –Instance variables of the outer class cannot be referenced –Non-static methods of the outer class cannot be invoked To invoke a static method or to use a static variable of a static nested class within the outer class, preface each with the name of the nested class and a dot

Unit 0812 Public Nested Classes If a nested class is marked public, then it can be used outside of the outer class In the case of a non-static nested class, it must be created using an object of the outer class BankAccount account = new BankAccount(); BankAccount.Money amount = account.new Money("41.99"); –Note that the prefix account. must come before new –The new object amount can now invoke methods from the inner class, but only from the inner class

Unit 0813 Public Inner Classes In the case of a static nested class, the procedure is simpler OuterClass.InnerClass innerObject = new OuterClass.InnerClass(); –Note that all of the following are acceptable innerObject.nonstaticMethod(); innerObject.staticMethod(); OuterClass.InnerClass.staticMethod();

Unit 0814 Local Classes A local class is defined within a block of Java code. Local classes are completely hidden in their containing block. When a class name is used only within a block it can be defined locally. A local class can access instance variables of the outer class and only the final local variables of the enclosing block.

Unit 0815 Local Classes: Example 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 0816 Local Classes: Example 2 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 0817 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 and 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?

Unit 0818 Anonymous Classes: Example interface Thing{ public void test(); } class AnonymousClassExample { 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){ AnonymousClassExample obj1 = new AnonymousClassExample(); Thing obj2 = obj1.f(); obj2.test(); }

Unit 0819 Anonymous Classes: Example 2 interface Thing{ public void test(); } class AnonymousClassExample2{ 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){ AnonymousClassExample2 obj = new AnonymousClassExample2(); Thing t; t = obj.method1(); t.test(); t = obj.method2(); t.test(); }