csc 1601 exam 1 review. topics javadoc advanced java i/o objects references static variables...

58
CSC 1601 CSC 1601 Exam 1 Review Exam 1 Review

Upload: alvin-lewis

Post on 18-Jan-2016

226 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CSC 1601 Exam 1 Review. Topics  javadoc  Advanced Java I/O  Objects  References  Static variables and methods  Wrapper classes  Class parameters

CSC 1601CSC 1601

Exam 1 ReviewExam 1 Review

Page 2: CSC 1601 Exam 1 Review. Topics  javadoc  Advanced Java I/O  Objects  References  Static variables and methods  Wrapper classes  Class parameters

TopicsTopics

javadocjavadoc Advanced Java I/OAdvanced Java I/O ObjectsObjects ReferencesReferences Static variables and Static variables and

methodsmethods Wrapper classesWrapper classes Class parametersClass parameters InheritanceInheritance EncapsulationEncapsulation

Access controlAccess control DebuggingDebugging the Object classthe Object class PolymorphismPolymorphism Late bindingLate binding Up/down castingUp/down casting Abstract methods & Abstract methods &

classesclasses InterfacesInterfaces ExceptionsExceptions

Page 3: CSC 1601 Exam 1 Review. Topics  javadoc  Advanced Java I/O  Objects  References  Static variables and methods  Wrapper classes  Class parameters

javadocjavadoc

What is the difference between a What is the difference between a Java compiler and javadoc?Java compiler and javadoc?• What is the output of a compiler?What is the output of a compiler?• What is the output of javadoc?What is the output of javadoc?

How do we control this output?How do we control this output?

Page 4: CSC 1601 Exam 1 Review. Topics  javadoc  Advanced Java I/O  Objects  References  Static variables and methods  Wrapper classes  Class parameters

Advanced Java I/OAdvanced Java I/O

Output: print, println, printfOutput: print, println, printf

Input: the Scanner classInput: the Scanner class

Page 5: CSC 1601 Exam 1 Review. Topics  javadoc  Advanced Java I/O  Objects  References  Static variables and methods  Wrapper classes  Class parameters

ObjectsObjects

Instance (value) of the class = a particular Instance (value) of the class = a particular object (a particular value of a class type)object (a particular value of a class type)

A class is a type and you can declare A class is a type and you can declare variables of a class type.variables of a class type.

Attributes, fields, or properties = dataAttributes, fields, or properties = data Methods = functionMethods = function Members = data + functions (or attributes Members = data + functions (or attributes

+ methods)+ methods) Defining a class vs. declaring an instance Defining a class vs. declaring an instance

of a classof a class

Page 6: CSC 1601 Exam 1 Review. Topics  javadoc  Advanced Java I/O  Objects  References  Static variables and methods  Wrapper classes  Class parameters

ObjectsObjects

Method definition vs. method Method definition vs. method invocationinvocation

Class variables vs. local variablesClass variables vs. local variables• maskingmasking

thisthis keyword and parameter keyword and parameter == and object== and object

Page 7: CSC 1601 Exam 1 Review. Topics  javadoc  Advanced Java I/O  Objects  References  Static variables and methods  Wrapper classes  Class parameters

Information hiding & encapsulationInformation hiding & encapsulation

separate how to use the class from separate how to use the class from the implementation detailsthe implementation details

grouping software into a unit in such grouping software into a unit in such a way that it is easy to use because a way that it is easy to use because there is a well-defined simple there is a well-defined simple interfaceinterface

data and actions are combined into a data and actions are combined into a single item (class object) and the single item (class object) and the details of the implementation are details of the implementation are hiddenhidden

Page 8: CSC 1601 Exam 1 Review. Topics  javadoc  Advanced Java I/O  Objects  References  Static variables and methods  Wrapper classes  Class parameters

APIAPI

Application Programming InterfaceApplication Programming Interface

description of how to use the classdescription of how to use the class

Page 9: CSC 1601 Exam 1 Review. Topics  javadoc  Advanced Java I/O  Objects  References  Static variables and methods  Wrapper classes  Class parameters

ADTADT

Abstract Data TypeAbstract Data Type• data type that is written using good data type that is written using good

information-hiding techniquesinformation-hiding techniques

