inner classes in java n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
Inner Classes in Java PowerPoint Presentation
Download Presentation
Inner Classes in Java

Loading in 2 Seconds...

play fullscreen
1 / 18

Inner Classes in Java - PowerPoint PPT Presentation


  • 138 Views
  • Uploaded on

Inner Classes in Java. CMSC 432 Shon Vick. Conceptual Structure. package p 1    class A 1    class A 2       ...    class A n ... package n without inner classes. package p1    class A 1    class A 2      class A 2_1 class A 2_2   ... inner classes. Example.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Inner Classes in Java' - mead


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
inner classes in java

Inner Classes in Java

CMSC 432

Shon Vick

conceptual structure
Conceptual Structure

package p1

   class A1   class A2      ...   class An...package n

withoutinner classes

package p1    class A1    class A2

     class A2_1 classA2_2

  ...

inner classes

example
Example

public x + z;

} class OC { // outer class

private int x = 2;

public class IC { // inner class

int z = 4;

public int getSum() {

return

}

}

slide4
Why?
  • Logical grouping of functionality
  • Data hiding
    • Yet another encapsulation mechanism
  • Linkage to outer class
    • We’ll see about this later
simple motivating example
Simple Motivating Example

public class MyList {

private Object [ ] a;

private int size;

}

Say we want to iterate over the

elements of this guy …

1 st design
Problems

Need to maintain reference to MyList

Need to access private data in MyList

1st Design

public class MyIterator {

private MyList list;

private int pos;

MyIterator(MyList list) {

this.list = list;

pos = 0;

}

public boolean hasNext() {

return (pos < list.size);

}

public Object next() {

return list.a[pos++];

}

}

2 nd try using inner classes
2nd Try – Using Inner Classes

public class MyList {

private Object [ ] a;

private int size;

public class MyIterator {

private int pos;

MyIterator() { pos = 0; }

public boolean hasNext() {

return (pos < size);

}

public Object next() {

return a[pos++];

}

}

}

inner classes example
Inner Classes Example

public class OC { // outer class

private int x = 2;

public class IC { // inner class

int z = 4;

public int getSum() {

return x + z;

}

}

}

mechanics and syntax
Mechanics and Syntax
  • Class referencing syntax
    • OuterClass.InnerClass
  • Example

OC oc = new OC( );

OC.IC ic;name of inner class

ic = new OC.IC()doesn’t work!

ic = oc.new IC();instantiates inner class -ic now will "know about" oc, but not vice versa

ic.getSum()yields 6 can access private x in oc!

accessing outer scope
Accessing Outer Scope

public class OC { outer class

int x = 2;

public class IC { inner class

int x = 6;

public void getX() { inner class method

int x = 8;

System.out.println( x ); prints 8

System.out.println( this.x ); prints 6

System.out.println( OC.this.x ); prints 2

}

}

}

instantiating inner class
Instantiating Inner Class
  • Common Technique
    • Outer class method returns instance of inner class
    • Used by Java Collections Library for Iterators
  • Code Example
    • public class MyList {
    • public class IC implements Iterator { … }
    • public Iterator iterator() {
    • return new IC(); // creates instance of IC
    • }
    • }
    • MyList m = new MyList();
    • Iterator it = m.iterator();
listeners
Listeners

Event

Source

  • Each event is represented by an object that gives information about the event and identifies the event source.
  • Event sources are typically components.
  • Each event source can have multiple listeners.Conversely, a single listener can register with multiple event sources.
making the applet the listener
Making the Applet the Listener

import java.applet.*;

import java.awt.*;

import java.awt.event.*;

public class Scribble extends Applet

implements MouseListener, MouseMotionListener {

private int last_x, last_y;

public void init() {

this.addMouseListener(this);

this.addMouseMotionListener(this);

} …

mouse events
Mouse Events

public void mousePressed(MouseEvent e) {

last_x = e.getX();

last_y = e.getY();

}

public void mouseDragged(MouseEvent e) {

Graphics g = this.getGraphics();

int x = e.getX(), y = e.getY();

g.drawLine(last_x, last_y, x, y);

last_x = x; last_y = y;

}

need to implement the whole interface
Need to implement the whole Interface

// The other, unused methods of the MouseListener interface.

public void mouseReleased(MouseEvent e) {;}

public void mouseClicked(MouseEvent e) {;}

public void mouseEntered(MouseEvent e) {;}

public void mouseExited(MouseEvent e) {;}

// The other method of the MouseMotionListener interface.

public void mouseMoved(MouseEvent e) {;}

}

using adapters and inner classes to handle events
Using Adapters and Inner Classes to Handle Events
  • The empty method bodies of the MouseListener interface from the previous example make the code harder to read and maintain
    • Adapter classes are a means around this problem
    • An adaptor allows you to override just the methods for the events of interest
adapters and anonymous classes
Adapters and Anonymous Classes

public void init() {

// Define, instantiate, and register a MouseListener object.

this.addMouseListener(new MouseAdapter() {

public void mousePressed(MouseEvent e) {

last_x = e.getX();

last_y = e.getY(); More Info on Innerclasses

} } );

references
References
  • http://www.cs.umd.edu/class/spring2005/cmsc132/lecs/lec27.pdf
  • http://java.sun.com/docs/books/tutorial/java/javaOO/nested.html