220 likes | 343 Views
This comprehensive guide by Maj. Joel Young from the Air Force Institute of Technology explores core concepts of object-oriented programming in Java, focusing on interfaces, copying/cloning mechanisms, and inner classes. Understand how interfaces enforce method implementations and permit multiple inheritance in Java, while learning about shallow and deep copying. The guide also delves into inner classes, detailing their purpose and functionality within the context of outer classes. With practical examples, this resource is invaluable for enhancing your Java programming skills.
E N D
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
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
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; } }
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
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
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; } } ... }
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
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; } }
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.
Stack • Push 3, Push 5, Push 2, Pop Push: Pop: 3 5 2 Top 3 2 5 3 5 3
Stack Implementation Issues: • Which end is the top? • Where does “top” point to? • What is the cost of the operations?
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 }
Stack Implementation • Each stack operation take O(1) time. • Array implementation limited by declared size of the array. (Look at Vector in API)
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.
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
Binary Search Tree Successor 17 15 14 13 root Successor(15): Successor(14): 17 12 15 12 8 7
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
Homework • Look in API for things like Stack and List and Vector • Questions for COMACC • Feedback will come on your requirements specification