26
Java OOP Review Professor Frank Kreimendahl School of Computing and Data Science Wentworth Institute of Technology Sep 8, 2021

Java OOP Review - 2000.witcompsci.com

  • Upload
    others

  • View
    9

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Java OOP Review - 2000.witcompsci.com

Java OOP Review

Professor Frank Kreimendahl

School of Computing and Data Science

Wentworth Institute of Technology

Sep 8, 2021

Page 2: Java OOP Review - 2000.witcompsci.com

ADTsADTs

Interfaces

Inheritance

abstractkeyword

Interfaces vs.AbstractClasses

ADTs

School of Computing and Data Science - 2/26 - Frank Kreimendahl | [email protected]

Page 3: Java OOP Review - 2000.witcompsci.com

ADTsADTs

Interfaces

Inheritance

abstractkeyword

Interfaces vs.AbstractClasses

Abstract Data Types (ADTs)

ADT features:

Uses encapsulation to limit access to data and methods

Describes what a Java class does without describing how

Supports the three fundamental operations of data: input,manipulation, output

Allows for conceptually similar data and methods to begrouped

Separates theory and implementation

Gives structure to data – hence the course name!

ADT requirements:

Never loses or forgets data (unless specified by an operation)

Never duplicates data (unless specified by an operation)

Functions consistently

School of Computing and Data Science - 3/26 - Frank Kreimendahl | [email protected]

Page 4: Java OOP Review - 2000.witcompsci.com

ADTs

InterfacesInterfaces

ATM Example

Interface Rules

implements keyword

Inheritance

abstractkeyword

Interfaces vs.AbstractClasses

Interfaces

School of Computing and Data Science - 4/26 - Frank Kreimendahl | [email protected]

Page 5: Java OOP Review - 2000.witcompsci.com

ADTs

InterfacesInterfaces

ATM Example

Interface Rules

implements keyword

Inheritance

abstractkeyword

Interfaces vs.AbstractClasses

Interface

An interface describes an ADT to the “user”

the method names

the parameter types to pass to methods

the types of values returned from the methods

the expected behavior of the methods

An interface is considered a contract – any ADT with an interfaceis guaranteed to have the interface’s methods.

School of Computing and Data Science - 5/26 - Frank Kreimendahl | [email protected]

Page 6: Java OOP Review - 2000.witcompsci.com

ADTs

InterfacesInterfaces

ATM Example

Interface Rules

implements keyword

Inheritance

abstractkeyword

Interfaces vs.AbstractClasses

Interfaces With Classes

A class that implements an interface provides code for theADT – a description how the data is handled

As long as the class satisfies the ADT contract, it may useany internal implementationThe class may optionally include other public fields ormethods as long as the interface is complete

• data fields not necessary for the interface• methods not required for the interface• constructors (which cannot be specified in the interface)

School of Computing and Data Science - 6/26 - Frank Kreimendahl | [email protected]

Page 7: Java OOP Review - 2000.witcompsci.com

ADTs

InterfacesInterfaces

ATM Example

Interface Rules

implements keyword

Inheritance

abstractkeyword

Interfaces vs.AbstractClasses

Interface Example: ATM

An automated teller machine machine (ATM machine) allows auser to perform specified banking operations. Every machineprovides operations to:

verify the user’s identity with a PIN

allow the user to choose a particular account of theirs

specify an amount of money to withdraw

display the result of an operation

display the balance of an account

Any Java class that implements an ATM machine must provide amethod for each of these operations.You can use an ATM without knowing the internal details.

School of Computing and Data Science - 7/26 - Frank Kreimendahl | [email protected]

Page 8: Java OOP Review - 2000.witcompsci.com

ADTs

InterfacesInterfaces

ATM Example

Interface Rules

implements keyword

Inheritance

abstractkeyword

Interfaces vs.AbstractClasses

ATM Interface

Declaration of a Java interface:

public interface ATM {/* method headers will go in here */

}

School of Computing and Data Science - 8/26 - Frank Kreimendahl | [email protected]

Page 9: Java OOP Review - 2000.witcompsci.com

ADTs

InterfacesInterfaces

ATM Example

Interface Rules

implements keyword

Inheritance

abstractkeyword

Interfaces vs.AbstractClasses