Page 10: CSC 1601 Exam 1 Review. Topics  javadoc  Advanced Java I/O  Objects  References  Static variables and methods  Wrapper classes  Class parameters

Access modifiersAccess modifiers

public = no access restrictions at allpublic = no access restrictions at all protected (to be discussed in the protected (to be discussed in the

future)future) private = the instance variable or private = the instance variable or

method cannot be accessed outside method cannot be accessed outside of the class definitionof the class definition

Page 11: CSC 1601 Exam 1 Review. Topics  javadoc  Advanced Java I/O  Objects  References  Static variables and methods  Wrapper classes  Class parameters

Types of methodsTypes of methods

1.1. Accessor – method that allows one Accessor – method that allows one to obtain (a copy of) the (often to obtain (a copy of) the (often private or protected) data in a classprivate or protected) data in a class

2.2. Mutator – method that allows one to Mutator – method that allows one to change the data (often private or change the data (often private or protected) in a classprotected) in a class

Page 12: CSC 1601 Exam 1 Review. Topics  javadoc  Advanced Java I/O  Objects  References  Static variables and methods  Wrapper classes  Class parameters

Preconditions and Preconditions and postconditionspostconditions

PreconditionPrecondition• states what is assumed to be true when states what is assumed to be true when

a method is invokeda method is invoked

PostconditionPostcondition• Describes the effect of the method callDescribes the effect of the method call• States what will be true after the States what will be true after the

method is invoked (assuming that the method is invoked (assuming that the preconditions have been met)preconditions have been met)

Page 13: CSC 1601 Exam 1 Review. Topics  javadoc  Advanced Java I/O  Objects  References  Static variables and methods  Wrapper classes  Class parameters

OverloadingOverloading

Simply methods w/ the same name Simply methods w/ the same name but different parameters.but different parameters.

Rules:Rules:1.1. Same name for eachSame name for each

2.2. Different number of parameters and/or Different number of parameters and/or parameter types.parameter types.

3.3. All must have the exact same return All must have the exact same return type.type.

Note: Java only allows methods to be overloaded. (Some Note: Java only allows methods to be overloaded. (Some languages allow operators to be overloaded as well.)languages allow operators to be overloaded as well.)

Page 14: CSC 1601 Exam 1 Review. Topics  javadoc  Advanced Java I/O  Objects  References  Static variables and methods  Wrapper classes  Class parameters

Constructors (ctors)Constructors (ctors)

Rules:Rules:1.1. Method w/ same name as class name.Method w/ same name as class name.

2.2. May have more than one ctor.May have more than one ctor. w/ 0 or more different argumentsw/ 0 or more different arguments You get classname() by default.You get classname() by default.

3.3. No return type may be specified No return type may be specified (including void).(including void).

4.4. Invoked via Invoked via newnew.. Copy ctor.Copy ctor.

Page 15: CSC 1601 Exam 1 Review. Topics  javadoc  Advanced Java I/O  Objects  References  Static variables and methods  Wrapper classes  Class parameters

Copy constructorCopy constructor

Ctor w/ a single argument of the same Ctor w/ a single argument of the same type as the class.type as the class.

Should create an object that is a Should create an object that is a separate, independent object that is separate, independent object that is an exact copy of the argument object.an exact copy of the argument object.

Page 16: CSC 1601 Exam 1 Review. Topics  javadoc  Advanced Java I/O  Objects  References  Static variables and methods  Wrapper classes  Class parameters

ReferencesReferences

Variables are stored in consecutive Variables are stored in consecutive bytes of memory.bytes of memory.

So variables can be referred to by So variables can be referred to by their value (copy) or by their their value (copy) or by their reference (address).reference (address).

In Java, variables of object types are In Java, variables of object types are references (addresses); primitive references (addresses); primitive variables are values (copies).variables are values (copies).

Page 17: CSC 1601 Exam 1 Review. Topics  javadoc  Advanced Java I/O  Objects  References  Static variables and methods  Wrapper classes  Class parameters

Static variables and methodsStatic variables and methods

Does not have/require a calling Does not have/require a calling object (class instance).object (class instance).

• Static members can’t refer to non Static members can’t refer to non static membersstatic members

• Use class name to refer to the static Use class name to refer to the static members.members.

Page 18: CSC 1601 Exam 1 Review. Topics  javadoc  Advanced Java I/O  Objects  References  Static variables and methods  Wrapper classes  Class parameters

