Question of the Day  On a game show you’re given the choice of three doors: Behind one door is a car; behind the others, goats. After you pick a door,

Slides:



Advertisements
Similar presentations
Looking inside classes Fields, Constructors & Methods Week 3.
Advertisements

Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
IMPLEMENTING CLASSES Chapter 3. Black Box  Something that magically does its thing!  You know what it does but not how.  You really don’t care how.
1 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
Chapter 10 THINKING IN OBJECTS 1 Object Oriented programming Instructor: Dr. Essam H. Houssein.
Lecture 2: Object Oriented Programming I
Defining classes and methods Recitation – 09/(25,26)/2008 CS 180 Department of Computer Science, Purdue University.
Road Map Introduction to object oriented programming. Classes
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
© 2006 Pearson Addison-Wesley. All rights reserved4-1 Chapter 4 Data Abstraction: The Walls.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
ECE122 L6: Problem Definition and Implementation February 15, 2007 ECE 122 Engineering Problem Solving with Java Lecture 6 Problem Definition and Implementation.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
IT PUTS THE ++ IN C++ Object Oriented Programming.
Programming Languages and Paradigms Object-Oriented Programming.
Classes and Class Members Chapter 3. 3 Public Interface Contract between class and its clients to fulfill certain responsibilities The client is an object.
Question of the Day  Write valid mathematical equation using: one addition operator (‘+’) one equality operator (‘=’)  Should have equal values.
Problem of the Day  What is the smallest positive integer that cannot be defined in less than twenty-five syllables?
Lecture 8 Inheritance Richard Gesick. 2 OBJECTIVES How inheritance promotes software reusability. The concepts of base classes and derived classes. To.
Chapter 8 More Object Concepts
Writing Classes (Chapter 4)
Packages. Package A package is a set of related classes Syntax to put a class into a package: package ; public class { …} Two rules:  A package declaration.
CSC 212 – Data Structures Lecture 12: Java Review.
Recap (önemli noktaları yinelemek) from last week Paradigm Kay’s Description Intro to Objects Messages / Interconnections Information Hiding Classes Inheritance.
CSC 212 Object-Oriented Programming and Java Part 1.
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.
CS200 Algorithms and Data StructuresColorado State University Part 4. Advanced Java Topics Instructor: Sangmi Pallickara
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.
Question of the Day  On a game show you’re given the choice of three doors: Behind one door is a car; behind the others, goats. After you pick a door,
1 Chapter 8 – Classes and Object: A Deeper Look Outline 1 Introduction 2 Implementing a Time Abstract Data Type with a Class 3 Class Scope 4 Controlling.
Computer Science 313 – Advanced Programming Topics.
Question of the Day  There are two escalators at each subway stop. One going up & one down.  Whenever an escalator needs to be fixed, they almost always.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Programming in Java CSCI-2220 Object Oriented Programming.
Chapter 10 Defining Classes. The Internal Structure of Classes and Objects Object – collection of data and operations, in which the data can be accessed.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Question of the Day  Thieves guild states it will sell to members: lock picking kits  $0.67 each 40’ rope  $2.12 each Wire cutters  $4.49 each How.
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)
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
Question of the Day  Thieves guild states it will sell to members: lock picking kits  $0.67 each 40’ rope  $2.12 each Wire cutters  $4.49 each How.
Object-Oriented Programming Chapter Chapter
Chapter 3 Introduction to Classes and Objects Definitions Examples.
IT108 Objects and Classes Part I George Mason University Revised 4/3/2012.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Session 6 Comments on Lab 3 & Implications of Inheritance.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 5 Methods.
Programming in java Packages Access Protection Importing packages Java program structure Interfaces Why interface Defining interface Accessing impln thru.
Object Oriented Programming. OOP  The fundamental idea behind object-oriented programming is:  The real world consists of objects. Computer programs.
ISBN Chapter 12 Support for Object-Oriented Programming.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
Mr H Kandjimi 2016/01/03Mr Kandjimi1 Week 3 –Modularity in C++
Georgia Institute of Technology More on Creating Classes Barb Ericson Georgia Institute of Technology June 2006.
Problem of the Day  Why are manhole covers round?
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
OOP Tirgul 7. What We’ll Be Seeing Today  Packages  Exceptions  Ex4 2.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
ENCAPSULATION. WHY ENCAPSULATE? So far, the objects we have designed have all of their methods and variables visible to any part of the program that has.
Object Oriented Programming. Constructors  Constructors are like special methods that are called implicitly as soon as an object is instantiated (i.e.
External Scope CECS 277 Mimi Opkins.
Chapter 3: Using Methods, Classes, and Objects
Week 4 Object-Oriented Programming (1): Inheritance
Encapsulation & Visibility Modifiers
IFS410: Advanced Analysis and Design
Lecture 22 Inheritance Richard Gesick.
Packages and Interfaces
Fundaments of Game Design
CSG2H3 Object Oriented Programming
Presentation transcript:

Question of the Day  On a game show you’re given the choice of three doors: Behind one door is a car; behind the others, goats. After you pick a door, the host, opens another door which has a goat. He then asks, "Do you change your selection?” Should you?

Params & Locals vs. Fields  Locals “live” for method or block within method  Name may be reused, but no relationship exists  Parameters pass data between methods  But receiving method must be called by sender  Fields store data between calls or even longer  Values available with instance (or longer if static)  Local always best choice to hold data  Easiest choice; use others only when needed

Params & Locals vs. Fields

Packages  All classes reside in a package  Declared at start of class file: package pkgName;  Classes in default package if not specified  Package part of full name of class  Within a package, class names must be unique  Between packages can reuse class names  Some classes usable without full name  Cannot import classes in default package  These classes are nearly useless

Why Use Packages?  Simplify organization of classes  Each package has small set of related classes  Can nest packages (e.g., java.util )  Enables creating hierarchy of related classes  Limits knowledge needed by programmer  Java programs use 1000s of classes  How many of these do you know?

Real-Life Debugging Story  System crashed when field was null  145 assignments in 100,000+ LOC  1 out of 145 did not check for null  Took 2 weeks to find & fix bug  Remembered class & its field’s issues  Saved time figuring out bug  Ultimately, I got lucky to fix this  Preventing bug is clear goal

Visibility Modifiers  Specify usage of classes, methods, or fields  Each modifier has different purpose  private – Access within same class (file) only  protected – Use anywhere in package or subclass  public – Use at anywhere & at any time  “package” – Lazy developer did not type a modifier

Common Nightmare

Prevent the Nightmare  Everyone tries keeping certain details private  What is exposed is limited

Prevent the Nightmare  Everyone tries keeping certain details private  What is exposed is limited

Prevent the Nightmare  Everyone tries keeping certain details private  What is exposed is limited  Limit how & why changes occur

Prevent the Nightmare  Everyone tries keeping certain details private  What is exposed is limited  Limit how & why changes occur  Stop others from seeing changes

Prevent the Nightmare  Exact same holds for objects  Make fields private unless for very important reason: private int fieldName;  Limits errors since field accesses entirely in 1 file  Improves modularity of your program  Makes object-oriented programs easier to write

Accessor Methods  Gets field’s value in another class  Getter methods useful, but not special  Named get Field or is Field depending on type public fieldType get Field () {  Other classes must use accessor method  Easily limit access by adding check for password  Searching for field’s uses is also simple

Mutator Methods  Sets field’s value from code in another class  Like getters, just another method  Named set Field but using actual name of field public void set Field (fieldType param) {  All field changes normally via mutator  Updates can be checked to insure value is legal  Number of ways creating bugs is limited

Visibility of Methods  Methods normally act on or with object  Use active verb as name  Declare as either public or protected  Replace copies of code with single method  May have common test, calculation, &c.  Work is internal to how class performs tasks  These methods should be private  Simplifies coding & debugging this class

Access Protection Benefits  Enforce constraints on object's state  Amount of understanding required is limited  Private members hidden unless rewriting code  Provide simple client interface  Abstraction: Make available only what people must know  Encapsulation: Separate interface from implementation

Your Turn  Get into your groups and complete activity

For Next Lecture  Study for term’s 1 st quiz on Friday  Includes everything we have covered so far  Focus on types, fields, methods, objects, variables…  Problems like activities, weekly assignments, etc.  (Finish) Reading for tomorrow’s lab on web  Provides good explanation about what we are doing  Week #3 assignment on Angel for you to start 5PM  Due Tuesday at 5PM (via Assignment Submitter)  During this lecture, we covered problem #1