Title: CSC 1601
1CSC 1601
2Topics
- javadoc
- Advanced Java I/O
- Objects
- References
- Static variables and methods
- Wrapper classes
- Class parameters
- Inheritance
- Encapsulation
- Access control
- Debugging
- the Object class
- Polymorphism
- Late binding
- Up/down casting
- Abstract methods classes
- Interfaces
- Exceptions
3javadoc
- What is the difference between a Java compiler
and javadoc? - What is the output of a compiler?
- What is the output of javadoc?
- How do we control this output?
4Advanced Java I/O
- Output print, println, printf
- Input the Scanner class
5Objects
- Instance (value) of the class a particular
object (a particular value of a class type) - A class is a type and you can declare variables
of a class type. - Attributes, fields, or properties data
- Methods function
- Members data functions (or attributes
methods) - Defining a class vs. declaring an instance of a
class
6Objects
- Method definition vs. method invocation
- Class variables vs. local variables
- masking
- this keyword and parameter
- and object
7Information hiding encapsulation
- separate how to use the class from the
implementation details - grouping software into a unit in such a way that
it is easy to use because there is a well-defined
simple interface - data and actions are combined into a single item
(class object) and the details of the
implementation are hidden
8API
- Application Programming Interface
- description of how to use the class
9ADT
- Abstract Data Type
- data type that is written using good
information-hiding techniques
10Access modifiers
- public no access restrictions at all
- protected (to be discussed in the future)
- private the instance variable or method cannot
be accessed outside of the class definition
11Types of methods
- Accessor method that allows one to obtain (a
copy of) the (often private or protected) data in
a class - Mutator method that allows one to change the
data (often private or protected) in a class
12Preconditions and postconditions
- Precondition
- states what is assumed to be true when a method
is invoked - Postcondition
- Describes the effect of the method call
- States what will be true after the method is
invoked (assuming that the preconditions have
been met)
13Overloading
- Simply methods w/ the same name but different
parameters. - Rules
- Same name for each
- Different number of parameters and/or parameter
types. - All must have the exact same return type.
- Note Java only allows methods to be overloaded.
(Some languages allow operators to be overloaded
as well.)
14Constructors (ctors)
- Rules
- Method w/ same name as class name.
- May have more than one ctor.
- w/ 0 or more different arguments
- You get classname() by default.
- No return type may be specified (including void).
- Invoked via new.
- Copy ctor.
15Copy constructor
- Ctor w/ a single argument of the same type as the
class. - Should create an object that is a separate,
independent object that is an exact copy of the
argument object.
16References
- Variables are stored in consecutive bytes of
memory. - So variables can be referred to by their value
(copy) or by their reference (address). - In Java, variables of object types are references
(addresses) primitive variables are values
(copies).
17Static variables and methods
- Does not have/require a calling object (class
instance). - Static members cant refer to non static members
- Use class name to refer to the static members.
18Static variables
- belongs to the class as a whole not just one
object (not only one copy) - can be used to communicate between objects
- one object changes it and it changes for every
object - automatically initialized (to 0, false, null)
- also useful for defining constants
- What keyword do we use for this?
19Wrapper classes
- Primitive types
- Not first class objects
- int, double, float, etc.
- Wrapper classes
- Integer, Double, etc.
- Lack no argument ctors
- May also have static members
20Wrapper classes
- Boxing
- The process of going from a value of a primitive
type to an object of its wrapper class. - Integer iObj new Integer( 42 )
- Unboxing
- The process of going from an object of a wrapper
class to the corresponding value of a primitive
type. - int I iObj.intValue()
21Class parameters
- Passing parameters to functions
- Primitive types are passed (call) by value.
- Class types are passed (call) by reference.
22Privacy leaks
- When private things become not so private!
23Mutable and immutable classes
- Immutable class doesnt contain any methods
that change any of the data in an object of the
class - Mutable class contains public mutator methods
or other public methods that can change the data
in an object of the class - Rule Never return a reference to a mutable
private object.
24Deep copy vs. shallow copy
- Deep copy
- Copy that, with one exception, has no references
in common with the original object - Exception references to immutable objects are
allowed to be shared - Shallow copy a copy that is not deep
25Inheritance
- New class (derived class) is created from another
class (base class).
26Inheritance
- Base class parent class ancestor superclass
- Derived class child class descendent
subclass - Public and protected attributes and methods in
the base class are available to (inherited by)
the derived class. - super ctor vs. this ctor
27Public and private
- Derived class can loosen access restrictions.
- Derived class can change private to public.
- Derived class cant tighten them.
- Derived class cant change public to private.
28Types
- An object of a derived class has more than one
type. - Not only its type but also the type of every one
of its ancestors (all the way back to Object). - Object is the base class for classes that dont
extend anything. Object is the ancestor of all
classes. - instanceof operator
29Encapsulation and inheritance
- Private instance variables and methods in a base
class cannot be directly accessed (by name) in a
derived class. - They can be indirectly access (via accessors and
mutators in the base class). - Its exactly as if they dont exist. (They can
actually be defined and redefined in the derived
class.)
30Protected access
- Protected (rather than public or private) access
allows - Access by name inside its own class definition.
- Access by name inside any class derived from it.
- Access by name in the definition of any class in
the same package (even if the class is not
derived from it).
31Protected access
- Access between private and public
- Very weak protection
- Use is discouraged (use the following instead)
32Package access
- AKA default access or friendly access.
- Can be access by name by anything in the package
but nothing outside of the package. - This is what you get when you dont specify
either public, private, or protected (hence the
name default access). - (If you dont specify a package, you belong to
the default package.)
33Package access
- More restricted than protected.
- Removes Access by name inside any class derived
from it. if the derived class is NOT in the same
package. - Packages are analogous to directories (folder).
If you control the directory, you control the
package.
34Debugging
- Driver program
- Inserting System.out.print statements
- Using the debugger
- Set breakpoints
- Execute program line by line
- Exam variables while running
- Change variables while running
35Object class
- All objects extend (inherit from) Object
- The Object class has a number of especially
interested methods (that are inherited by our
classes) such as - clone
- equals
- getClass
- toString
36The instanceof operator and the getClass method
- Recall that the instanceof operator is true up
and down the inheritance hierarchy. - We need something more specific.
- getClass returns a representation of the class
that was used with new to create the object - Can be compared with and !
- Ex.
- if (object1.getClass() object2.getClass())
- System.out.println( same class. )
- else
- System.out.println( not the same class. )
37A better equals method
- public boolean equals ( Object other )
- if (othernull)
- return false
- else if (getClass() ! other.getClass())
- return false
- else
- Employee tmp (Employee)other
- return ( name.equals( tmp.name )
- hireDate.equals( tmp.hireDate ) )
-
383 main programming mechanisms that constitute OOP
- Encapsulation
- Inheritance
- Polymorphism
39Polymorphism
- The ability to associate many meanings to one
method name by means of a special mechanism known
as late binding or dynamic binding. - Allows one to make changes in the method
definition for the derived classes and have those
changes apply to the software written in the base
class.
40Late binding
- AKA dynamic binding
- Binding the process of associating a method
definition with a method invocation - Early binding the method definition is
associated with the method invocation when the
code is compiled AKA static binding - Late binding the method invocation is
associated with the method invocation when the
method is invoked (at run time) - Java uses late binding except for a few cases.
41Late binding exceptions
- Java does not use late binding with
- Private methods
- Methods marked final
- Static methods
- Static binding is used instead.
42Downcasting and upcasting
- Upcast assigning an object of a derived class
to a variable of a base class (or any ancestor
class) - straightforward
- Downcast a type cast from a base class to a
derived class (or from any ancestor class to any
descendent class) - troublesome
43clone() method
- defined in Object as
- protected Object clone()
- every object inherits a clone() method
- (supposed to) return a deep copy of the calling
object - you are expected to override it
- like a copy ctor but there are cases where
clone() works but the copy ctor does not.
44Abstract method
- A placeholder for a method that will be fully
defined in a subclass. - An abstract method has a complete method heading
with the addition of the keyword abstract. - Cannot be private.
45Abstract class
- A class that has at least one abstract method is
called an abstract class. - The class definition must have the keyword
abstract. - Ex.
- abstract public class Feet
-
- abstract void doSomething ( int count )
-
-
- A class without any abstract methods is called a
concrete class.
46Points to remember
- You cannot create an instance of an abstract
class. - An abstract class is a type. Therefore you can
use abstract classes as parameters to functions
and as variable types for concrete classes
derived from the abstract class.
47Interfaces
- Specifies a set of methods (i.e., method
headings) that any class that implements that
interface must have. - An interface is a type (but is not a class).
- Interface can be parameter type.
- Javas way of approximating multiple inheritance.
48Interfaces
- To implement an interface, a concrete class must
do - State implements InterfaceName or implements
InterfaceName1, , InterfaceNamen - You must implement all of the method headings
listed in the definition(s) of the interface(s).
49Interfaces
- To implement an interface, an abstract class must
do - State implements InterfaceName or implements
InterfaceName1, , InterfaceNamen - You must either implement all of the method
headings listed in the definition(s) of the
interface(s) or you must define as abstract the
method headings in the interface(s).
50Interfaces and interfaces
- An interface may extend an interface and specify
additional method headings. - Any concrete class that implements the derived
interface must implement all of the methods in
both interfaces.
51Constants and interfaces
- Constants may be defined in interfaces.
- Not really in the spirit of an interface
- Must be public static final (and will be even if
omitted) - No instance variables in interfaces
52Important interfaces
- Cloneable
- ActionListener
- MouseListener
- MouseMotionListener
53Exceptions exception handling
- Use sparingly.
- Things you can do with exceptions
- Define a new exception class.
- Create an exception instance.
- Throw an exception.
- Declare that an exception may be thrown (in a
particular function). - Handle the possibility that an exception may be
thrown.
54Define a new exception class
- extend Exception (or extend a subclass of
Exception). - This creates a new type.
- All have a ctor w/ a single String arg.
- Each has an accessor method called getMessage()
that returns the String from the ctor arg.
55Create an exception instance
- Ex.
- new Exception( Uh oh! )
- Exception e new Exception( Rats! )
56Throw an exception
- Ex.
- throw new Exception( Invalid value. )
- Exception e new Exception( Invalid age. )
- throw e
57Declare (a method that indicates) that an
exception may be thrown
- Ex.
- public int f ( int x ) throws Exception
-
58Handle the possibility that an exception may be
thrown
- The try-catch blocks
- try
-
- catch (Exception e)
-