Inner Classes. 2 Simple Uses of Inner Classes Inner classes are classes defined within other classes The class that includes the inner class is called.

Презентация:



Advertisements
Похожие презентации
1 © Luxoft Training 2012 Inner and anonymous classes.
Advertisements

Operator Overloading Customised behaviour of operators Chapter: 08 Lecture: 26 & 27 Date:
Unit II Constructor Cont… Destructor Default constructor.
Multiples Michael Marchenko. Definition In mathematics, a multiple is the product of any quantity and an integer. in other words, for the quantities a.
1/27 Chapter 9: Template Functions And Template Classes.
A class is just a collection of variables--often of different types--combined with a set of related functions. The variables in the class are referred.
MONEY MAKES THE WORLD GO ROUND……. Whenever people pay for goods or services, they use some form of money. Money can be almost anything, as long as everyone.
1/30 Chapter 8: Dynamic Binding And Abstract classes.
11 BASIC DRESS-UP FEATURES. LESSON II : DRESS UP FEATURES 12.
2005 Pearson Education, Inc. All rights reserved. 1 Object-Oriented Programming: Interface.
HPC Pipelining Parallelism is achieved by starting to execute one instruction before the previous one is finished. The simplest kind overlaps the execution.
© Luxoft Training 2013 Annotations. © Luxoft Training 2013 Java reflection / RTTI // given the name of a class, get a "Class" object that // has all info.
Data Types in C. A Data Type A data type is –A set of values AND –A set of operations on those values A data type is used to –Identify the type of a variable.
Operators and Arithmetic Operations. Operators An operator is a symbol that instructs the code to perform some operations or actions on one or more operands.
AVL-Trees COMP171 Fall AVL Trees / Slide 2 Balanced binary tree * The disadvantage of a binary search tree is that its height can be as large as.
© 2009 Avaya Inc. All rights reserved.1 Chapter Two, Voic Pro Components Module Two – Actions, Variables & Conditions.
© 2005 Cisco Systems, Inc. All rights reserved. BGP v Customer-to-Provider Connectivity with BGP Connecting a Multihomed Customer to Multiple Service.
Correlation. In statistics, dependence refers to any statistical relationship between two random variables or two sets of data. Correlation refers to.
RLC circuit. An RLC circuit (or LCR circuit) is an electrical circuit consisting of a resistor, an inductor, and a capacitor, connected in series or in.
SPLAY TREE The basic idea of the splay tree is that every time a node is accessed, it is pushed to the root by a series of tree rotations. This series.
Транксрипт:

Inner Classes

2 Simple Uses of Inner Classes Inner classes are classes defined within other classes The class that includes the inner class is called the outer class There is no particular location where the definition of the inner class (or classes) must be place within the outer class Placing it first or last, however, will guarantee that it is easy to find

3 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 The name of an inner class may be reused for something else outside 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

Aug 7, Inner/Outer Classes public class Outer { private class Inner { // inner class instance variables // inner class methods } // end of inner class definition // outer class instance variables // outer class methods }

5 Simple Uses of Inner Classes There are two main advantages to inner classes They can make the outer class more self-contained since they are defined inside a class Both of their methods have access to each other's private methods and instance variables

6 Have Access to Each Other's 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 Essentially, the inner class has a hidden reference to 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 (nonstatic) method of the inner class as long as an object of the inner class is used as a calling object Within the definition of the inner or outer classes, the modifiers public and private are equivalent

7 Class with an Inner Class public class BankAccount{ private class Money{ private double amount; public Money(double amt){ amount = amt; } public double getAmount() { return amount; } public void addIn (Money secondAmount) { amount = amount + secondAmount.getAmount(); }

8 Class with an Inner Class

9 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()

10 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

11 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();

12 Public Money Inner Class If the Money inner class in the BankAccount example was defined as public, we can create and use objects of type Money outside the BankAccount class. // this is okay in main( ) BankAccount account = new BankAccount( ); BankAccount.Money amt= account.new Money(41.99); System.out.println( amt.getAmount( ) ); // but NOT this – why not?? System.out.println( amt.getBalance( ) );

13 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 Static inner class has no connection to an object of the outer class, within a static inner class method Instance variables of the outer class cannot be referenced Nonstatic methods of the outer class cannot be invoked

14 Static Inner Classes 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 There are certain situations, 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

15 Multiple Inner Classes A class can have as many inner classes as it needs. Inner classes have access to each others private members as long as an object of the other inner class is used as the calling object.

16 The.class File for an Inner Class Compiling any class in Java produces a.class file named ClassName.class Compiling a class with one (or more) inner classes causes both (or more) classes to be compiled, and produces two (or more).class files Such as ClassName.class and ClassName$InnerClassName.class

17 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();

18 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

19 Anonymous Classes Is an un-named inner class If an object is to be created, but there is no need to name the object's class, then an anonymous class definition can be used The class definition is embedded inside the expression with the new operator An anonymous class is an abbreviated notation for creating a simple local object within an expression, simply by wrapping the desired code in a "new" expression.

20 Anonymous Classes

21 Anonymous Classes