1 / 21

Object-Oriented Programming in Java Topic : Interfaces, Copying/Cloning, Inner Classes

Object-Oriented Programming in Java Topic : Interfaces, Copying/Cloning, Inner Classes. Maj Joel Young Joel.Young@afit.edu. Maj Joel Young. Interface Concepts. Interface – a set of requirements for classes that want to conform to the interface

adah
Download Presentation

Object-Oriented Programming in Java Topic : Interfaces, Copying/Cloning, Inner Classes

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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Air Force Institute of Technology Electrical and Computer Engineering Object-Oriented Programming in JavaTopic : Interfaces, Copying/Cloning, Inner Classes Maj Joel Young Joel.Young@afit.edu Maj Joel Young

  2. Interface Concepts • Interface – a set of requirements for classes that want to conform to the interface • Interface is a promise that your class will implement certain methods with certain signatures • The way in which the methods is implemented is up to the class • Java's way of dealing with multiple inheritance • Inheritance restricted to one parent class • A class can implement any number of interfaces • For a class to implement an interface • Declare that the class intends to implement the given interface • Supply definitions for all methods in the interface

  3. Interface Example public interface Comparable { int compareTo( Object other ); } class Employee implements Comparable { . . . public int compareTo( Object otherObject) { if (salary < other.salary) return –1; else if (salary > other.salary) return 1; else return 0; } }

  4. Interface vs Abstract Class • Why not use abstract class? abstract class Comparable { public abstract int compareTo(Object other); } class Employee extends Comparable; • What if employee already extends another class? class Employee extends Person, Comparable // ERROR • But a class can implement multiple interfaces… class Employee extends Person implements Comparable, Cloneable

  5. Properties of Interfaces • Can declare an object to be of an interface type Comparable empX = new Employee(…);Employee empY = new Employee(…);if (empX.compare(empY) < 0 ) … • Interfaces can be extended to create another interface, inheriting properties of the parent interface public interface MoreComparable extends Comparable • No instance fields or static methods in an interface • Constants can be specified in an interface public interface Powered extends Moveable{ double milesPerGallon(); double SPEED_LIMIT = 95;} • Implementing class must implement all methods of interface

  6. Copying and Cloning

  7. Shallow Copy

  8. Deep Copy

  9. Copying and Cloning in Java class Employee implements Cloneable { public Employee(String n, double s) { name = n; salary = s; } public Object clone() { try { // call Object.clone() Employee cloned = (Employee)super.clone(); // clone mutable fields cloned.payDay = (GregorianCalendar)payDay.clone(); return cloned; } catch (CloneNotSupportedException e) { return null; } } ... }

  10. Inner Classes • A class that is defined inside another class • Has implicit reference to the outer class object that instantiated it -- this • Can access the implementation of the object that created it • Can be hidden from other classes in the same package • Can access local variables of scope in which they are defined • Why • Helper objects (e.g. property editor) • Can control private implementation of a class, without giving that access to other classes • Can have own state

  11. Inner Classes class BankAccount { ... private double balance; // Inner Class private class InterestAdder implements ActionListener { ... public void actionPerformed(ActionEvent event) { // update interest double interest = balance * rate / 100; balance += interest; ... } private double rate; } }

  12. Stack • A stack is a restricted form of a linked list. • Accessible only through the front of the list • LIFO: Last In, First Out. • Operators: • Insert: push • Remove: pop • The accessible element is called top.

  13. Stack • Push 3, Push 5, Push 2, Pop Push: Pop: 3 5 2 Top 3 2 5 3 5 3

  14. Stack Implementation Issues: • Which end is the top? • Where does “top” point to? • What is the cost of the operations?

  15. Stack Implementation // Stack abstract class This could be an interface too abstract class Stack { // Reinitialize the stack abstract public void clear(); // Push an element onto the top of the stack abstract public boolean push( Object); // Remove the element at the top of the stack abstract public void pop( ); // Get the top element in the stack abstract public Object top( ); … // Array based implementation int size; // Maximum size of stack int top; // Index for top element Object[] stack; // Array holding elements }

  16. Stack Implementation • Each stack operation take O(1) time. • Array implementation limited by declared size of the array. (Look at Vector in API)

  17. Binary Search Tree • BST Property: All elements stored in the left subtree of a node with value K have values < K. All elements stored in the right subtree of a node with value K have values >= K.

  18. Binary Search Tree Insert Insert: 15 12 8 9 root 12 8 15 9 • Go search for key until run off the end of the tree • Put new key there

  19. Binary Search Tree Successor 17 15 14 13 root Successor(15): Successor(14): 17 12 15 12 8 7

  20. Binary Search Tree Successor 17 15 13 root Delete(17) Delete(15) Delete(14) Successor(14.5) 12 Case I: Node is leaf. Just delete it. Case II: Node has only one child. Splice out the node 8 Case III: Node has two children. Delete successor of node and replace node’s key with successor’s key. Note that successor always satisfies Case I or Case II. 14.5 14 7 14.5 14

  21. Homework • Look in API for things like Stack and List and Vector • Questions for COMACC • Feedback will come on your requirements specification

More Related