CmSc 150 Fundamentals of Computing I. Lesson 28: Introduction to Classes and Objects in Java. 1. Classes and Objects

Similar documents
Chapter 4: Writing Classes

Encapsulation. Administrative Stuff. September 12, Writing Classes. Quick review of last lecture. Classes. Classes and Objects

Anatomy of a Class Encapsulation Anatomy of a Method

ECE 122. Engineering Problem Solving with Java

Anatomy of a Method. HW3 is due Today. September 15, Midterm 1. Quick review of last lecture. Encapsulation. Encapsulation

CS1004: Intro to CS in Java, Spring 2005

11/19/2014. Objects. Chapter 4: Writing Classes. Classes. Writing Classes. Java Software Solutions for AP* Computer Science A 2nd Edition

Table of Contents Date(s) Title/Topic Page #s. Chapter 4: Writing Classes 4.1 Objects Revisited

class declaration Designing Classes part 2 Getting to know classes so far Review Next: 3/18/13 Driver classes:

Designing Classes. Where do objects come from? Where do objects come from? Example: Account datatype. Dr. Papalaskari 1

Designing Classes part 2

Where do objects come from? Good question!

Chapter 4 Defining Classes I

Objects and Classes -- Introduction

CSC 1051 Data Structures and Algorithms I. Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University

CSC 1051 Data Structures and Algorithms I. Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University

Chapter. We've been using predefined classes. Now we will learn to write our own classes to define objects

Encapsulation. You can take one of two views of an object: internal - the structure of its data, the algorithms used by its methods

Assignment 1 due Monday at 11:59pm

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

Chapter 5: Writing Classes and Enums

COMP 202. More on OO. CONTENTS: static revisited this reference class dependencies method parameters variable scope method overloading

Binghamton University. CS-140 Fall Problem Solving. Creating a class from scratch

Programming II (CS300)

Encapsulation. Mason Vail Boise State University Computer Science

Programming II (CS300)

COMP 202. More on OO. CONTENTS: static revisited this reference class dependencies method parameters variable scope method overloading

COMP-202. Objects, Part III. COMP Objects Part III, 2013 Jörg Kienzle and others

Object-Oriented Programming (Java)

Java Object Oriented Design. CSC207 Fall 2014

Object Class. EX: LightSwitch Class. Basic Class Concepts: Parts. CS257 Computer Science II Kevin Sahr, PhD. Lecture 5: Writing Object Classes

Programming II (CS300)

Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson

CMSC 132: Object-Oriented Programming II

Object-Oriented Programming (OOP) Basics. CSCI 161 Introduction to Programming I

Comments are almost like C++

Object Oriented Programming. Week 1 Part 3 Writing Java with Eclipse and JUnit

8.1 Inheritance. 8.1 Class Diagram for Words. 8.1 Words.java. 8.1 Book.java 1/24/14

Day 3. COMP 1006/1406A Summer M. Jason Hinek Carleton University

ITI Introduction to Computing II

ECE 122. Engineering Problem Solving with Java

ENCAPSULATION. private, public, scope and visibility rules. packages and package level access.

Object Oriented Features. Inheritance. Inheritance. CS257 Computer Science I Kevin Sahr, PhD. Lecture 10: Inheritance

Chapter 6 Introduction to Defining Classes

CS 112 Introduction to Programming

Methods and Data (Savitch, Chapter 5)

CS 302 Week 9. Jim Williams

Classes and Objects 3/28/2017. How can multiple methods within a Java class read and write the same variable?

Classes Classes 2 / 36

ITI Introduction to Computing II

COMP 250 Winter 2011 Reading: Java background January 5, 2011

Recap of OO concepts. Objects, classes, methods and more. Mairead Meagher Dr. Siobhán Drohan. Produced by:

COMP-202 Unit 8: Defining Your Own Classes. CONTENTS: Class Definitions Attributes Methods and Constructors Access Modifiers and Encapsulation

An Introduction to C++

Recommended Group Brainstorm (NO computers during this time)

Computer Science II. OO Programming Classes Scott C Johnson Rochester Institute of Technology

C08: Inheritance and Polymorphism

Chief Reader Report on Student Responses:

JAVA GUI PROGRAMMING REVISION TOUR III

Designing Classes. Where do objects come from? Where do objects come from? Example: Account datatype. Dr. Papalaskari 1

Introduction to Visual Basic and Visual C++ Introduction to Java. JDK Editions. Overview. Lesson 13. Overview

Objec,ves. Review: Object-Oriented Programming. Object-oriented programming in Java. What is OO programming? Benefits?

Object-Oriented Programming Concepts

Data Structures. Data structures. Data structures. What is a data structure? Simple answer: a collection of data equipped with some operations.

Classes and Methods גרא וייס המחלקה למדעי המחשב אוניברסיטת בן-גוריון

Lesson 10B Class Design. By John B. Owen All rights reserved 2011, revised 2014

Introduction to Programming Using Java (98-388)

CMSC131. Objects: Designing Classes and Creating Instances

Inheritance. Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L

Object Oriented Programming SCJ2153. Class and Object. Associate Prof. Dr. Norazah Yusof

Classes and Methods לאוניד ברנבוים המחלקה למדעי המחשב אוניברסיטת בן-גוריון

ECE 122. Engineering Problem Solving with Java

CLASSES AND OBJECTS IN JAVA

CPS122 Lecture: Defining a Class

Software and Programming 1

CS111: PROGRAMMING LANGUAGE II. Lecture 1: Introduction to classes

Understanding the Object Paradigm

Container Vs. Definition Classes. Container Class

CS-202 Introduction to Object Oriented Programming

CS1083 Week 2: Arrays, ArrayList

CSCE 156 Computer Science II