Static variablesStatic variables

belongs to the class as a whole – not belongs to the class as a whole – not just one object (not only one copy)just one object (not only one copy)

can be used to communicate can be used to communicate between objectsbetween objects

one object changes it and it changes one object changes it and it changes for every objectfor every object

automatically initialized (to 0, false, automatically initialized (to 0, false, null)null)

also useful for defining constantsalso useful for defining constants• What keyword do we use for this?What keyword do we use for this?

Page 19: CSC 1601 Exam 1 Review. Topics  javadoc  Advanced Java I/O  Objects  References  Static variables and methods  Wrapper classes  Class parameters

Wrapper classesWrapper classes

Primitive typesPrimitive types• Not “first class” objectsNot “first class” objects• int, double, float, etc.int, double, float, etc.

Wrapper classesWrapper classes• Integer, Double, etc.Integer, Double, etc.• Lack “no argument” ctorsLack “no argument” ctors• May also have static membersMay also have static members

Page 20: CSC 1601 Exam 1 Review. Topics  javadoc  Advanced Java I/O  Objects  References  Static variables and methods  Wrapper classes  Class parameters

Wrapper classesWrapper classes

BoxingBoxing• The process of going from a value of a The process of going from a value of a

primitive type to an object of its wrapper primitive type to an object of its wrapper class.class.

• Integer iObj = new Integer( 42 );Integer iObj = new Integer( 42 ); UnboxingUnboxing

• The process of going from an object of a The process of going from an object of a wrapper class to the corresponding wrapper class to the corresponding value of a primitive type.value of a primitive type.

• int I = iObj.intValue();int I = iObj.intValue();

Page 21: CSC 1601 Exam 1 Review. Topics  javadoc  Advanced Java I/O  Objects  References  Static variables and methods  Wrapper classes  Class parameters

Class parametersClass parameters

Passing parameters to functions:Passing parameters to functions:• Primitive types are passed (call) by Primitive types are passed (call) by

value.value.• Class types are passed (call) by Class types are passed (call) by

reference.reference.

Page 22: CSC 1601 Exam 1 Review. Topics  javadoc  Advanced Java I/O  Objects  References  Static variables and methods  Wrapper classes  Class parameters

Privacy leaksPrivacy leaks

When private things become not so When private things become not so private!private!

Page 23: CSC 1601 Exam 1 Review. Topics  javadoc  Advanced Java I/O  Objects  References  Static variables and methods  Wrapper classes  Class parameters

Mutable and immutable classesMutable and immutable classes Immutable class = doesn’t contain any Immutable class = doesn’t contain any

methods that change any of the data in an methods that change any of the data in an object of the classobject of the class

Mutable class = contains public mutator Mutable class = contains public mutator methods or other public methods that can methods or other public methods that can change the data in an object of the classchange the data in an object of the class• Rule: Never return a reference to a mutable Rule: Never return a reference to a mutable

private object.private object.

Page 24: CSC 1601 Exam 1 Review. Topics  javadoc  Advanced Java I/O  Objects  References  Static variables and methods  Wrapper classes  Class parameters

Deep copy vs. shallow copyDeep copy vs. shallow copy

Deep copyDeep copy• Copy that, with one exception, has no Copy that, with one exception, has no

references in common with the original references in common with the original objectobject

• Exception: references to immutable Exception: references to immutable objects are allowed to be sharedobjects are allowed to be shared

Shallow copy = a copy that is not Shallow copy = a copy that is not deepdeep

Page 25: CSC 1601 Exam 1 Review. Topics  javadoc  Advanced Java I/O  Objects  References  Static variables and methods  Wrapper classes  Class parameters

InheritanceInheritance

New class (derived class) is created New class (derived class) is created from another class (base class).from another class (base class).

Page 26: CSC 1601 Exam 1 Review. Topics  javadoc  Advanced Java I/O  Objects  References  Static variables and methods  Wrapper classes  Class parameters

InheritanceInheritance

Base class = parent class = ancestor Base class = parent class = ancestor = superclass= superclass

Derived class = child class = Derived class = child class = descendent = subclassdescendent = subclass

Public and protected attributes and Public and protected attributes and methods in the base class are methods in the base class are available to (inherited by) the available to (inherited by) the derived class.derived class.

