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

Slides:



Advertisements
Similar presentations
PHP functions What are Functions? A function structure:
Advertisements

Final and Abstract Classes
Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
CS 211 Inheritance AAA.
Inheritance Inheritance Reserved word protected Reserved word super
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 Object-Oriented Design. 2 Objectives F To become familiar with the process of program development. F To the relationship types: association, aggregation,
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,
Lecture From Chapter 6 & /8/10 1 Method of Classes.
Java Methods By J. W. Rider. Java Methods Modularity Declaring methods –Header, signature, prototype Static Void Local variables –this Return Reentrancy.
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 12 Object-Oriented.
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Recap (önemli noktaları yinelemek) from last week Paradigm Kay’s Description Intro to Objects Messages / Interconnections Information Hiding Classes Inheritance.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Polymorphism, Inheritance Pt. 1 COMP 401, Fall 2014 Lecture 7 9/9/2014.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
CIS 644 Aug. 25, 1999 tour of Java. First … about the media lectures… we are experimenting with the media format please give feedback.
2000 Jordan Anastasiade. All rights reserved. 1 Class In this lesson you will be learning about: Class. Inheritance. Polymorphism. Nested and.
Scis.regis.edu ● CS-432: Modern Software Engineering Week 2 Dr. Jesús Borrego Lead Faculty, COS Regis University 1.
CSE 501N Fall ‘09 14: Inheritance 20 October 2009 Nick Leidenfrost.
Chapter 7 Objects and Classes 1 Fall 2012 CS2302: Programming Principles.
Objects and Classes Chapter 6 CSCI CSCI 1302 – Objects and Classes2 Outline Introduction Defining Classes for Objects Constructing Objects Accessing.
Tuc Goodwin  Object and Component-Oriented Programming  Classes in C#  Scope and Accessibility  Methods and Properties  Nested.
Object Based Programming Chapter 8. 2 In This Chapter We will learn about classes Garbage Collection Data Abstraction and encapsulation.
Methods in Java. Program Modules in Java  Java programs are written by combining new methods and classes with predefined methods in the Java Application.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
JAVA Classes Review. Definitions Class – a description of the attributes and behavior of a set of computational objects Constructor – a method that is.
CSSE501 Object-Oriented Development. Chapter 4: Classes and Methods  Chapters 4 and 5 present two sides of OOP: Chapter 4 discusses the static, compile.
Programming in Java CSCI-2220 Object Oriented Programming.
Summing Up Object Oriented Design. Four Major Components: Abstraction modeling real-life entities by essential information only Encapsulation clustering.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Chapter 5 Objects and Classes Inheritance. Solution Assignments 3 & 4 Review in class…..
08 Encapsulation and Abstraction. 2 Contents Defining Abstraction Levels of Abstraction Class as Abstraction Defining a Java Class Instantiating a Class.
Chapter 5 Introduction to Defining Classes
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Scis.regis.edu ● CS-432: Modern Software Engineering Week 4 Dr. Jesús Borrego Lead Faculty, COS Regis University 1.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
More on Objects Mehdi Einali Advanced Programming in Java 1.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
Classes, Interfaces and Packages
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
CS 116 Lecture 1 John Korah Contains content provided by George Koutsogiannakis & Matt Bauer.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
1 Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
SourceAnatomy1 Java Source Anatomy Barb Ericson Georgia Institute of Technology July 2008.
YG - CS Concept of Encapsulation What is encapsulation? - data and functions/methods are packaged together in the class normally.
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
C# Programming: From Problem Analysis to Program Design1 Creating Your Own Classes C# Programming: From Problem Analysis to Program Design 4th Edition.
JAVA ACCESS MODIFIERS. Access Modifiers Access modifiers control which classes may use a feature. A classes features are: - The class itself - Its member.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Chapter 5 Introduction to Defining Classes Fundamentals of Java.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Objects and Classes. F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive data type and object type.
3 Introduction to Classes and Objects.
Inheritance and Polymorphism
Overloading and Overriding
Inheritance, Polymorphism, and Interfaces. Oh My
Interfaces.
Java Inheritance.
CIS 199 Final Review.
Final and Abstract Classes
Corresponds with Chapter 5
Presentation transcript:

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?