Classes and Methods עזאם מרעי המחלקה למדעי המחשב אוניברסיטת בן-גוריון מבוסס על השקפים של אותו קורס שניתן בשנים הקודמות

Inheritance Chapter 8. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Objects and Classes. 1 Creating Classes and Objects. CSCI-UA 101 Objects and Classes

COMP 401: THE DUAL ROLE OF A CLASS. Instructor: Prasun Dewan (FB 150,

Object Oriented Programming is a programming method that combines: Advantage of Object Oriented Programming

Principles of Object Oriented Programming. Lecture 4

9/19/2018 Programming Data Structures. Polymorphism And Abstract

CS200: Advanced OO in Java

ENCAPSULATION AND POLYMORPHISM

Notes on Chapter Three

CHAPTER 7 OBJECTS AND CLASSES

Java Review. Fundamentals of Computer Science

Data Structures (list, dictionary, tuples, sets, strings)

10. Java Classes. Classes - Technical. Example: Earthquake catalog. Classes - Conceptual

This Week. Fields and Variables. W05 Example 1: Variables & Fields. More on Java classes. Constructors. Modifiers

Handout 7. Defining Classes part 1. Instance variables and instance methods.

ECE 122. Engineering Problem Solving with Java

CMSC131. Library Classes

Robots. Byron Weber Becker. chapter 6

Transcription:

CmSc 150 Fundamentals of Computing I Lesson 28: Introduction to Classes and Objects in Java 1. Classes and Objects True object-oriented programming is based on defining classes that represent objects with well-defined characteristics and functionality An object has state and behavior Example: Consider the critter as defined in the Python class. Its state can be defined by its name, level of hunger and level of boredom. It s primary behavior includes eating, talking and playing. The Critter class models the state and behavior of a critter. The class serves as the blueprint for a critter object We can then instantiate as many critter objects as we need for any particular program. Example of the Critter class in Java: public class Critter private String name; private int hunger; private int boredom // Constructors: initialize private variables public Critter (String name) this.name = name; hunger = 0; boredom = 0; public Critter (String name, int hunger, int boredom) this.name = name; this.hunger = hunger; this.boredom = boredom; // Feeding the critter public void eat ( ) hunger--; if (hunger < 0) hunger = 0; System.out.println( Brruppp. Thank you. ); 1

2. Anatomy of a Class In Java we have two types of classes: classes that define objects and classes that define processes. For example, the Critter class defines objects of type Critter, while the ArrayMethods class defines processes over arrays. A class can contain data declarations and method declarations int size, weight; char category; Data declarations Method declarations Classes that define processes contain static methods. They may contain static variables accessible to all methods. Classes that define objects have two components object state and object behavior. Object state: defined by the values of the variables that give the objects characteristics Object behavior: defined by the functionality of the class methods 2.1. Data declarations The variables name, hunger and boredom in the Critter class are called instance data because each instance (object) that is created has its own version of them. A class declares the type of the data, but it does not reserve any memory space for it. Every time a Critter object is created, new instance variables are created as well. The objects of a class share the method definitions, but each object has its own data space. 2

Visibility modifiers of instance variables private: can be used only by the methods in the class public: can be used by programs that create objects of that class through the name of the object (example myarray.length) protected: can be used by derived classes through inheritance (not to be discussed here) As a rule instance variables of a class are defined as private. 2.2. Methods Constructors a constructor is a special method that is used to set up an object when it is initially created A constructor has the same name as the class A constructor has no return type there may be more than one constructor If no constructor is given, Java creates a default constructor without parameters The Critter constructor is used to set the name of the Critter and optionally the levels of hunger and boredom. Since Java does not have default variables as Python, we need two constructors. Accessors and Mutators Because instance data are private, a class usually provides services to access and modify data values An accessor method returns the current value of a variable A mutator method changes the value of a variable The names of accessor and mutator methods sometimes take the form getx and setx, respectively, where X is the name of the value. They are sometimes called getters and setters. The tostring method All classes that represent objects should define a tostring method The tostring method returns a character string that represents the object in some way It is called automatically when an object is concatenated to a string or when it is passed to the println method Methods of a class that defines objects are non-static, which means that they can be used only when applied to an object of that class. 3

3. Encapsulation We can take two views of an object: internal - the details of the variables and methods of the class that defines it external - the services that an object provides and how the object interacts with the rest of the system From the external view, an object is an encapsulated entity, providing a set of specific services. These services define the interface to the object One object (called the client) may use another object for the services it provides The client of an object may request its services (call its methods), but it should not have to be aware of how those services are accomplished Any changes to the object's state (its variables) should be made by that object's methods We should make it difficult, if not impossible, for a client to access an object s variables directly. That is, an object should be self-governing An encapsulated object can be thought of as a black box -- its inner workings are hidden from the client. The client invokes the interface methods of the object, which manages the instance data. Client Methods Data 4. Visibility Modifiers In Java, we accomplish encapsulation through the appropriate use of visibility modifiers A modifier is a Java reserved word that specifies particular characteristics of a method or data. 4

Java has three visibility modifiers: public, protected, and private (not discussed in this class) Members of a class that are declared with public visibility can be referenced anywhere. Members of a class that are declared with private visibility can be referenced only within that class Public variables violate encapsulation because they allow the client to reach in and modify the values directly. Therefore instance variables should not be declared with public visibility It is acceptable to give a constant public visibility, which allows it to be used outside of the class. Public constants do not violate encapsulation because, although the client can access it, its value cannot be changed Methods that provide the object's services are declared with public visibility so that they can be invoked by clients. Public methods are also called service methods A method created simply to assist a service method is called a support method. Since a support method is not intended to be called by a client, it should not be declared with public visibility public private Variables Violate encapsulation Enforce encapsulation Methods Provide services to clients Support other methods in the class 5