supersuper ctor vs. ctor vs. thisthis ctor ctor

Page 27: CSC 1601 Exam 1 Review. Topics  javadoc  Advanced Java I/O  Objects  References  Static variables and methods  Wrapper classes  Class parameters

Public and privatePublic and private

Derived class can “loosen” access Derived class can “loosen” access restrictions.restrictions.• Derived class can change private to Derived class can change private to

public.public. Derived class can’t “tighten” them.Derived class can’t “tighten” them.

• Derived class can’t change public to Derived class can’t change public to private.private.

Page 28: CSC 1601 Exam 1 Review. Topics  javadoc  Advanced Java I/O  Objects  References  Static variables and methods  Wrapper classes  Class parameters

TypesTypes

An object of a derived class has more An object of a derived class has more than one type.than one type.• Not only its type but also the type of Not only its type but also the type of

every one of its ancestors (all the way every one of its ancestors (all the way back to Object).back to Object).

• Object is the base class for classes that Object is the base class for classes that don’t extend anything. Object is the don’t extend anything. Object is the ancestor of all classes.ancestor of all classes.

• instanceof operatorinstanceof operator

Page 29: CSC 1601 Exam 1 Review. Topics  javadoc  Advanced Java I/O  Objects  References  Static variables and methods  Wrapper classes  Class parameters

Encapsulation and inheritanceEncapsulation and inheritance

Private instance variables and Private instance variables and methods in a base class cannot be methods in a base class cannot be directly accessed (by name) in a directly accessed (by name) in a derived class.derived class.

They can be indirectly access (via They can be indirectly access (via accessors and mutators in the base accessors and mutators in the base class).class).

It’s exactly as if they don’t exist. It’s exactly as if they don’t exist. (They can actually be defined and (They can actually be defined and redefined in the derived class.)redefined in the derived class.)

Page 30: CSC 1601 Exam 1 Review. Topics  javadoc  Advanced Java I/O  Objects  References  Static variables and methods  Wrapper classes  Class parameters

Protected accessProtected access

Protected (rather than public or Protected (rather than public or private) access allows:private) access allows:

1.1. Access by name inside its own class Access by name inside its own class definition.definition.

2.2. Access by name inside any class Access by name inside any class derived from it.derived from it.

3.3. Access by name in the definition of any Access by name in the definition of any class in the same package (even if the class in the same package (even if the class is not derived from it).class is not derived from it).

Page 31: CSC 1601 Exam 1 Review. Topics  javadoc  Advanced Java I/O  Objects  References  Static variables and methods  Wrapper classes  Class parameters

Protected accessProtected access

Access between private and publicAccess between private and public Very weak protectionVery weak protection Use is discouraged (use the following Use is discouraged (use the following

instead)instead)

Page 32: CSC 1601 Exam 1 Review. Topics  javadoc  Advanced Java I/O  Objects  References  Static variables and methods  Wrapper classes  Class parameters

Package accessPackage access

AKA default access or friendly access.AKA default access or friendly access. Can be access by name by anything in the Can be access by name by anything in the

package but nothing outside of the package but nothing outside of the package.package.

This is what you get when you don’t This is what you get when you don’t specify either public, private, or protected specify either public, private, or protected (hence the name default access).(hence the name default access).

(If you don’t specify a package, you belong (If you don’t specify a package, you belong to the default package.)to the default package.)

Page 33: CSC 1601 Exam 1 Review. Topics  javadoc  Advanced Java I/O  Objects  References  Static variables and methods  Wrapper classes  Class parameters

Package accessPackage access

More restricted than protected.More restricted than protected.

• Removes “Access by name inside any Removes “Access by name inside any class derived from it.” if the derived class derived from it.” if the derived class is NOT in the same package.class is NOT in the same package.

• Packages are analogous to directories Packages are analogous to directories (folder). If you control the directory, you (folder). If you control the directory, you control the package.control the package.

Page 34: CSC 1601 Exam 1 Review. Topics  javadoc  Advanced Java I/O  Objects  References  Static variables and methods  Wrapper classes  Class parameters

DebuggingDebugging

Driver programDriver program Inserting System.out.print Inserting System.out.print

statementsstatements Using the debuggerUsing the debugger

