(7) Inner Classes

(7) Inner Classes - Overview CMSC 132 Object-Oriented...

Info icon This preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
1 Java Inner Classes Department of Computer Science University of Maryland, College Park CMSC 132: Object-Oriented Programming II 2 Overview Classes Top-level classes, versus inner classes and nested classes Inner classes Iterator example Used inside outer class Anonymous inner classes Syntax Uses for GUIs Nested classes 3 Java classes Top-level classes: Are declared inside a package Are visible throughout the package, and perhaps further Are normally declared in their own file Public classes must be defined in their own file This is not required for other classes Inner and nested classes are declared inside another class (possibly inside a method in that class) Are normally used only in the outer (enclosing) class, but can have wider visibility 4 Inner classes An inner class is a just a class that is defined in the scope of another class It may be named or anonymous An outer class and an inner class inside it can directly access each other’s fields and methods (even if private)
Image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
5 Inner class example public class OuterClass { private int x; private class InnerClass { private int y; void f() { x= 1; } // access private field } void g() { InnerClass ic= new InnerClass(); ic.y= 2; // access private field } } 6 Inner class link to outer class An inner class instance has an association to an instance of its outer class It must be instantiated with an instance of the enclosing outer class It is tied to the outer class object at the moment of creation (this can't be changed) MyList MyIterator MyIterator MyList MyIterator MyList 7 Why? Inner classes are useful for: Private helper classes Logical grouping of functionality Data hiding Linkage to outer class An inner class object is tied to an outer class object Examples: Iterator for Java Collections ActionListener for Java GUI widgets 8 Iterator example Consider this MyList class: public class MyList { private Object[] a; private int size; } Suppose we want to make MyList implement Iterable Just ignore generic types for simplicity We need to be able to return an Iterator
Image of page 2
9 The
Image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern