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 Homework 3 - Individual Questions?
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)
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
Key Terms Constructor - Constructor Inheritance - Herencia Interface – Interfaz Overloading – Sobrecargar Package – Paquete Polymorphism - Polimorfismo
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
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
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;
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) packageX classA packageY classA
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() {…} … }
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() {…} … }
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; } …
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 () {…} … } public class Book extends LibraryItem{ public LibraryItem() { … setHashCode(); }
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
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
Inheritance A class extends another class and acquires properties and behavior of class being extended
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 } public class Audio extends LibraryItem { // properties and behavior of Audio go here public class Periodical extends LibraryItem { // properties and behavior of Periodical go here
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
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
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
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
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
Inner classes
Activity 1 – YouTube Videos 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
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
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
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
JUnit Tutorial Working with JUnit in NetBeans tutorial: http://www.youtube.com/watch?v=Q0ue- T0Z6Zs Many more in both English and Spanish
Questions?