1 / 29

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

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

shauna
Download Presentation

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

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS-434: Object-Oriented Programming Using JavaWeek 2 Dr. Jesús Borrego Adjunct Faculty Regis University

  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?

  3. 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)

  4. 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

  5. Key Terms Constructor - Constructor Inheritance - Herencia Interface – Interfaz Overloading – Sobrecargar Package – Paquete Polymorphism - Polimorfismo

  6. 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

  7. 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

  8. 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;

  9. Resolving Ambiguity packageX classA packageY classA • 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)

  10. 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() {…} … }

  11. 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() {…} … }

  12. 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; } … }

  13. 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(); } … }

  14. 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

  15. 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

  16. Inheritance A class extends another class and acquires properties and behavior of class being extended

  17. 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 }

  18. 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

  19. 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

  20. 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

  21. 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

  22. 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

  23. Inner classes

  24. 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

  25. 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

  26. 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

  27. 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

  28. JUnit Tutorial Working with JUnit in NetBeans tutorial: http://www.youtube.com/watch?v=Q0ue-T0Z6Zs Many more in both English and Spanish

  29. Questions?

More Related