CS-434: Object-Oriented Programming Using Java Week 2

29
scis.regis.edu [email protected] CS-434: Object-Oriented Programming Using Java Week 2 Dr. Jesús Borrego Adjunct Faculty Regis University 1

description

CS-434: Object-Oriented Programming Using Java Week 2. Dr. Jesús Borrego Adjunct Faculty Regis University. Class Outline. Review of Homework 1 and 2 Key Terms Inheritance, overloading, visibility polymorphism, constructors Classes, Interfaces, Packages Sample Program – in class - PowerPoint PPT Presentation

Transcript of CS-434: Object-Oriented Programming Using Java Week 2

Page 1: CS-434: Object-Oriented Programming Using Java Week 2

scis.regis.edu ● [email protected]

CS-434: Object-Oriented Programming Using Java

Week 2

Dr. Jesús BorregoAdjunct FacultyRegis University

1

Page 2: CS-434: Object-Oriented Programming Using Java Week 2

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

Page 3: CS-434: Object-Oriented Programming Using Java Week 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

Page 4: CS-434: Object-Oriented Programming Using Java Week 2

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

Page 5: CS-434: Object-Oriented Programming Using Java Week 2

Key Terms

• Constructor - Constructor• Inheritance - Herencia• Interface – Interfaz• Overloading – Sobrecargar• Package – Paquete• Polymorphism - Polimorfismo

5

Page 6: CS-434: Object-Oriented Programming Using Java Week 2

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

Page 7: CS-434: Object-Oriented Programming Using Java Week 2

Package java

•java.applet• java.awt•java.beans•java.ioExample:• 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

Page 8: CS-434: Object-Oriented Programming Using Java Week 2

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

Page 9: CS-434: Object-Oriented Programming Using Java Week 2

Resolving Ambiguity• If a class appears in two or more packages and

we import them, the compiler does not know which one to access

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

packageXclassA

packageYclassA

Page 10: CS-434: Object-Oriented Programming Using Java Week 2

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

Page 11: CS-434: Object-Oriented Programming Using Java Week 2

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

Page 12: CS-434: Object-Oriented Programming Using Java Week 2

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 definedpackage domain; public class Book { private String isbn; public void getIsbn() { return isbn; } … }

12

Page 13: CS-434: Object-Oriented Programming Using Java Week 2

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 classespackage domain; public class LibraryItem{ protected void setHashCode () {…} … }package domain; public class Book extends LibraryItem{ public LibraryItem() { … setHashCode(); } … }

13

Page 14: CS-434: Object-Oriented Programming Using Java Week 2

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

Page 15: CS-434: Object-Oriented Programming Using Java Week 2

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

Page 16: CS-434: Object-Oriented Programming Using Java Week 2

Inheritance

16

•A class extends another class and acquires properties and behavior of class being extended

Page 17: CS-434: Object-Oriented Programming Using Java Week 2

Inheritance

17

•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 }

Page 18: CS-434: Object-Oriented Programming Using Java Week 2

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

Page 19: CS-434: Object-Oriented Programming Using Java Week 2

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 class19

Page 20: CS-434: Object-Oriented Programming Using Java Week 2

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

Page 21: CS-434: Object-Oriented Programming Using Java Week 2

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

Page 22: CS-434: Object-Oriented Programming Using Java Week 2

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

Page 23: CS-434: Object-Oriented Programming Using Java Week 2

Inner classes

23

Page 24: CS-434: Object-Oriented Programming Using Java Week 2

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

Live Chat 3

Page 25: CS-434: Object-Oriented Programming Using Java Week 2

Activity 2 – Java Program

•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

25

Page 26: CS-434: Object-Oriented Programming Using Java Week 2

Program details

•The program reads values into an array•It will find the longest word in the array•Demonstrates passing of parameters•Displays the longest word

26

Page 27: CS-434: Object-Oriented Programming Using Java Week 2

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

Page 28: CS-434: Object-Oriented Programming Using Java Week 2

JUnit Tutorial

•Working with JUnit in NetBeans tutorial: http://www.youtube.com/watch?v=Q0ue-T0Z6Zs

•Many more in both English and Spanish

28

Page 29: CS-434: Object-Oriented Programming Using Java Week 2

Questions?

29