20-CS-694 Advanced Programming Techniques Spring 2012
OOP

Interfaces, Exceptions, Graphics, Animation, Threads, Reflection, Networking, RMI, JDBC, JNI

Java Source     Inner.java     All lectures
What is going on here?

Why inner classes can be tricky

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.applet.*;

interface S {
   public int getN();
   public void setN(int n);
}

abstract class B implements S {
   public int n;
   public B () { n = 5; }
   public int getN () { return n; }
}

abstract class C implements S {
   public int n;
   public C (int n) { this.n = n; }
   public int getN () { return n; }
}

class D implements S {
   public int n;
   public D () { n = 5; }
   public int getN () { return n+2; }
   public void setN (int n) { this.n = n+2; }
}

class A {
   Inner inner;

   public A (Inner inner) { this.inner = inner; }

   B b = new B() {  public void setN (int n) { this.n = n; } };
        
   // Yikes!
   // Two different type objects of the same class allowed!!
   // Yikes!
   B b1 = new B() {  public void setN (int n) { this.n = n+1; } };
        
   C c = new C(16) {  public void setN (int n) { this.n = n+1; } };
      
   D d = new D() {  public int getN () { return n+3; }  };
        
   public void doit () {
      b.setN(10);
      inner.text.append(b.getN()+" "+b1.getN()+" "+c.getN()+" "+d.getN()+"\n");
   }
}

public class Inner extends Applet implements ActionListener {
   JTextArea text;
   JButton button;

   public void init () {
      setLayout(new FlowLayout());
      add(new JScrollPane(text = new JTextArea(6,30)));
      add(button = new JButton("Press Me"));
      button.addActionListener(this);
      setBackground(new Color(255,255,223));
      text.setFont(new Font("TimesRoman", Font.PLAIN, 18));
   }

   public void actionPerformed (ActionEvent evt) { (new A(this)).doit(); }
}