ATM Interface: Verify

Verify the user’s identity with a PIN:

/** Verifies a user's PIN.@param pin The user's PIN

*/boolean verifyPIN(String pin);

School of Computing and Data Science - 9/26 - Frank Kreimendahl | [email protected]

Page 10: Java OOP Review - 2000.witcompsci.com

ADTs

InterfacesInterfaces

ATM Example

Interface Rules

implements keyword

Inheritance

abstractkeyword

Interfaces vs.AbstractClasses

ATM Interface: Choose Account

Allow the user to choose a particular account of theirs

/** Allows the user to select an account.@return a String representing

the account selected*/String selectAccount();

School of Computing and Data Science - 10/26 - Frank Kreimendahl | [email protected]

Page 11: Java OOP Review - 2000.witcompsci.com

ADTs

InterfacesInterfaces

ATM Example

Interface Rules

implements keyword

Inheritance

abstractkeyword

Interfaces vs.AbstractClasses

ATM Interface: Withdraw

Specify an amount of money to withdraw:

/** Withdraws a specified amount of money@param account The account from which

the money comes@param amount The amount of money withdrawn@return whether or not the operation

is successful*/boolean withdraw(String account, double amount);

School of Computing and Data Science - 11/26 - Frank Kreimendahl | [email protected]

Page 12: Java OOP Review - 2000.witcompsci.com

ADTs

InterfacesInterfaces

ATM Example

Interface Rules

implements keyword

Inheritance

abstractkeyword

Interfaces vs.AbstractClasses

ATM Interface: Display Results

Display the result of an operation:

/** Displays the result of an operation@param account The account from which money

was withdrawn@param amount The amount of money withdrawn@param success Whether or not the withdrawal

took place*/void display(String account, double amount,

boolean success);

School of Computing and Data Science - 12/26 - Frank Kreimendahl | [email protected]

Page 13: Java OOP Review - 2000.witcompsci.com

ADTs

InterfacesInterfaces

ATM Example

Interface Rules

implements keyword

Inheritance

abstractkeyword

Interfaces vs.AbstractClasses

ATM Interface: Display Balance

Display the balance of an account:

/** Displays an account balance@param account The account selected

*/void showBalance(String account);

School of Computing and Data Science - 13/26 - Frank Kreimendahl | [email protected]

Page 14: Java OOP Review - 2000.witcompsci.com

ADTs

InterfacesInterfaces

ATM Example

Interface Rules

implements keyword

Inheritance

abstractkeyword

Interfaces vs.AbstractClasses

ATM Interface: Full Interface

The full ATM interface:

(comments removed for brevity)public interface ATM {

boolean verifyPIN(String pin);String selectAccount();boolean withdraw(String account, double amount);void display(String account, double amount,

boolean success);void showBalance(String account);

}

School of Computing and Data Science - 14/26 - Frank Kreimendahl | [email protected]

Page 15: Java OOP Review - 2000.witcompsci.com

ADTs

InterfacesInterfaces

ATM Example

Interface Rules

implements keyword

Inheritance

abstractkeyword

Interfaces vs.AbstractClasses

Interface Rules

Only method headings are shown

Interface methods are considered abstract methodsabstract methods must be defined in every class thatimplements the interface

constants may be declared in an interface, using a normalJava variable declaration/assignment

School of Computing and Data Science - 15/26 - Frank Kreimendahl | [email protected]

Page 16: Java OOP Review - 2000.witcompsci.com

ADTs

InterfacesInterfaces

ATM Example

Interface Rules

implements keyword

Inheritance

abstractkeyword

Interfaces vs.AbstractClasses

implements Keyword

If you want a class to implement an interface, use theimplements keyword:

