72
10-1 Lecture 10 Inheritance, Polymorphism, Interfaces, and Packages

10-1 Lecture 10 Inheritance, Polymorphism, Interfaces, and Packages

Embed Size (px)

Citation preview

Page 1: 10-1 Lecture 10 Inheritance, Polymorphism, Interfaces, and Packages

10-1

Lecture 10Inheritance, Polymorphism, Interfaces, and Packages

Page 2: 10-1 Lecture 10 Inheritance, Polymorphism, Interfaces, and Packages

10-2

Readings

• Chapter 7, Sections 7.8-7.10• Chapter 8

Page 3: 10-1 Lecture 10 Inheritance, Polymorphism, Interfaces, and Packages

10-3

Topics

•Inheritance• Polymorphism • Interfaces• Packages

Page 4: 10-1 Lecture 10 Inheritance, Polymorphism, Interfaces, and Packages

10-4

Class hierarchies

It's common/natural for classes to be related to each other

_______ ANIMAL________ / \ MAMMAL REPTILE / \ / \WHALE DOG SNAKE LIZARD / \ IGUANA SKINK

Page 5: 10-1 Lecture 10 Inheritance, Polymorphism, Interfaces, and Packages

10-5

Subclasses and Superclasses _______ ANIMAL________ / \ MAMMAL REPTILE / \ / \WHALE DOG SNAKE LIZARD / \ IGUANA SKINK

Class hierarchies represent "IS A" (or "is kind of") relationships.

Page 6: 10-1 Lecture 10 Inheritance, Polymorphism, Interfaces, and Packages

10-6

Why Capture these "is a kind of" relationships?

• Better organize related concepts• Reduce duplication through

inheritance

Advantages?

Page 7: 10-1 Lecture 10 Inheritance, Polymorphism, Interfaces, and Packages

10-7

So what is inheritance?

• the key of object-oriented programming.• is the act of sharing state and behaviour from a

more general type of class (i.e. a superclass).• allows code to be shared between classes

(software reusability).• saves time when programming (less code

needs to be written).• encourages reuse of well designed objects (i.e.,

proven and tested code).• helps keep code simple.

Page 8: 10-1 Lecture 10 Inheritance, Polymorphism, Interfaces, and Packages

10-8

A subclass:

• Is a class that does the inheriting by using state (i.e. instance variables) and behaviour (i.e., methods) from some other class.

• Note: it cannot access private methods or instance variables from its superclasses.

Page 9: 10-1 Lecture 10 Inheritance, Polymorphism, Interfaces, and Packages

10-9

A superclass

• Is a class from which its subclasses inherit state and behaviour.

• Is more general that its subclasses.

• Note: subclasses are more specific and "larger".

Page 10: 10-1 Lecture 10 Inheritance, Polymorphism, Interfaces, and Packages

10-10

A direct superclass:

• is the superclass from which the subclass explicitly inherits.

• Example– Mammal is the direct superclass of

Dog _______ ANIMAL________ / \ MAMMAL REPTILE / \ / \WHALE DOG SNAKE LIZARD / \ IGUANA SKINK

Page 11: 10-1 Lecture 10 Inheritance, Polymorphism, Interfaces, and Packages

10-11

Example _______ ANIMAL________ / \ MAMMAL REPTILE / \ / \WHALE DOG SNAKE LIZARD / \ IGUANA SKINKclass Animal {…}

class Mammal extends Animal {…}class Reptile extends Animal {…}class Whale extends Mammal {…}class Dog extends Mammal {…}class Snake extends Reptile {…}class Lizard extends Reptile {…}class Iguana extends Lizard {…}class Skink extends Lizard {…}

Page 12: 10-1 Lecture 10 Inheritance, Polymorphism, Interfaces, and Packages

10-12

Example _______ ANIMAL________ / \ MAMMAL REPTILE / \ / \WHALE DOG SNAKE LIZARD / \ IGUANA SKINKclass Animal {

private int age; public getAge(){ return age};}

