Chapter 8. Chapter Objectives. Chapter Objectives. User-Defined Classes and ADTs

Similar documents
Classes. Classes (continued) Syntax The general syntax for defining a class:

OO Programming Concepts. Classes. Objects. Chapter 8 User-Defined Classes and ADTs

C++ Programming: From Problem Analysis to Program Design, Fifth Edition. Chapter 12: Classes and Data Abstraction

Building custom components IAT351

Classes. Logical method to organise data and functions in a same structure. Also known as abstract data type (ADT).

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

And Even More and More C++ Fundamentals of Computer Science

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

Chapter 10 Introduction to Classes

CS1004: Intro to CS in Java, Spring 2005

Chapter 4: Writing Classes

Lecture 18 Tao Wang 1

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

Packages Inner Classes

Chapter 6 Class and Method

Chapter 3 Objects and Classes

Chapter 4 Defining Classes I

EE 422C HW 6 Multithreaded Programming

CS 302: Introduction to Programming in Java. Lecture 15

Notes on Chapter Three

Inheritance. Lecture 11 COP 3252 Summer May 25, 2017

Implementing an ADT with a Class

Fundamental Concepts and Definitions

Anatomy of a Class Encapsulation Anatomy of a Method

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

List ADT. Announcements. The List interface. Implementing the List ADT

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

Chapter 6 Introduction to Defining Classes

Subclassing for ADTs Implementation

CSCI 1301: Introduction to Computing and Programming Summer 2018 Lab 07 Classes and Methods

CMSC 132: Object-Oriented Programming II

1. (5 points) In your own words, describe what an instance is.

Programming for Engineers in Python

Object-Oriented Programming in C# (VS 2015)

Self-review Questions

Address book. Presenting the Java assignment about an Address Book v

Data abstractions: ADTs Invariants, Abstraction function. Lecture 4: OOP, autumn 2003

Object-Oriented Design (OOD) and C++

CSCE 156 Computer Science II

Introduction to Programming Using Java (98-388)

This page intentionally left blank

Implementing Abstractions

Inheritance and Polymorphism

INHERITANCE. Spring 2019

Chapter 15: Object Oriented Programming

Arrays & Classes. Problem Statement and Specifications

Computer Science 2 Lecture 4 Inheritance: Trinidad Fruit Stand 02/15/2014 Revision : 1.7

Starting Out with Java: From Control Structures Through Objects Sixth Edition

CSCE3193: Programming Paradigms

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

EECS 1001 and EECS 1030M, lab 01 conflict

CS313D: ADVANCED PROGRAMMING LANGUAGE

Overview. Lecture 7: Inheritance and GUIs. Inheritance. Example 9/30/2008

CSCI 1301: Introduction to Computing and Programming Spring 2019 Lab 10 Classes and Methods

Java for Programmers Course (equivalent to SL 275) 36 Contact Hours

CS313D: ADVANCED PROGRAMMING LANGUAGE

Object-Oriented Programming in C# (VS 2012)

Administration. Classes. Objects Part II. Agenda. Review: Object References. Object Aliases. CS 99 Summer 2000 Michael Clarkson Lecture 7

Introduction to Classes

CMSC131. Creating a Datatype Class Continued Exploration of Memory Model. Reminders

Object Orientation Fourth Story. Bok, Jong Soon

CS313D: ADVANCED PROGRAMMING LANGUAGE

Chapter 8 Objects and Classes

C++ Basic Syntax. Constructors and destructors. Wojciech Frohmberg / OOP Laboratory. Poznan University of Technology

Aggregation. Introduction to Computer Science I. Overview (1): Overview (2): CSE 1020 Summer Bill Kapralos. Bill Kapralos.

Introduction to Computer Science I

CMSC 132: Object-Oriented Programming II

INTRODUCTION TO SOFTWARE SYSTEMS (COMP1110/COMP1140/COMP1510/COMP6710)

Special Member Functions

Understanding class definitions

More on Inheritance. Interfaces & Abstract Classes

Java objects: a first view [Reading: chapters 1 & 2] CSC 142 B 1

Special Member Functions. Compiler-Generated Destructor. Compiler-Generated Default Constructor. Special Member Functions

Basic Principles of OO. Example: Ice/Water Dispenser. Systems Thinking. Interfaces: Describing Behavior. People's Roles wrt Systems

Programs as Models. Procedural Paradigm. Class Methods. CS256 Computer Science I Kevin Sahr, PhD. Lecture 11: Objects

Lecture 7. Log into Linux New documents posted to course webpage

The static keyword Used to denote fields and methods that belong to a class (but not to any particular object).

Exception Handling. Sometimes when the computer tries to execute a statement something goes wrong:

CSCI 135 Exam #2 Fundamentals of Computer Science I Fall 2013

Software Design and Analysis for Engineers

Objects and Classes -- Introduction

Exception Handling. Run-time Errors. Methods Failure. Sometimes when the computer tries to execute a statement something goes wrong:

C++ (classes) Hwansoo Han

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

