PPT - Regis University: Academic Web Server for Faculty
Download
Report
Transcript PPT - Regis University: Academic Web Server for Faculty
CS-434: Object-Oriented
Programming Using Java
Week 2
Dr. Jesús Borrego
Adjunct Faculty
Regis University
1
scis.regis.edu ● [email protected]
Class Outline
• Review of Homework 1 and 2
• Key Terms
• Inheritance, overloading, visibility
polymorphism, constructors
• Classes, Interfaces, Packages
• Sample Program – in class
• Homework 3 - Individual
• Questions?
2
Review of Homework 1
• Create a use case diagram for a library
• Include actors, use cases, and relations between
users and use cases
• Prefer use of Visio
• More details in WorldClass (Case Study_Library
Application)
3
Review of Homework 2
• Create a Hello World program using NetBeans
• Capture screen where it shows the package and
results
• Submit to WorldClass before week 2
4
Key Terms
•
•
•
•
•
•
Constructor - Constructor
Inheritance - Herencia
Interface – Interfaz
Overloading – Sobrecargar
Package – Paquete
Polymorphism - Polimorfismo
5
Packages
• A package is a container for classes and other
code artifacts
• A package contains functionally related items
• A package is a namespace
▫ Collection of uniquely defined terms
• A main Java API package is called “java”
▫ It is empty but it is used to aggregate other
packages
6
Package java
• java.applet
• java.awt
• java.beans
• java.io
Example:
• java.util.concurrent.locks
• Package java has nested package java.util that
has a java.util.concurrent that one has
java.util.concurrent.locks
7
Visibility
• Data types and classes contained in a package
are visible to all items in the package
• Data types and classes contained in other
packages are not visible to others
• To access items in other packages, we have to
import them:
▫ packageb.ClassB b = new packageb.ClassB();
• To avoid this, import the package
▫ import packageb.ClassB;
8
Resolving Ambiguity
• If a class appears in two or more packages and we
import them, the compiler does not know which
packageX
packageY
one to access
classA
classA
import packageX;
import packageY;
We want class A, but which one?
package1.ClassA aaa = new packageX.classA();
Resolve ambiguity by using Fully Qualified Names (FQN)
9
Access Modifiers - Public
• Can be applied to class or data members and
methods within a class to denote the access is given
across package boundaries
package domain;
public class Book {
public void checkOut() {…}
…
}
10
Access Modifiers - Default
• Default – no modifier: Can be applied to class or
data members and methods within a class to denote
the access is NOT given across package boundaries
▫ Only accessible in the package in which they are
defined
package domain;
class Book {
public void checkOut() {…}
…
}
11
Access Modifiers - Private
• Can be applied to data members and methods (but
not to classes) within a class to denote the access is
restricted to members of the same class
▫ Only accessible in the package in which they are defined
package domain;
public class Book {
private String isbn;
public void getIsbn() {
return isbn;
}
…
}
12
Access Modifiers - Protected
• Can be applied to data members and methods (but not to
classes) within a class to denote the access is restricted to
members of the same class and derived classes
package domain;
public class LibraryItem{
protected void setHashCode () {…}
… }
package domain;
public class Book extends LibraryItem{
public LibraryItem() {
…
setHashCode();
}
… }
13
Method Overloading
• Sometimes we want to use the same name for a
method
• To differentiate, the methods must have at least
one of:
▫ Different number of parameters
▫ Type of parameters
▫ Order of parameters
• Return type is a differentiator, so it cannot be
used to overload methods
14
Static Fields and Methods
• Data members and methods are instance
members and therefore ‘non-static’
• They are created every time a class is
instantiated
• Sometimes it is desirable not to instantiate data
members (such as constants)
• In this case, we use ‘static’
public static final double PI = 3.14159
15
Inheritance
• A class extends another class and acquires
properties and behavior of class being extended
16
Inheritance
• A class extends another class and acquires
properties and behavior of class being extended
package domain;
public class Book extends LibraryItem {
// properties and behavior of Book go here
}
package domain;
public class Audio extends LibraryItem {
// properties and behavior of Audio go here
}
package domain;
public class Periodical extends LibraryItem {
// properties and behavior of Periodical go here
}
17
Class Constructors
• A constructor is a method that gets invoked
when a class is created
• The constructors must have the same name as
the enclosing class, and are declared with no
return value
• Constructors can be overloaded by providing
different argument profiles
• The constructor with no arguments is the default
constructor
18
Default Constructor Characteristics
• If no constructor is provided, Java provides a
default
• If you define at least one constructor, Java does
not provide the default
• Constructors are useful to initialize newly
created object’s data members
▫ Using default parameters, or arguments in the
constructor call
• Can call other constructors of the same or base
class
19
Overriding Methods
• Overriding occurs when a method in a base class
is redefined in a derived class
• To override a method, the signature of both
must match identically
20
Abstract Classes
• An abstract class is never intended to be
instantiated as an object
• It is intended to be a base class that provides
methods and together form an API
• Classes that inherit the abstract class can
override its base behavior and these can also be
instantiated as objects
• These are called templates
21
Inner classes and Anonymous classes
• Every Java class should be defined as a public
class, in its own file, where the file matches the
name of the class, and has a suffix .java
• Java also supports Inner Classes
• Anonymous classes are also inner classes
defined within the context of another class, but
do not have a name
22
Inner classes
23
Live Chat 3
Activity 1 – YouTube Videos
• YouTube contains videos on creating
applications and test cases
• Some examples:
▫ http://www.youtube.com/watch?v=oEqv2AJW-m4
▫ http://www.youtube.com/watch?v=OqYXpipGyJY
24
Activity 2 – Java Program
•
•
•
•
•
•
25
Demonstrate input and output
Demonstrate use of array manipulation
Demonstrate use of loops
Demonstrate use of random numbers
Create a class file
Create a main that invokes the class
Program details
•
•
•
•
26
The program reads values into an array
It will find the longest word in the array
Demonstrates passing of parameters
Displays the longest word
Homework 3
• This individual task is to be performed on the
Library application
• Create and test (using JUnit) a Book class for the
Library application project
▫ In addition to properties of author and isbn, with
setters/getters, the Book should override Object’s
base method “boolean equals(Object)” and have a
“boolean validate()” method
• Zip up your NetBeans project, and submit to
WorldClass before Week 3
27
JUnit Tutorial
• Working with JUnit in NetBeans tutorial:
http://www.youtube.com/watch?v=Q0ueT0Z6Zs
• Many more in both English and Spanish
28
Questions?
29