Inheritance interface

A Java Class

class Point {
   public double x, y; 
   public static Point origin = new Point(0,0); 
     // This always refers to an object at (0,0) 
   Point(double x_value, double y_value) {
      x = x_value; 
      y = y_value; 
   }
   public void clear() {
      this.x = 0; 
      this.y = 0; 
   }
   public double distance(Point that) {
      double xDiff = x - that.x; 
      double yDiff = y - that.y; 
      return Math.sqrt(xDiff * xDiff + yDiff * yDiff);
   }
}

Extending a Class: Inheritance

class Pixel extends Point {
  Color color; 

  public void clear() {
     super.clear(); 
     color = null; 
  }
}

Interfaces

interface Lookup {
   /** Return the value associated with the name, or 
    * null if there is no such value */ 
    Object find(String name); 
}

void processValues(String[] names, Lookup table) {
   for (int i = 0; i ! names.length; i++) {
       Object value = table.find(names[i]); 
       if (value != null) 
          processValue(names[i], value); 
   }
}

class SimpleLookup implements Lookup {
   private String[] Names; 
   private Object[] Values; 

   public Object find(String name) {
      for (int i = 0; i < Names.length; i++) {
          if (Names[i].equals(name)) 
             return Values[i]; 
      }
      return null; // not found 
   }
   // ... 
}

Creating Threads in Java

public class PingPONG extends Thread { 
    private String word; // What word to print  
    private int delay; // how long to pause  
    public PingPONG(String whatToSay, int delayTime) { 
        word = whatToSay;  
        delay = delayTime;  
    } 
    public void run() { 
        try { 
            for (;;) { 
                System.out.print(word + " ");  
                sleep(delay); // wait until next time  
            } 
        } catch (InterruptedException e) { 
            return; // end this thread;  
        } 
    } 
    public static void main(String[] args) { 
        new PingPONG("Ping", 33).start(); // 1/30 second  
        new PingPONG("PONG",100).start(); // 1/10 second  
    } 
} 

Two Synchronization Methods

class Account {
    private double balance; 
    Public Account(double initialDeposit) {
       balance = initialDeposit; 
    }
    public synchronized double getBalance() {
       return balance; 
    }
    public synchronized viod deposit(double amount) {
       balance += amont; 
    }
}

/** make all elements in the array non-negative */ 
public static void abs(int[] values) {
    synchronized (values) {
       for (int i = 0; i < values.length; i++) {
          if (values[i] < 0)
             values[i] = -values[i]; 
       }
    }
}

2 Response to "Inheritance interface"

  1. shanmuga says:
    2 January 2012 at 08:38

    amazing website continue

  2. Anonymous Says:
    6 January 2012 at 06:17

    TAMILMUSIC

Post a Comment

comments