Presentation is loading. Please wait.

Presentation is loading. Please wait.

16-Aug-15 Air Force Institute of Technology Electrical and Computer Engineering Object-Oriented Programming in Java Topic : Interfaces, Copying/Cloning,

Similar presentations


Presentation on theme: "16-Aug-15 Air Force Institute of Technology Electrical and Computer Engineering Object-Oriented Programming in Java Topic : Interfaces, Copying/Cloning,"— Presentation transcript:

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

2 Air Force Institute of Technology Electrical and Computer Engineering 16-Aug-152 Object-Oriented Programming Design 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 Air Force Institute of Technology Electrical and Computer Engineering 16-Aug-153 Object-Oriented Programming Design 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 Air Force Institute of Technology Electrical and Computer Engineering 16-Aug-154 Object-Oriented Programming Design 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 Air Force Institute of Technology Electrical and Computer Engineering 16-Aug-155 Object-Oriented Programming Design 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 Air Force Institute of Technology Electrical and Computer Engineering 16-Aug-156 Object-Oriented Programming Design Copying and Cloning

7 Air Force Institute of Technology Electrical and Computer Engineering 16-Aug-157 Object-Oriented Programming Design Shallow Copy

8 Air Force Institute of Technology Electrical and Computer Engineering 16-Aug-158 Object-Oriented Programming Design Deep Copy

9 Air Force Institute of Technology Electrical and Computer Engineering 16-Aug-159 Object-Oriented Programming Design 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 Air Force Institute of Technology Electrical and Computer Engineering 16-Aug-1510 Object-Oriented Programming Design 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 Air Force Institute of Technology Electrical and Computer Engineering 16-Aug-1511 Object-Oriented Programming Design 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 Air Force Institute of Technology Electrical and Computer Engineering 16-Aug-1512 Object-Oriented Programming Design 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 Air Force Institute of Technology Electrical and Computer Engineering 16-Aug-1513 Object-Oriented Programming Design Stack  Push 3, Push 5, Push 2, Pop Top 3 3 25 Push:352 3 5 Pop:

14 Air Force Institute of Technology Electrical and Computer Engineering 16-Aug-1514 Object-Oriented Programming Design Stack Implementation Issues:  Which end is the top?  Where does “top” point to?  What is the cost of the operations?

15 Air Force Institute of Technology Electrical and Computer Engineering 16-Aug-1515 Object-Oriented Programming Design 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 Air Force Institute of Technology Electrical and Computer Engineering 16-Aug-1516 Object-Oriented Programming Design Stack Implementation  Each stack operation take O(1) time.  Array implementation limited by declared size of the array. (Look at Vector in API)

17 Air Force Institute of Technology Electrical and Computer Engineering 16-Aug-1517 Object-Oriented Programming Design  BST Property: All elements stored in the left subtree of a node with value K have values = K. Binary Search Tree

18 Air Force Institute of Technology Electrical and Computer Engineering 16-Aug-1518 Object-Oriented Programming Design 15 Binary Search Tree Insert root Insert:12 8 8  Go search for key until run off the end of the tree  Put new key there 15 9 9

19 Air Force Institute of Technology Electrical and Computer Engineering 16-Aug-1519 Object-Oriented Programming Design 17 15 Binary Search Tree Successor root 12 8 7 1413 Successor(15):12Successor(14):15

20 Air Force Institute of Technology Electrical and Computer Engineering 16-Aug-1520 Object-Oriented Programming Design 1414.5 Delete(17)Delete(15) 1715 Binary Search Tree Successor root 12 8 7 13 Case I: Node is leaf. Just delete it. Case II: Node has only one child. Splice out the node Delete(14) 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. Successor(14.5) 14

21 Air Force Institute of Technology Electrical and Computer Engineering 16-Aug-1521 Object-Oriented Programming Design Homework Look in API for things like Stack and List and Vector Questions for COMACC –Feedback will come on your requirements specification


Download ppt "16-Aug-15 Air Force Institute of Technology Electrical and Computer Engineering Object-Oriented Programming in Java Topic : Interfaces, Copying/Cloning,"

Similar presentations


Ads by Google