class Mammal extends Animal { private float bodyTemperature; public get bodyTemperature{return bodyTemperature}

class Dog extends Mammal { String name;…}

fido.getAge()

Page 13: 10-1 Lecture 10 Inheritance, Polymorphism, Interfaces, and Packages

10-13

Instance Method lookup

• When a message (method) is sent to an instance, Java must look up the method in the class hierarchy.

• If Java does not find the method defined in the class of the receiver, it checks the superclass.

• If not found their either, it checks the superclass of that class and so on ... until the Object class is reached.

• This lookup occurs at runtime (i.e. when the program is executing).

Page 14: 10-1 Lecture 10 Inheritance, Polymorphism, Interfaces, and Packages

10-14

Class Method Inheritance

• For class methods, the look-up occurs at compile time.

• Since an inherited class method is compiled as static (i.e., lookup at compile time) then within that method the object must be treated as an instance of the class that defines the method, not the subclass that inherits it.

Page 15: 10-1 Lecture 10 Inheritance, Polymorphism, Interfaces, and Packages

10-15

How many classes can a class inherit from?

_______ ANIMAL________ / \ MAMMAL PET REPTILE / \ / / \WHALE DOG SNAKE LIZARD / \ IGUANA SKINK

Is a dog both a Mammal and a Pet?

Page 16: 10-1 Lecture 10 Inheritance, Polymorphism, Interfaces, and Packages

10-16

Multiple Inheritance

• is the term given to a class that inherits from more than one direct superclass.

• is NOT supported in Java (but it is in C++ which we will not talk about at all).

• can get some of its advantages through the notion of interfaces.

Page 17: 10-1 Lecture 10 Inheritance, Polymorphism, Interfaces, and Packages

10-17

Exercise

A university has students, faculty, staff and administrators. Each has a number, name and date of birth. Faculty teach courses that have numbers, names and enrollment. Courses are given in rooms at specific times. Rooms have room numbers and capacities. Faculty and staff and administrators are employees. Employees have social security numbers and levels. Staff are either clerical or technical. Dalhousie, Carleton and Western are universities. All employees get salaries. Faculty have research grants that other employees and staff do not.Define the basic class structure for this problem in Java and make sure it compiles.

Page 18: 10-1 Lecture 10 Inheritance, Polymorphism, Interfaces, and Packages

10-18

Is a class without instances useful?

Human / \Female Male

Should we make instances of Human?

Page 19: 10-1 Lecture 10 Inheritance, Polymorphism, Interfaces, and Packages

10-19

An Abstract Class

• is a class that can never be instantiated (i.e., never have instances).

• always has subclasses• helps to organize a program

Page 20: 10-1 Lecture 10 Inheritance, Polymorphism, Interfaces, and Packages

10-20

A Concrete Class

• is a class that can be instantiated (i.e., can have instances).

Page 21: 10-1 Lecture 10 Inheritance, Polymorphism, Interfaces, and Packages

10-21

More Visibility Modifiers

Others modifiers: public,private,static

Modifier Class Member (variable ormethod)

protected N/ A Accessible within itspackage and subclasses

abstract A top-levelclass, not aninner one

Class member accessedthrough its class name

Final May not besub-classed

A f ield may not bechanged. A method maynot be overridden.

Page 22: 10-1 Lecture 10 Inheritance, Polymorphism, Interfaces, and Packages

10-22

Recall: the other modifiers

Modifier Class Member (variable ormethod)

none Accessible onlywithin itspackage

Accessible only withinits package

public Accessibleanywhere thatits package is

Accessible anywherethat its class packageis

private N/ A Accessible only withinits own class

static A top-levelclass, not aninner one

Class memberaccessed through itsclass name

Page 23: 10-1 Lecture 10 Inheritance, Polymorphism, Interfaces, and Packages

10-23

Modifier Summary

Member Visibility

Accessible to: public protected package private

Same Class yes yes yes yes

Class in samepackage

yes yes yes no

Subclass in

different package

yes yes no no

Non-subclass,different package

yes no no no

Page 24: 10-1 Lecture 10 Inheritance, Polymorphism, Interfaces, and Packages

10-24

Rules of thumb

• Use public only for methods and constants that form part of the API of the class.

• Use protected for fields and methods that aren’t necessary to "use" in the class, but may be of interest to anyone creating a subclass as part of a different package

Page 25: 10-1 Lecture 10 Inheritance, Polymorphism, Interfaces, and Packages

10-25

Rules of thumb

• Use none (ie. The defualt package visability) for fields ant methods that you want hidden outside the package , but which you want cooperating classes within the same package to have access to.

• Use private for fields and methods that are only used inside the class and should be hidden elsewhere.

Page 26: 10-1 Lecture 10 Inheritance, Polymorphism, Interfaces, and Packages

10-26

Examples hierarchies Student GraduateStudent UndergraduateStudent

Shape Circle Triangle Rectangle Square

Loan CarLoan HomeImprovemantLoan MortgageLoan

Account CheckingAccount SavingsAccount

Which should be abstract?Which concrete?

Page 27: 10-1 Lecture 10 Inheritance, Polymorphism, Interfaces, and Packages

10-27

Are they right?

• How do we know what subclasses to use ?

• What about a cube or a sphere or even a tetrahedron ?

Shape Circle Triangle Rectangle Square

Page 28: 10-1 Lecture 10 Inheritance, Polymorphism, Interfaces, and Packages

10-28

A better shape hierarchy?

• Perhaps more abstract classes would help?

• What else should we add?

Shape TwoDimensionalShape Circle Triangle Rectangle Square ThreeDimensionalShape Sphere Cube Tetrahedron

Page 29: 10-1 Lecture 10 Inheritance, Polymorphism, Interfaces, and Packages

10-29

More extensive yet

• What should be abstract?

Shape TwoDimensionalShape Ellipse Circle Polygon Triangle Rectangle Square ThreeDimensionalShape Sphere Polyhedron Cube Tetrahedron

Page 30: 10-1 Lecture 10 Inheritance, Polymorphism, Interfaces, and Packages

10-30

Can we enforce the idea of an abstract class?

• Yes, just use the abstract keyword:• Syntax:

public abstract class TheSubclassName extends TheSuperclassName{ ... }

Page 31: 10-1 Lecture 10 Inheritance, Polymorphism, Interfaces, and Packages

10-31

Every object inherits from something!!!

• When we do not write the extends portion of the declaration, Java automatically makes the class a subclass of Object

• Object is the most general class (as everything inherits from).

Page 32: 10-1 Lecture 10 Inheritance, Polymorphism, Interfaces, and Packages

10-32

Dogs bark, Terriers yap

Mammal

Dog

Terrier

May need to overrideThe definition of a method!!

max

fido

Page 33: 10-1 Lecture 10 Inheritance, Polymorphism, Interfaces, and Packages

10-33

Overriding

• is used when a method belonging to a superclass is not needed or is inappropriate

• allows the re-writing of the method for use in the subclass only

• the subclass own method with the same name (and same parameter list) that does something else (perhaps nothing).

Page 34: 10-1 Lecture 10 Inheritance, Polymorphism, Interfaces, and Packages

10-34

Person (name, phone#, address) Employee (employee#, workPhone#) Professor (courses, office #) Secretary (workload, schedule) Student (student#, courses, gpa) FullTimeStudent (major) PartTimeStudent (workPhone#)

Example

Page 35: 10-1 Lecture 10 Inheritance, Polymorphism, Interfaces, and Packages

10-35

Class Person

public class Person { protected String name, phoneNumber, address; /* Place code for methods here */ } public class Employee extends Person { protected int employeeNumber; protected String workPhoneNumber; /* Place code for methods here */ } public class Professor extends Employee { protected Vector courses; protected String officeNumber; /* Place code for methods here */ } public class Secretary extends Employee { protected Vector workLoad; protected Hashtable schedule; /* Place code for methods here */ }

Page 36: 10-1 Lecture 10 Inheritance, Polymorphism, Interfaces, and Packages

10-36

Class Person continued

public class Student extends Person { protected int studentNumber; protected Vector courses; protected float gpa; /* Place code for methods here */ } public class FullTimeStudent extends Student { protected String major; /* Place code for methods here */ } public class PartTimeStudent extends Student { protected String workPhoneNumber; /* Place code for methods here */ }

Page 37: 10-1 Lecture 10 Inheritance, Polymorphism, Interfaces, and Packages

10-37

Which class gets what?

• Which classes should implement – getName or getcourses methods?

• What should the toString methods return?

Person (name, phone#, address) Employee (employee#, workPhone#) Professor (courses, office #) Secretary (workload, schedule) Student (student#, courses, gpa)

Page 38: 10-1 Lecture 10 Inheritance, Polymorphism, Interfaces, and Packages

10-38

Possible toString outputPerson NAME: Jim Class ADDRESS: 1445 Porter St. PHONE #: 845-3232

Employee NAME: Rob Banks ADDRESS: 789 ScotiaBank Road. PHONE #: 899-2332 EMPLOYEE #: 88765 WORK #: 555-2433

Professor NAME: Guy Smart EMPLOYEE #: 65445 WORK #: 232-3415 OFFICE #: 5240 PA

Secretary NAME: Earl E. Bird ADDRESS: 12 Knowhere Cres. PHONE #: 443-7854 EMPLOYEE #: 76845 WORK #: 444-3243

Student NAME: May I. Passplease ADDRESS: 5567 Java Drive PHONE #: 732-8923 STUDENT #: 156753

Page 39: 10-1 Lecture 10 Inheritance, Polymorphism, Interfaces, and Packages

10-39

toString for the Person class

//This is the toString method for the Person class public String toString() { return("Person \n" + " NAME: " + getName() + "\n" + " ADDRESS: " + getAddress() + "\n" + " PHONE #: " + getPhoneNumer()); }

Can we do better? Remember this method will be inhetited!!!

Page 40: 10-1 Lecture 10 Inheritance, Polymorphism, Interfaces, and Packages

10-40

A better toString for the Person class

//This is the toString method for the Person class public String toString() { return(this.getClass().getName() + "\n" + " NAME: " + getName() + "\n" + " ADDRESS: " + getAddress() + "\n" + " PHONE #: " + getPhoneNumer()); }

Page 41: 10-1 Lecture 10 Inheritance, Polymorphism, Interfaces, and Packages

10-41

toString for the Employee class

//This is the toString method for the Employee class public String toString() { return(super.toString() + "\n" + " EMPLOYEE #: " + getEmployeeNumber() + "\n" + " WORK #: " + getWorkNumer()); }

Idea: super classes method PLUS!!!

Page 42: 10-1 Lecture 10 Inheritance, Polymorphism, Interfaces, and Packages

10-42

What next?

Person NAME: Jim Class ADDRESS: 1445 Porter St. PHONE #: 845-3232

Employee NAME: Rob Banks ADDRESS: 789 ScotiaBank Road. PHONE #: 899-2332 EMPLOYEE #: 88765 WORK #: 555-2433

Professor NAME: Guy Smart EMPLOYEE #: 65445 WORK #: 232-3415 OFFICE #: 5240 PA

Secretary NAME: Earl E. Bird ADDRESS: 12 Knowhere Cres. PHONE #: 443-7854 EMPLOYEE #: 76845 WORK #: 444-3243

Student NAME: May I. Passplease ADDRESS: 5567 Java Drive PHONE #: 732-8923 STUDENT #: 156753

Person Employee Professor Secretary Student

Page 43: 10-1 Lecture 10 Inheritance, Polymorphism, Interfaces, and Packages

10-43

toString for the Professor class

//This is the toString method for the Professor class public String toString() { return(this.getClass().getName() + "\n" + " NAME: " + getName() + "\n" + " EMPLOYEE #: " + getEmployeeNumber() + "\n" + " WORK #: " + getWorkNumer()); " OFFICE# : " + getOfficeNumber() + "\n"); }

Page 44: 10-1 Lecture 10 Inheritance, Polymorphism, Interfaces, and Packages

10-44

toString for the Student class

• inherits from Employee and also displays the student number.

//This is the toString method for the Student class public String toString() { return(super.toString() + "\n" + " STUDENT# : " + getStudentNumber() + "\n"); }

Page 45: 10-1 Lecture 10 Inheritance, Polymorphism, Interfaces, and Packages

10-45

Inheritance with Constructors

• A subclass automatically inherits the constructor of its superclass

• Good programming style: override the superclass constructor

• should explicitly call the superclass constructor at the beginning of code

• If not called explicitly, a superclass constructor is automatically called at the beginning of the subclass constructor

Page 46: 10-1 Lecture 10 Inheritance, Polymorphism, Interfaces, and Packages

10-46

Person constructors

public class Person { protected String name, phoneNumber, address;

public Person() { setName(""); setPhoneNumber(""); setAddress("") } public Person(String aName, String aPhoneNumber, String anAddress) { setName(aName); setPhoneNumber(aPhoneNumber); setAddress(anAddress); } }

Page 47: 10-1 Lecture 10 Inheritance, Polymorphism, Interfaces, and Packages

10-47

Employee Constructors

public class Employee extends Person { protected int employeeNumber; protected String workPhoneNumber;

public Employee() { //There is an implicit call to the Person() constructor here setEmployeeNumber(0); setWorkPhoneNumber(""); }

public Employee(int aNumber, String aPhoneNumber) { //There is an implicit call to the Person() constructor here setEmployeeNumber(aNumber); setWorkPhoneNumber(aPhoneNumber); } }

Page 48: 10-1 Lecture 10 Inheritance, Polymorphism, Interfaces, and Packages

10-48

Professor Constructors

public class Professor extends Employee { protected Vector courses; protected String officeNumber;

public Professor() { super(); courses = new Vector(); setOfficeNumber(""); }

public Professor(int aNumber, String aPhoneNumber, String anOfficeNumber) { super(aNumber, aPhoneNumber); courses = new Vector(); setOfficeNumber(anOfficeNumber); } }

Page 49: 10-1 Lecture 10 Inheritance, Polymorphism, Interfaces, and Packages

10-49

Final

• When a method is declared as final, it is not allowed to be overridden by a subclass.

• If a class is declared final, it cannot have subclasses.

Page 50: 10-1 Lecture 10 Inheritance, Polymorphism, Interfaces, and Packages

10-50

Relationship Between Superclass and Subclass

Objects• An object of a subclass can be treated

as an object of its corresponding superclass.

• For instance, a Professor can be treated as an employee under certain circumstances (i.e., administration purposes).

Can we treat an Employee object as a Professor object?

Page 51: 10-1 Lecture 10 Inheritance, Polymorphism, Interfaces, and Packages

10-51

Only in special cases!

• No, in general, an Employee does not have the state or behaviour of a Professor unless the Employee is actually a Professor object.

• In this case, we can use a type cast to instruct Java that the object will now be treated as a Professor.

Page 52: 10-1 Lecture 10 Inheritance, Polymorphism, Interfaces, and Packages

10-52

Exercise

• Complete this class structure• See Person.java

Person (name, phone#, address) Employee (employee#, workPhone#) Professor (courses, office #) Secretary (workload, schedule) Student (student#, courses, gpa) FullTimeStudent (major) PartTimeStudent (workPhone#)

Page 53: 10-1 Lecture 10 Inheritance, Polymorphism, Interfaces, and Packages

10-53

Topics

• Inheritance

•Polymorphism

• Interfaces• Packages

Page 54: 10-1 Lecture 10 Inheritance, Polymorphism, Interfaces, and Packages

10-54

Problem: Given a shape Draw it.

• Too many different methods

• Too much testing

Shape Circle Triangle Rectangle

aString = aShape.getClass().getName()); if (aString.equals("Circle")) aShape.drawCircle(); if (aString.equals("Triangle")) aShape.drawTriangle(); if (aString.equals("Rectangle")) aShape.drawRectangle();

Page 55: 10-1 Lecture 10 Inheritance, Polymorphism, Interfaces, and Packages

10-55

Polymorphism

• Having the same method name (and signature) for different methods of multiple classes

• Examples we have seen:– toString()– size()– isEmpty()

Page 56: 10-1 Lecture 10 Inheritance, Polymorphism, Interfaces, and Packages

10-56

A note on polymorphism

• If a method is inherited, it does not mean that polymorphism is "in play".

• In fact, this is NOT an example of polymorphism.

• In order to have polymorphism, there must be more than one method with the same name and signature.

• An inherited method is NOT a different method.

Page 57: 10-1 Lecture 10 Inheritance, Polymorphism, Interfaces, and Packages

10-57

Topics

• Inheritance• Polymorphism

•Interfaces• Packages

Page 58: 10-1 Lecture 10 Inheritance, Polymorphism, Interfaces, and Packages

10-58

What if I *really* do need multiple inheritance?

java.lang.Object | +--java.awt.Component | +--java.awt.Container | +--java.awt.Window | +--java.awt.Frame | +-- myWindow

ActionListener ComponentListener MouseListener TextListener WindowListener

Page 59: 10-1 Lecture 10 Inheritance, Polymorphism, Interfaces, and Packages

10-59

An Interface

• is a special kind of class which defines a specification of a set of methods.

• provides a guarantee that any class that implements this interface will provide these methods.

• is similar to setting "standards" and any class implementing it will get a "stamp of approval".

• A contract!!!!

Page 60: 10-1 Lecture 10 Inheritance, Polymorphism, Interfaces, and Packages

10-60

Inheritance vs. interfaces

• Is different from inheritance since interfaces do not inherit state or behaviour, instead they just define a specification!!!!!!

Page 61: 10-1 Lecture 10 Inheritance, Polymorphism, Interfaces, and Packages

10-61

Syntax

interface interfaceName { /* Method specifications */ }

class className implements interfaceName { /* Bodies for the interface methods */ /* Own data and methods. */ }

Page 62: 10-1 Lecture 10 Inheritance, Polymorphism, Interfaces, and Packages

10-62

Example: MovableObject

interface MovableObject { boolean start(); void stop(); boolean turn(int degrees); double fuelRemaining(); boolean changeSpeed(double kmPerHour); }

Page 63: 10-1 Lecture 10 Inheritance, Polymorphism, Interfaces, and Packages

10-63

aPlane class Plane implements MovableObject { boolean start() { //Do what's necessary to start the plane. Return true if it actually started. } void stop() { //Do whatever is necessary to stop the plane. } boolean turn(int degrees) { // Do what's necessary to turn the plane. Return true if it worked. } double fuelRemaining() { //Return the amount of plane fuel remaining. } boolean changeSpeed(double kmPerHour) { //Do what's necessary to accelerate or decelerate by the given amount } }

Page 64: 10-1 Lecture 10 Inheritance, Polymorphism, Interfaces, and Packages

10-64

How does having an interface help us?

• It helps us extract common behaviour among similar objects (kind of like inheritance).

• It makes a system more robust and easier to understand in that it ensures that the similar objects have at least certain behavours.

Page 65: 10-1 Lecture 10 Inheritance, Polymorphism, Interfaces, and Packages

10-65

A handheld remote control for MovableObjects.

class RemoteControl { private MovableObject machine;

// The constructor RemoteControl(MovableObject m) { machine = m; } ... //When the start button is pressed on the remote Boolean okay = machine.start(); if (!okay) display("No Response on start"); ... }

Works for Plane, Car, Train, Ship, Lawnmower, ....

Page 66: 10-1 Lecture 10 Inheritance, Polymorphism, Interfaces, and Packages

10-66

Using it!!!

• Can create remote controls for more than one class

• Each will operate correctly using its own implementation of the MovableObject interface

Plane aPlane = new Plane(); Ship aShip = new Ship(); RemoteControl planeRemote = new RemoteControl(plane); RemoteControl shipRemote = new RemoteControl(ship);

Page 67: 10-1 Lecture 10 Inheritance, Polymorphism, Interfaces, and Packages

10-67

Topics

• Inheritance• Polymorphism

• Interfaces

•Packages

Page 68: 10-1 Lecture 10 Inheritance, Polymorphism, Interfaces, and Packages

10-68

Packages• A Java package is a collection of classes• May or may not be related by inheritance• Groups similar and interdependent classes

together• The Java API is composed of multiple

packages• The import statement is used to assert

that a particular program will use classes from a particular package

Page 69: 10-1 Lecture 10 Inheritance, Polymorphism, Interfaces, and Packages

10-69

Packages

• A programmer can define a package and add classes to it

• The package statement is used to specify that all classes defined in a file belong to a particular package

• The syntax of the package statement is: package package-name;• It must be located at the top of a file, and there

can be only one package statement per file

Page 70: 10-1 Lecture 10 Inheritance, Polymorphism, Interfaces, and Packages

10-70

Packages

• The classes must be organized in the directory structure such that they can be found when referenced by an import statement

• There is a CLASSPATH environment variable on each computer system that determines where to look for classes when referenced

• See Simple_IO and Simple_IO_Test.java

Page 71: 10-1 Lecture 10 Inheritance, Polymorphism, Interfaces, and Packages

10-71

Packages

• The import statement specifies particular classes, or an entire package of classes, that can be used in that program

• Import statements are not necessary; a class can always be referenced by its fully qualified name in-line

• See Simple_IO_Test2.java• If two classes from two packages have the

same name and are used in the same program, they must be referenced by their fully qualified name

Page 72: 10-1 Lecture 10 Inheritance, Polymorphism, Interfaces, and Packages

10-72

Packages

• As a rule of thumb, if you will use only one class from a package, import that class specifically

• If two or more classes will be used, use the * wildcard character in the import statement to provide access to all classes in the package

• See Simple_IO_Test3.java