public class ATMbankAmerica implements ATM {

A class may implement multiple interfaces – interface names arecomma-separated

School of Computing and Data Science - 16/26 - Frank Kreimendahl | [email protected]

Page 17: Java OOP Review - 2000.witcompsci.com

ADTs

Interfaces

InheritanceExample

abstractkeyword

Interfaces vs.AbstractClasses

Inheritance

School of Computing and Data Science - 17/26 - Frank Kreimendahl | [email protected]

Page 18: Java OOP Review - 2000.witcompsci.com

ADTs

Interfaces

InheritanceExample

abstractkeyword

Interfaces vs.AbstractClasses

A Human is a Mammal – with more specific details

Human has all the data fields and methods defined byMammal

Mammal is the superclass of Human

Human is a subclass of Mammal

Human can define more fields and methods (or redefineMammal methods)

A VesperBat is a Mammal – with different specifics than aHuman

Both Human and VesperBat extend the fields/methods ofMammal, but in different ways

School of Computing and Data Science - 18/26 - Frank Kreimendahl | [email protected]

Page 19: Java OOP Review - 2000.witcompsci.com

ADTs

Interfaces

Inheritance

abstractkeywordAbstract Classes

Purpose

Example

Interfaces vs.AbstractClasses

abstract keyword

School of Computing and Data Science - 19/26 - Frank Kreimendahl | [email protected]

Page 20: Java OOP Review - 2000.witcompsci.com

ADTs

Interfaces

Inheritance

abstractkeywordAbstract Classes

Purpose

Example

Interfaces vs.AbstractClasses

Abstract Classes

An abstract class has the keyword abstract in its heading

An abstract class cannot be initialized using the newkeyword

An abstract class may declare abstract methods (similar tomethod headings in an interface)

Any subclass of an abstract class must implement theabstract methods

School of Computing and Data Science - 20/26 - Frank Kreimendahl | [email protected]

Page 21: Java OOP Review - 2000.witcompsci.com

ADTs

Interfaces

Inheritance

abstractkeywordAbstract Classes

Purpose

Example

Interfaces vs.AbstractClasses

Purpose of Abstract Classes

Use an abstract class as a base class when you have twosubclasses that have overlapping attributes

Declare common fields and methods in the base class

Declare unique fields and methods in each subclassseparately

School of Computing and Data Science - 21/26 - Frank Kreimendahl | [email protected]

Page 22: Java OOP Review - 2000.witcompsci.com

ADTs

Interfaces

Inheritance

abstractkeywordAbstract Classes

Purpose

Example

Interfaces vs.AbstractClasses

Abstract Class Example

public abstract class Food {public final String name;private double calories;// Actual methodspublic double getCalories () {

return calories;}protected Food (String name, double calories) {

this.name = name;this.calories = calories;

}// Abstract methodspublic abstract double percentProtein();public abstract double percentFat();public abstract double percentCarbs();

}

School of Computing and Data Science - 22/26 - Frank Kreimendahl | [email protected]

Page 23: Java OOP Review - 2000.witcompsci.com

ADTs

Interfaces

Inheritance

abstractkeyword

Interfaces vs.AbstractClassesComparison

Usage

Class Hierarchy

Interfaces vs. Abstract Classes

School of Computing and Data Science - 23/26 - Frank Kreimendahl | [email protected]

Page 24: Java OOP Review - 2000.witcompsci.com

ADTs

Interfaces

Inheritance

abstractkeyword

Interfaces vs.AbstractClassesComparison

Usage

Class Hierarchy

Comparison of Interface/Abstract Class

A Java interface can declare methods but not implementthem

A Java abstract class can either implement methods ordeclare them abstractInterfaces can only have constant values, while classes canhave variable fields

Neither can be instantiated

Abstract classes can have constructors defined

School of Computing and Data Science - 24/26 - Frank Kreimendahl | [email protected]

Page 25: Java OOP Review - 2000.witcompsci.com

ADTs

Interfaces

Inheritance

abstractkeyword

Interfaces vs.AbstractClassesComparison

Usage

Class Hierarchy

Usage

Interfaces leave all of the implementation details to bedefined in an implementing class

Abstract methods leave some of the implementation detailsfor an extending class

A class can extend 0 or 1 superclass

A class can implement 0 or more interfaces

An interface cannot implement another interface but it canextend another interface (by adding more required methods)

School of Computing and Data Science - 25/26 - Frank Kreimendahl | [email protected]

Page 26: Java OOP Review - 2000.witcompsci.com

ADTs

Interfaces

Inheritance

abstractkeyword

Interfaces vs.AbstractClassesComparison

Usage

Class Hierarchy

Class Hierarchy Example

A Shape class with three subclasses that extend Shape

School of Computing and Data Science - 26/26 - Frank Kreimendahl | [email protected]