THE NAME OF THE CONSTRUCTOR AND DESTRUCTOR(HAVING (~) BEFORE ITS NAME) FUNCTION MUST BE SAME AS THE NAME OF THE CLASS IN WHICH THEY ARE DECLARED.

JavaScript: Sort of a Big Deal,

Lecture Contents CS313D: ADVANCED PROGRAMMING LANGUAGE

[Course Overview] After completing this module you are ready to: Develop Desktop applications, Networking & Multi-threaded programs in java.

CS/ENGRD 2110 FALL Lecture 5: Local vars; Inside-out rule; constructors

More About Classes. Gaddis Ch. 14, CS 2308 :: Fall 2015 Molly O'Neil

Inheritance. A mechanism for specialization A mechanism for reuse. Fundamental to supporting polymorphism

Java 2. Course Outcome Summary. Western Technical College. Course Information. Course History. Course Competencies

Inheritance and Polymorphism

Homework Set 2- Class Design

S.No Question Blooms Level Course Outcome UNIT I. Programming Language Syntax and semantics

Advanced Java Programming

VB.NET. Exercise 1: Creating Your First Application in Visual Basic.NET

Object-Oriented Analysis, Design and Implementation. Case Study Part II

CHAPTER 7 OBJECTS AND CLASSES

Java Interfaces. 6 April 2016 OSU CSE 1

Transcription:

Chapter 8 User-Defined Classes and ADTs Chapter Objectives Learn about classes Learn about private, protected, public, and static members of a class Explore how classes are implemented Learn about the various operations on classes Chapter Objectives Examine constructors and finalizers Examine the method tostring Learn about the abstract data type (ADT)

Classes class: reserved word; collection of a fixed number of components Components: members of a class Members accessed by name Class categories/modifiers private protected public Classes private: members of class are not accessible outside class public: members of class are accessible outside class Class members: can be methods or variables Variable members declared like any other variables Syntax The general syntax for defining a class is: modifier(s) class ClassIdentifier modifier(s) { classmembers } The general syntax for using the operator new is: new classname() //Line 1 OR new classname(argument1, argument2,..., argumentn) //Line 2

Classes The syntax to access a data member of a class object or method is: referencevariablename.membername Shallow copying: two or more reference variables of the same type point to the same object Deep copying: each reference variable refers to its own object Constructors Guarantee that data members are initialized when object is declared Automatically execute when class object created Name of constructor is name of class More than one constructor can be present in one class Default constructor: constructor without parameters The Copy Constructor Executes when an object is instantiated Initialized using an existing object Syntax: public ClassName(ClassName otherobject)

UML Diagram UML Diagram Top box: name of class Middle box: data members and their data types Bottom box: member methods names, parameter list, return type of method + means public method - means private method # means protected method Example: class Clock myclock = new Clock(); yourclock = new Clock(9,35,15);

Example: class Clock Example: class Clock The Method tostring public value-returning method Takes no parameters Returns address of String object Output using print and println methods Default definition creates String with name of object s class name followed by hash code of object

The Modifier static In the method heading, specifies that the method can be invoked by using the name of the class If used to declare data member, data member invoked by using the class name Static data members of class exist even when no object of class type instantiated Static variables are initialized to their default values static Data Members of a Class Illustrate illusobject1 = new Illustrate(3); Illustrate illusobject2 = new Illustrate(5); Finalizers Automatically execute when class object goes out of scope Have no parameters Only one finalizer per class Name of finalizer: finalize

Creating Packages You can create packages using reserved word package Define the class to be public (If class is not public it can only be used within package) Choose name for package Organize package (create subdirectories) Creating Package for class Clock package jpfpatpd.ch08.clockpackage; public class Clock { //put instance variables and methods, as before, here } import jpfpatpd.ch08.clockpackage.clock; The Reference this Refers to instance variables and methods of a class Used to implement cascaded method calls

Inner Classes Defined within other classes Can be either a complete class definition or anonymous inner class definition Used to handle events Abstract Data Types Definition: A data type that specifies the logical properties without the implementation details Programming Example: (Problem Statement) A new candy machine is bought for the gym, but it is not working properly. The machine sells candies, chips, gum, and cookies. In this programming example, we write a program to create a Java application program for this candy machine so that it can be put into operation. We divide this program in two parts. In the first part we design a non-gui application program. In the second part we design an application program that will create a GUI as described in the second part. The non-gui application program should do the following: 1. Show the customer the different products sold by the candy machine. 2. Let the customer make the selection. 3. Show the customer the cost of the item selected. 4. Accept money from the customer. 5. Release the item.

Programming Example: (Input and Output) Input: The item selection and the cost of the item Output: The selected item Programming Example: Components Cash Register Dispenser Machine Programming Example:

Programming Example: Programming Example: Programming Example:

Programming Example: Programming Example: Chapter Summary Creating classes Members of a class private protected public Static Implementing classes Various operations on classes

Chapter Summary Constructors Finalizers Method tostring Abstract Data Types