Objectives Packages Final Abstract Classes Interfaces Sep 14, - - PDF document

objectives
SMART_READER_LITE
LIVE PREVIEW

Objectives Packages Final Abstract Classes Interfaces Sep 14, - - PDF document

9/14/20 Objectives Packages Final Abstract Classes Interfaces Sep 14, 2020 Sprenkle - CSCI209 1 1 Review How does Java pass parameters? How do we make a class inherit from a parent class? How does a class refer to


slide-1
SLIDE 1

9/14/20 1

Objectives

  • Packages
  • Final
  • Abstract Classes
  • Interfaces

Sep 14, 2020 Sprenkle - CSCI209 1

1

Sep 14, 2020 Sprenkle - CSCI209 2

Review

  • How does Java pass parameters?
  • How do we make a class inherit from a parent class?
  • How does a class refer to its parent class?
  • What does a class inherit from its parent class?

Ø What is not inherited?

  • What are the access modifiers, ordered from least

restrictive to most restrictive?

  • How can we verify that an object variable is a certain

type?

  • How can we specify that an object variable has a different

type (e.g., a derived type)?

  • How does Java decide which method to call on an object?

Ø Example: chicken[1].feed();

2

slide-2
SLIDE 2

9/14/20 2

Review

  • Designing classes: When should you make a

variable/field

Ø Local vs instance vs static? Ø Private vs protected vs public?

  • Inheritance in game code

Ø Javadocs

Sep 14, 2020 Sprenkle - CSCI209 3

3

Sep 14, 2020 Sprenkle - CSCI209 4

Summary of Inheritance

  • Remove repetitive code by modeling the “is-a”

hierarchy

Ø Move “common denominator” code up the inheritance chain

  • Don’t use inheritance unless all inherited

methods make sense

  • Use polymorphism

4

slide-3
SLIDE 3

9/14/20 3

PACKAGES

Sep 14, 2020 Sprenkle - CSCI209 5

5

Sep 14, 2020 Sprenkle - CSCI209 6

Review: Packages

  • Hierarchical structure of Java classes

Ø Directories of directories

  • Use import to access packages

java net lang util Object Date Fully qualified name: java.lang.String String

6

slide-4
SLIDE 4

9/14/20 4

Review: Importing Packages

  • Can import one class at a time or all the classes

within a package

  • Examples:

Ø * form may increase compile time

  • BUT, no effect on run-time performance

Sep 14, 2020 Sprenkle - CSCI209 7

import java.util.Date; import java.io.*; Import entire package

7

Standard Practice

  • To reduce chance of a conflict between names of classes,

put classes in packages

  • Use package keyword to say that a class belongs to a

package:

Ø package java.util; Ø First line in class file

  • Typically, use a unique prefix, similar to domain names

Ø com.ibm Ø edu.wlu.cs.logic

  • Organize code by the packages

Ø For example, code in edu.wlu.cs.logic package would be in a logic directory inside a cs directory inside a wlu directory inside a logic directory

Sep 14, 2020 Sprenkle - CSCI209 8

We will start organizing our code in packages soon

8

slide-5
SLIDE 5

9/14/20 5

FINAL KEYWORD

Sep 14, 2020 Sprenkle - CSCI209 9

9

Sep 14, 2020 Sprenkle - CSCI209 10

Preventing Inheritance

  • Sometimes, you do not want a class to derive from one
  • f your classes
  • A class that cannot be extended is known as a final

class

  • To make a class final, simply add the keyword final

in front of the class definition:

  • Example of final class: System

public final class Rooster extends Chicken { . . . } 10

slide-6
SLIDE 6

9/14/20 6

Sep 14, 2020 Sprenkle - CSCI209 11

Final methods

  • Can make a method final

Ø Any class derived from this class cannot override the final methods

  • By default, all methods in a final class are

final methods.

class Chicken { . . . public final String getName() { . . . } . . . }

Why would we want to make methods final? What are possible benefits to us, the compiler, …?

11

ABSTRACT CLASSES

Sep 14, 2020 Sprenkle - CSCI209 12

12

slide-7
SLIDE 7

9/14/20 7

Sep 14, 2020 Sprenkle - CSCI209 13

Abstract Classes

  • Classes in which not all methods are implemented

are abstract classes

