Slides prepared by Rose Williams, Binghamton University Chapter 13 Interfaces and Inner Classes.

Slides:



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

Programming With Java ICS201 University Of Hail1 Chapter 13 Inner Classes.
METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same.
Generics and the ArrayList Class
Comp 249 Programming Methodology Chapter 7 - Inheritance – Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University,
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 7 Inheritance.
Slides prepared by Rose Williams, Binghamton University Chapter 1 Getting Started 1.1 Introduction to Java.
Slides prepared by Rose Williams, Binghamton University Chapter 14 Generics and the ArrayList Class.
Slides prepared by Rose Williams, Binghamton University Chapter 7 Inheritance.
Slides prepared by Rose Williams, Binghamton University Chapter 9 Exception Handling.
Slides prepared by Rose Williams, Binghamton University Chapter 14 Generics and the ArrayList Class.
Slides prepared by Rose Williams, Binghamton University Chapter 20 Java Threads Part II.
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.
Slides prepared by Rose Williams, Binghamton University Chapter 14 Generics and the ArrayList Class.
Slides prepared by Rose Williams, Binghamton University Chapter 8 Polymorphism Part I.
Slides prepared by Rose Williams, Binghamton University Chapter 9 More Exception Handling.
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 prepared by Rose Williams, Binghamton University Chapter 7 Inheritance.
Slides prepared by Rose Williams, Binghamton University Chapter 1 Getting Started 1.3 The Class String.
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 in Java. Overview Inner class – one class nested inside another – static (i.e. class) rarely if ever used – member (i.e. instance) – anonymous.
Inheritance. © 2004 Pearson Addison-Wesley. All rights reserved 8-2 Inheritance Inheritance is a fundamental object-oriented design technique used to.
CMSC 202 Inner Classes. Aug 7, Simple Uses of Inner Classes Inner classes are classes defined within other classes  The class that includes the.
Comp 248 Introduction to Programming Chapter 4 - Defining Classes Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Comp 249 Programming Methodology Chapter 8 - Polymorphism Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University, Montreal,
Writing Classes (Chapter 4)
Objects and Classes Chapter 6 CSCI CSCI 1302 – Objects and Classes2 Outline Introduction Defining Classes for Objects Constructing Objects Accessing.
Chapter 1 Section 1.1 Introduction to Java Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage.
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.
Chapter 4 Writing Classes Part 2. © 2004 Pearson Addison-Wesley. All rights reserved4-2 Classes A class can contain data declarations and method declarations.
Comp 248 Introduction to Programming Chapter 4 & 5 Defining Classes Part B 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.
© 2004 Pearson Addison-Wesley. All rights reserved September 14, 2007 Anatomy of a Method ComS 207: Programming I (in Java) Iowa State University, FALL.
CMSC 202 Inheritance II. Version 10/092 Inherited Constructors? An Employee constructor cannot be used to create HourlyEmployee objects. Why not? We must.
Interfaces and Inner Classes
Nested Classes CompSci 230 S Software Construction.
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.
Basic Class Structure. Class vs. Object class - a template for building an object –defines the instance data that the object will hold –defines instance.
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.
Chapter 5: Enhancing Classes
Nested class.
Comp 249 Programming Methodology
Interfaces and Inner Classes
Inheritance 2nd Lecture
Inner Classes 29-Nov-18.
Comp 249 Programming Methodology
Inheritance 2nd Lecture
Interfaces and Inner Classes
Inheritance 2nd Lecture
Inner Classes.
CMSC 202 Inner Classes.
Inner Classes 17-Apr-19.
Inner Classes 21-Apr-19.
Inner Classes 11-May-19.
Inner Classes 18-May-19.
Chapter 4 Constructors Section 4.4
CMSC 202 Inheritance II.
Presentation transcript:

Slides prepared by Rose Williams, Binghamton University Chapter 13 Interfaces and Inner Classes

© 2006 Pearson Addison-Wesley. All rights reserved13-2 Static Inner Classes A normal inner 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 an inner class must be static –If an object of the inner class is created within a static method of the outer class –If the inner class must have static members

© 2006 Pearson Addison-Wesley. All rights reserved13-3 Static Inner Classes Since a static inner class has no connection to an object of the outer class, within an inner class method –Instance variables of the outer class cannot be referenced –Nonstatic methods of the outer class cannot be invoked To invoke a static method or to name a static variable of a static inner class within the outer class, preface each with the name of the inner class and a dot

© 2006 Pearson Addison-Wesley. All rights reserved13-4 Public Inner Classes If an inner class is marked public, then it can be used outside of the outer class In the case of a nonstatic inner 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

© 2006 Pearson Addison-Wesley. All rights reserved13-5 Public Inner Classes In the case of a static inner class, the procedure is similar to, but simpler than, that for nonstatic inner classes OuterClass.InnerClass innerObject = new OuterClass.InnerClass(); –Note that all of the following are acceptable innerObject.nonstaticMethod(); innerObject.staticMethod(); OuterClass.InnerClass.staticMethod();

© 2006 Pearson Addison-Wesley. All rights reserved13-6 Tip: Referring to a Method of the Outer Class If a method is invoked in an inner class –If the inner class has no such method, then it is assumed to be an invocation of the method of that name in the outer class –If both the inner and outer class have a method with the same name, then it is assumed to be an invocation of the method in the inner class –If both the inner and outer class have a method with the same name, and the intent is to invoke the method in the outer class, then the following invocation must be used: OuterClassName.this.methodName() –using this inside an inner class refers to the object of the inner class

© 2006 Pearson Addison-Wesley. All rights reserved13-7 Nesting Inner Classes It is legal to nest inner classes within inner classes –The rules are the same as before, but the names get longer –Given class A, which has public inner class B, which has public inner class C, then the following is valid: A aObject = new A(); A.B bObject = aObject.new B(); A.B.C cObject = bObject.new C();

© 2006 Pearson Addison-Wesley. All rights reserved13-8 Inner Classes and Inheritance Given an OuterClass that has an InnerClass –Any DerivedClass of OuterClass will automatically have InnerClass as an inner class –In this case, the DerivedClass cannot override the InnerClass An outer class can be a derived class An inner class can be a derived class also

© 2006 Pearson Addison-Wesley. All rights reserved13-9 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. Not in the BOOK 

© 2006 Pearson Addison-Wesley. All rights reserved13-10 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 loc = new Local ( ) ; loc.test ( ) ; } public static void main ( String [ ] args ) { LocalClassExample obj = new LocalClassExample ( ); obj.method ( ) ; } Not in the BOOK 

© 2006 Pearson Addison-Wesley. All rights reserved13-11 Anonymous Classes It is a local class without a name If only one object has to be created from a class, and there is no need to name the class, then an anonymous class definition can be used –The class definition is embedded inside the expression with the new operator Anonymous class has no constructors It is either derived from a class, or implements an interface. Like: –AnInterface i = new AnInterface ( ) { // methods defs. … } –ASuperclass c = new ASuperclass(…) { // methods defs. … }

© 2006 Pearson Addison-Wesley. All rights reserved13-12 Anonymous Classes

© 2006 Pearson Addison-Wesley. All rights reserved13-13 Anonymous Classes

© 2006 Pearson Addison-Wesley. All rights reserved13-14 Anonymous Classes