• Set breakpointsSet breakpoints• Execute program line by lineExecute program line by line• Exam variables while runningExam variables while running• Change variables while runningChange variables while running

Page 35: CSC 1601 Exam 1 Review. Topics  javadoc  Advanced Java I/O  Objects  References  Static variables and methods  Wrapper classes  Class parameters

Object classObject class

All objects extend (inherit from) All objects extend (inherit from) ObjectObject

The Object class has a number of The Object class has a number of especially interested methods (that especially interested methods (that are inherited by our classes) such as:are inherited by our classes) such as:• cloneclone• equalsequals• getClassgetClass• toStringtoString

Page 36: CSC 1601 Exam 1 Review. Topics  javadoc  Advanced Java I/O  Objects  References  Static variables and methods  Wrapper classes  Class parameters

The instanceof operator and the The instanceof operator and the getClass methodgetClass method

Recall that the instanceof operator is true Recall that the instanceof operator is true “up and down” the inheritance hierarchy.“up and down” the inheritance hierarchy.

We need something more specific.We need something more specific. getClass returns a representation of the getClass returns a representation of the

class that was used with new to create the class that was used with new to create the objectobject• Can be compared with == and !=Can be compared with == and !=• Ex.Ex.

if (object1.getClass() == object2.getClass())if (object1.getClass() == object2.getClass())System.out.println( “same class.” );System.out.println( “same class.” );

elseelseSystem.out.println( “not the same class.” );System.out.println( “not the same class.” );

Page 37: CSC 1601 Exam 1 Review. Topics  javadoc  Advanced Java I/O  Objects  References  Static variables and methods  Wrapper classes  Class parameters

A better equals methodA better equals method

public boolean equals ( Object other ) {public boolean equals ( Object other ) {if (other==null) {if (other==null) {

return false;return false;} else if (getClass() != other.getClass()) {} else if (getClass() != other.getClass()) {

return false;return false;} else {} else {

Employee tmp = (Employee)other;Employee tmp = (Employee)other;return (return ( name.equals( tmp.name )name.equals( tmp.name )

&&&& hireDate.equals( tmp.hireDate ) );hireDate.equals( tmp.hireDate ) );}}

}}

Page 38: CSC 1601 Exam 1 Review. Topics  javadoc  Advanced Java I/O  Objects  References  Static variables and methods  Wrapper classes  Class parameters

3 main programming mechanisms 3 main programming mechanisms that constitute OOP:that constitute OOP:

1.1. EncapsulationEncapsulation

2.2. InheritanceInheritance

3.3. PolymorphismPolymorphism

Page 39: CSC 1601 Exam 1 Review. Topics  javadoc  Advanced Java I/O  Objects  References  Static variables and methods  Wrapper classes  Class parameters

PolymorphismPolymorphism

The ability to associate many meanings to The ability to associate many meanings to one method name by means of a special one method name by means of a special mechanism known as late binding or mechanism known as late binding or dynamic binding.dynamic binding.

Allows one to make changes in the method Allows one to make changes in the method definition for the derived classes and have definition for the derived classes and have those changes apply to the software those changes apply to the software written written in the base classin the base class..

Page 40: CSC 1601 Exam 1 Review. Topics  javadoc  Advanced Java I/O  Objects  References  Static variables and methods  Wrapper classes  Class parameters

Late bindingLate binding

AKA dynamic bindingAKA dynamic binding Binding – the process of associating a method Binding – the process of associating a method

definition with a method invocationdefinition with a method invocation Early binding – the method definition is Early binding – the method definition is

associated with the method invocation when associated with the method invocation when the code is compiled; AKA static bindingthe code is compiled; AKA static binding

Late binding – the method invocation is Late binding – the method invocation is associated with the method invocation when associated with the method invocation when the method is invoked (at run time)the method is invoked (at run time)• Java uses late binding except for a few cases.Java uses late binding except for a few cases.

Page 41: CSC 1601 Exam 1 Review. Topics  javadoc  Advanced Java I/O  Objects  References  Static variables and methods  Wrapper classes  Class parameters

Late binding exceptionsLate binding exceptions

Java does not use late binding with:Java does not use late binding with:• Private methodsPrivate methods• Methods marked finalMethods marked final• Static methodsStatic methods

Static binding is used instead.Static binding is used instead.

