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

21
Jun 15, 2022 Air Force Institute of Technology Electrical and Computer Engineering Object-Oriented Programming in Java Topic : Interfaces, Copying/Cloning, Inner Classes Maj Joel Young [email protected] Maj Joel Young

description

Object-Oriented Programming in Java Topic : Interfaces, Copying/Cloning, Inner Classes. Maj Joel Young [email protected]. Maj Joel Young. Interface Concepts. Interface – a set of requirements for classes that want to conform to the interface - PowerPoint PPT Presentation

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

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

Apr 21, 2023

Air Force Institute of TechnologyElectrical and Computer Engineering

Object-Oriented Programming in Java

Topic : Interfaces, Copying/Cloning, Inner Classes

Maj Joel [email protected]

Maj Joel Young

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

Air Force Institute of TechnologyElectrical and Computer EngineeringApr 21, 2023 2

Object-Oriented Programming

DesignInterface 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

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

Air Force Institute of TechnologyElectrical and Computer EngineeringApr 21, 2023 3

Object-Oriented Programming

DesignInterface 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; }}

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

Air Force Institute of TechnologyElectrical and Computer EngineeringApr 21, 2023 4

Object-Oriented Programming

DesignInterface 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

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

Air Force Institute of TechnologyElectrical and Computer EngineeringApr 21, 2023 5

Object-Oriented Programming

DesignProperties 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

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

Air Force Institute of TechnologyElectrical and Computer EngineeringApr 21, 2023 6

Object-Oriented Programming

DesignCopying and Cloning

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

Air Force Institute of TechnologyElectrical and Computer EngineeringApr 21, 2023 7

Object-Oriented Programming

DesignShallow Copy

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

Air Force Institute of TechnologyElectrical and Computer EngineeringApr 21, 2023 8

Object-Oriented Programming

DesignDeep Copy

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

Air Force Institute of TechnologyElectrical and Computer EngineeringApr 21, 2023 9

Object-Oriented Programming

DesignCopying 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; } } ...}

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

Air Force Institute of TechnologyElectrical and Computer EngineeringApr 21, 2023 10

Object-Oriented Programming

DesignInner 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

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

Air Force Institute of TechnologyElectrical and Computer EngineeringApr 21, 2023 11

Object-Oriented Programming

DesignInner 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; }}

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

Air Force Institute of TechnologyElectrical and Computer EngineeringApr 21, 2023 12

Object-Oriented Programming

DesignStack

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.

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

Air Force Institute of TechnologyElectrical and Computer EngineeringApr 21, 2023 13

Object-Oriented Programming

DesignStack

Push 3, Push 5, Push 2, Pop

Top 3

3

25

Push: 352

3

5

Pop:

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

Air Force Institute of TechnologyElectrical and Computer EngineeringApr 21, 2023 14

Object-Oriented Programming

DesignStack

Implementation Issues:

Which end is the top?

Where does “top” point to?

What is the cost of the operations?

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

Air Force Institute of TechnologyElectrical and Computer EngineeringApr 21, 2023 15

Object-Oriented Programming

DesignStack Implementation

// Stack abstract class This could be an interface tooabstract class Stack {

// Reinitialize the stackabstract public void clear();

// Push an element onto the top of the stackabstract public boolean push( Object);

// Remove the element at the top of the stackabstract public void pop( );

// Get the top element in the stackabstract public Object top( );

…// Array based implementation int size; // Maximum size of stack int top; // Index for top element Object[] stack; // Array holding elements}

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

Air Force Institute of TechnologyElectrical and Computer EngineeringApr 21, 2023 16

Object-Oriented Programming

DesignStack Implementation

Each stack operation take O(1) time.

Array implementation limited by declared size of the array. (Look at Vector in API)

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

Air Force Institute of TechnologyElectrical and Computer EngineeringApr 21, 2023 17

Object-Oriented Programming

Design

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

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

Air Force Institute of TechnologyElectrical and Computer EngineeringApr 21, 2023 18

Object-Oriented Programming

Design

15

Binary Search Tree Insert

rootInsert: 1212

8

8

Go search for key until run off the end of the tree

Put new key there

15

9

9

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

Air Force Institute of TechnologyElectrical and Computer EngineeringApr 21, 2023 19

Object-Oriented Programming

Design

17

17

15

Binary Search Tree Successor

root12

8

7 14

13

Successor(15): 12Successor(14): 15

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

Air Force Institute of TechnologyElectrical and Computer EngineeringApr 21, 2023 20

Object-Oriented Programming

Design

1414.5

14.5

Delete(17)Delete(15)

17

15

Binary Search Tree Successor

root12

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

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

Air Force Institute of TechnologyElectrical and Computer EngineeringApr 21, 2023 21

Object-Oriented Programming

DesignHomework

• Look in API for things like Stack and List and Vector• Questions for COMACC

– Feedback will come on your requirements specification