Ø public abstract class public abstract class ZooAnimal ZooAnimal

  • Some methods defined, others not defined

Ø Partial implementation

  • Blank (unimplemented) methods are labeled as

abstract

Ø public abstract void public abstract void exercise(Environment exercise(Environment env env); );

13

Sep 14, 2020 Sprenkle - CSCI209 14

Abstract Classes

  • An abstract class cannot be instantiated

Ø i.e., can’t create an object of that class Ø But can have a constructor!

  • Child class of an abstract class can only be

instantiated if it overrides and implements every abstract method of parent class

Ø If child class does not override all abstract methods, it is also abstract

14

slide-8
SLIDE 8

9/14/20 8

Sep 14, 2020 Sprenkle - CSCI209 15

Abstract Classes

  • static, private, and final methods

cannot be abstract

Ø B/c cannot be overridden by a child class

  • final class cannot contain abstract methods
  • A class can be abstract even if it has no abstract

methods

Ø Use when implementation is incomplete and is meant to serve as a parent class for class(es) that complete the implementation

  • Can have array of objects of abstract class

Ø JVM will do dynamic dispatch for methods

Why? ZooAnimal[] animals = new ZooAnimals[10];

15

Sep 14, 2020 Sprenkle - CSCI209 16

Examples of abstract classes

  • Example 1:

Ø java.net.Socket Ø java.net.ssl.SSLSocket (abstract)

  • Example 2:

Ø java.util.Calendar (abstract) Ø java.util.GregorianCalendar

16

slide-9
SLIDE 9

9/14/20 9

Sep 14, 2020 Sprenkle - CSCI209 17

Summary: Defining Abstract Classes

➨Define a class as abstract when class has

partial implementation

17

INTERFACES

Sep 14, 2020 Sprenkle - CSCI209 18

18

slide-10
SLIDE 10

9/14/20 10

Sep 14, 2020 Sprenkle - CSCI209 19

Interfaces

  • Pure specification, no implementation

Ø A set of requirements for classes to conform to

  • Classes can implement one or more interfaces

19

Sep 14, 2020 Sprenkle - CSCI209 20

Example of an Interface

  • We can call Arrays.sort(array)
  • Arrays.sort sorts arrays of any object class that

implements the Comparable interface

  • Classes that implement Comparable must

provide a way to decide if one object is less than, greater than, or equal to another object

20

slide-11
SLIDE 11

9/14/20 11

Sep 14, 2020 Sprenkle - CSCI209 21

java.lang.Comparable

  • Any object that is (inherits) Comparable must

have a method named compareTo()

  • Returns:

Ø Return a negative integer if this object is less than the

  • bject passed as a parameter

Ø Return a positive integer if this object is greater than the

  • bject passed as a parameter

Ø Return a 0 if the two objects are equal

public interface Comparable { int compareTo(Object other); }

21

Sep 14, 2020 Sprenkle - CSCI209 22

Comparable Interface API/Javadoc

  • Specifies what the compareTo() method should

do

  • Says which Java library classes implement

Comparable

https://docs.oracle.com/en/java/javase/14/docs/api/java.base/ java/lang/Comparable.html

22

slide-12
SLIDE 12

9/14/20 12

Sep 14, 2020 Sprenkle - CSCI209 23

Implementing an Interface

  • In the class definition, specify that the class will

implement the specific interface

  • Provide a definition for all methods specified in

interface

public class Chicken implements Comparable How to determine Chicken order?

23

Sep 14, 2020 Sprenkle - CSCI209 24

Comparable Chickens

One way: order by height

What if otherObject is not a Chicken?

public class Chicken implements Comparable { . . . public int compareTo(Object otherObject) { Chicken other = (Chicken)otherObject; if (height < other.getHeight() ) return –1; if (height > other.getHeight()) return 1; return 0; // simpler: return height-other.getHeight() } } 24

slide-13
SLIDE 13

9/14/20 13

Sep 14, 2020 Sprenkle - CSCI209 25

Testing for Interfaces