Page 42: CSC 1601 Exam 1 Review. Topics  javadoc  Advanced Java I/O  Objects  References  Static variables and methods  Wrapper classes  Class parameters

Downcasting and upcastingDowncasting and upcasting

Upcast = assigning an object of a derived Upcast = assigning an object of a derived class to a variable of a base class (or any class to a variable of a base class (or any ancestor class)ancestor class)• straightforwardstraightforward

Downcast = a type cast from a base class Downcast = a type cast from a base class to a derived class (or from any ancestor to a derived class (or from any ancestor class to any descendent class)class to any descendent class)• troublesometroublesome

Page 43: CSC 1601 Exam 1 Review. Topics  javadoc  Advanced Java I/O  Objects  References  Static variables and methods  Wrapper classes  Class parameters

clone() methodclone() method

defined in Object as:defined in Object as:protected Object clone()protected Object clone()

every object inherits a clone() methodevery object inherits a clone() method (supposed to) return a deep copy of the (supposed to) return a deep copy of the

calling objectcalling object• you are expected to override ityou are expected to override it

like a copy ctor but there are cases where like a copy ctor but there are cases where clone() works but the copy ctor does not.clone() works but the copy ctor does not.

Page 44: CSC 1601 Exam 1 Review. Topics  javadoc  Advanced Java I/O  Objects  References  Static variables and methods  Wrapper classes  Class parameters

Abstract methodAbstract method

A placeholder for a method that will A placeholder for a method that will be fully defined in a subclass.be fully defined in a subclass.

An abstract method has a complete An abstract method has a complete method heading with the addition of method heading with the addition of the keyword the keyword abstractabstract..

Cannot be private.Cannot be private.

Page 45: CSC 1601 Exam 1 Review. Topics  javadoc  Advanced Java I/O  Objects  References  Static variables and methods  Wrapper classes  Class parameters

Abstract classAbstract class A class that has at least one abstract method is A class that has at least one abstract method is

called an called an abstract classabstract class.. The class definition The class definition mustmust have the keyword have the keyword

abstractabstract.. Ex.Ex.

abstract public class Feet {abstract public class Feet {……abstract void doSomething ( int count );abstract void doSomething ( int count );……

}} A class without any abstract methods is called a A class without any abstract methods is called a

concrete classconcrete class..

Page 46: CSC 1601 Exam 1 Review. Topics  javadoc  Advanced Java I/O  Objects  References  Static variables and methods  Wrapper classes  Class parameters

Points to rememberPoints to remember

You cannot create an instance of an You cannot create an instance of an abstract class.abstract class.

An abstract class is a type. An abstract class is a type. Therefore you can use abstract Therefore you can use abstract classes as parameters to functions classes as parameters to functions and as variable types for concrete and as variable types for concrete classes derived from the abstract classes derived from the abstract class.class.

Page 47: CSC 1601 Exam 1 Review. Topics  javadoc  Advanced Java I/O  Objects  References  Static variables and methods  Wrapper classes  Class parameters

Specifies a set of methods (i.e., Specifies a set of methods (i.e., method headings) that any class that method headings) that any class that implements that interface must implements that interface must have.have.

An interface is a type (but is not a An interface is a type (but is not a class).class).• Interface can be parameter type.Interface can be parameter type.

Java’s way of approximating multiple Java’s way of approximating multiple inheritance.inheritance.

Page 48: CSC 1601 Exam 1 Review. Topics  javadoc  Advanced Java I/O  Objects  References  Static variables and methods  Wrapper classes  Class parameters

To implement an interface, a To implement an interface, a concrete classconcrete class must do: must do:1.1. State “implements InterfaceName” or State “implements InterfaceName” or

“implements InterfaceName“implements InterfaceName11, …, , …, InterfaceNameInterfaceNamenn””

2.2. You must implement all of the method You must implement all of the method headings listed in the definition(s) of headings listed in the definition(s) of the interface(s).the interface(s).

Page 49: CSC 1601 Exam 1 Review. Topics  javadoc  Advanced Java I/O  Objects  References  Static variables and methods  Wrapper classes  Class parameters

To implement an interface, an To implement an interface, an abstract classabstract class must do: must do:1.1. State “implements InterfaceName” or State “implements InterfaceName” or

“implements InterfaceName“implements InterfaceName11, …, , …, InterfaceNameInterfaceNamenn””

2.2. You must either implement all of the You must either implement all of the method headings listed in the method headings listed in the definition(s) of the interface(s) or you definition(s) of the interface(s) or you must define as abstract the method must define as abstract the method headings in the interface(s).headings in the interface(s).

Page 50: CSC 1601 Exam 1 Review. Topics  javadoc  Advanced Java I/O  Objects  References  Static variables and methods  Wrapper classes  Class parameters

An interface may extend an An interface may extend an interface and specify additional interface and specify additional method headings.method headings.

Any concrete class that implements Any concrete class that implements the derived interface must the derived interface must implement all of the methods in implement all of the methods in both interfaces.both interfaces.

Page 51: CSC 1601 Exam 1 Review. Topics  javadoc  Advanced Java I/O  Objects  References  Static variables and methods  Wrapper classes  Class parameters

Constants may be defined in Constants may be defined in interfaces.interfaces.• Not really in the spirit of an interfaceNot really in the spirit of an interface• Must be public static final (and will be Must be public static final (and will be

even if omitted)even if omitted)• No instance variables in interfacesNo instance variables in interfaces

Page 52: CSC 1601 Exam 1 Review. Topics  javadoc  Advanced Java I/O  Objects  References  Static variables and methods  Wrapper classes  Class parameters

Important interfacesImportant interfaces

CloneableCloneable ActionListenerActionListener MouseListenerMouseListener MouseMotionListenerMouseMotionListener

Page 53: CSC 1601 Exam 1 Review. Topics  javadoc  Advanced Java I/O  Objects  References  Static variables and methods  Wrapper classes  Class parameters

Exceptions & exception handlingExceptions & exception handlingUse sparingly.Use sparingly.

Things you can do with exceptions:Things you can do with exceptions:1.1. Define a new exception class.Define a new exception class.

2.2. Create an exception instance.Create an exception instance.

3.3. Throw an exception.Throw an exception.

4.4. Declare that an exception may be thrown (in Declare that an exception may be thrown (in a particular function).a particular function).

5.5. Handle the possibility that an exception may Handle the possibility that an exception may be thrown.be thrown.

Page 54: CSC 1601 Exam 1 Review. Topics  javadoc  Advanced Java I/O  Objects  References  Static variables and methods  Wrapper classes  Class parameters

Define a new exception classDefine a new exception classextend Exception (or extend a extend Exception (or extend a

subclass of Exception).subclass of Exception).This creates a new type.This creates a new type.All have a ctor w/ a single String arg.All have a ctor w/ a single String arg.Each has an accessor method called Each has an accessor method called

getMessage() that returns the String getMessage() that returns the String from the ctor arg.from the ctor arg.

Page 55: CSC 1601 Exam 1 Review. Topics  javadoc  Advanced Java I/O  Objects  References  Static variables and methods  Wrapper classes  Class parameters

Create an exception instanceCreate an exception instanceEx.Ex.

•new Exception( “Uh oh!” );new Exception( “Uh oh!” );•Exception e = new Exception( “Rats!” );Exception e = new Exception( “Rats!” );

Page 56: CSC 1601 Exam 1 Review. Topics  javadoc  Advanced Java I/O  Objects  References  Static variables and methods  Wrapper classes  Class parameters

Throw an exceptionThrow an exceptionEx.Ex.

• throw new Exception( “Invalid value.” );throw new Exception( “Invalid value.” );

•Exception e = new Exception( “Invalid Exception e = new Exception( “Invalid age.” );age.” );

• throw e;throw e;

Page 57: CSC 1601 Exam 1 Review. Topics  javadoc  Advanced Java I/O  Objects  References  Static variables and methods  Wrapper classes  Class parameters

Declare (a method that indicates) that an Declare (a method that indicates) that an exception may be thrownexception may be thrown

Ex.Ex.public int f ( int x ) public int f ( int x ) throws Exceptionthrows Exception { {

……

}}

Page 58: CSC 1601 Exam 1 Review. Topics  javadoc  Advanced Java I/O  Objects  References  Static variables and methods  Wrapper classes  Class parameters

Handle the possibility that an Handle the possibility that an exception may be thrownexception may be thrown

The try-catch blocks:The try-catch blocks:

try {try {

……

} catch (Exception e) {} catch (Exception e) {

……

}}