  • Can also use the instanceof operator to

see if an object implements an interface

Ø e.g., to determine if an object can be compared to another object using the Comparable interface

if (obj instanceof Comparable) { // runs if obj is an object variable of a class // that implements the Comparable interface } else { // runs if it does not implement the interface }

25

Sep 14, 2020 Sprenkle - CSCI209 26

Interface Object Variables

  • Can use an object variable to refer to an object of any

class that implements an interface

  • Using this object variable, can only access the

interface’s methods

  • For example…

public void aMethod(Object obj) { … if (obj instanceof Comparable) { Comparable comp = (Comparable) obj; boolean res = comp.compareTo(obj2); } } 26

slide-14
SLIDE 14

9/14/20 14

Sep 14, 2020 Sprenkle - CSCI209 27

Interface Definitions

  • Interface methods are public by default

Ø Do not need to specify methods as public

public interface Comparable { int compareTo(Object other); }

27

Sep 14, 2020 Sprenkle - CSCI209 28

Interface Definitions and Inheritance

  • Can extend interfaces

Ø Allows a chain of interfaces that go from general to more specific

  • For example, define an interface for an object

that is capable of moving:

public interface Movable { void move(double x, double y); } 28

slide-15
SLIDE 15

9/14/20 15

Sep 14, 2020 Sprenkle - CSCI209 29

Interface Definitions and Inheritance

  • A powered vehicle is also Movable

Ø Must also have a milesPerGallon() method, which will return its gas mileage

public interface Powered extends Movable { double milesPerGallon(); } 29

Sep 14, 2020 Sprenkle - CSCI209 30

Constants in an Interface

  • If a variable is specified in an interface, it is

automatically a constant:

Øpublic static final variable

  • An object that implements Powered

interface has a constant SPEED_LIMIT defined

public interface Powered extends Movable { double milesPerGallon(); double SPEED_LIMIT = 95; } 30

slide-16
SLIDE 16

9/14/20 16

Sep 14, 2020 Sprenkle - CSCI209 31

Interface Definitions and Inheritance

  • Powered interface extends Movable interface
  • An object that implements Powered interface

must satisfy all requirements of that interface as well as the parent interface.

Ø A Powered object must have a milesPerGallon() and move() method

31

Sep 14, 2020 Sprenkle - CSCI209 32

Multiple Interfaces

  • A class can implement multiple interfaces

Ø Must fulfill the requirements of each interface

  • But NOT possible with inheritance

Ø A class can only extend (or inherit from) one class

public final class String implements Serializable, Comparable, CharSequence { …

32

slide-17
SLIDE 17

9/14/20 17

Sep 14, 2020 Sprenkle - CSCI209 33

Benefits of Interfaces

  • Abstraction

Ø Separate the interface from the implementation

  • Allow easier type substitution

Ø We’ll see this with Collections

  • Can implement multiple interfaces

33

Sep 14, 2020 Sprenkle - CSCI209 34

Interface Summary

  • Contain only object (not class) methods
  • All methods are public

Ø Implied if not explicit

  • Fields are constants that are static and

final

  • A class can implement multiple interfaces

Ø Separated by commas in definition

34

slide-18
SLIDE 18

9/14/20 18

Compare Interfaces and Abstract Classes

Sep 14, 2020 Sprenkle - CSCI209 35

  • Summarize characteristics of each.
  • Then discuss when should we use

an interface or an abstract class.

35

Using an Interface or Abstract Class

üAny class can use

ü Can implement multiple interfaces

  • No implementation
  • Implementing methods

multiple times

  • Adding a method to

interface will break classes that implement

  • Contain partial

implementation

  • Can’t extend/subclass

multiple classes

üAdd non-abstract methods

without breaking subclasses

Sep 14, 2020 Sprenkle - CSCI209 36

Interfaces Abstract Classes

36

slide-19
SLIDE 19

9/14/20 19

Sep 14, 2020 Sprenkle - CSCI209 37

One Option: Use Both!

  • Define interface, e.g., MyInterface
  • Define abstract class, e.g.,

AbstractMyInterface

Ø Implements interface Ø Provides implementation for some methods

37

Abstract Classes and Interfaces

  • Important structures in Java

Ø Make code easier to change

  • Will return to/apply these ideas throughout the

course

  • Concepts are used in many languages besides

Java

Sep 14, 2020 Sprenkle - CSCI209 38

38

slide-20
SLIDE 20

9/14/20 20

Looking Ahead

  • Assignment 6: Goblin Game

Ø Can now do the refactoring part Ø Due Wednesday before class

Sep 14, 2020 Sprenkle - CSCI209 39

39