Objects and Classes. Basic OO Principles. Classes in Java. Mark Allen Weiss Copyright 2000

Similar documents
Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

Chapter 4 Defining Classes I

Chapter 6 Introduction to Defining Classes

EECS168 Exam 3 Review

C++ Review. CptS 223 Advanced Data Structures. Larry Holder School of Electrical Engineering and Computer Science Washington State University

Java Programming Training for Experienced Programmers (5 Days)

Short Notes of CS201

CS201 - Introduction to Programming Glossary By

CLASSES AND OBJECTS IN JAVA

Programming II (CS300)

Chapter 11. Categories of languages that support OOP: 1. OOP support is added to an existing language

CS 231 Data Structures and Algorithms, Fall 2016

JAVA MOCK TEST JAVA MOCK TEST II

Purpose of Review. Review some basic C++ Familiarize us with Weiss s style Introduce specific constructs useful for implementing data structures

What will this print?

Object Oriented Programming: In this course we began an introduction to programming from an object-oriented approach.

OBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe

Introduction to Programming Using Java (98-388)

Object-Oriented Programming Concepts

COP 3330 Final Exam Review

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

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

CHAPTER 7 OBJECTS AND CLASSES

Interview Questions of C++

Java: introduction to object-oriented features

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

Lecture 13: Object orientation. Object oriented programming. Introduction. Object oriented programming. OO and ADT:s. Introduction

CPS 506 Comparative Programming Languages. Programming Language

From C++ to Java. Duke CPS

Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson

CST141 Thinking in Objects Page 1

C++ Important Questions with Answers

Lecture 18 Tao Wang 1

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

CMPT 117: Tutorial 1. Craig Thompson. 12 January 2009

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

Designing and Implementing Classes Topic 6

CHAPTER 7 OBJECTS AND CLASSES

Week 7 - More Java! this stands for the calling object:

CMSC 132: Object-Oriented Programming II

Defining Classes and Methods

F1 A Java program. Ch 1 in PPIJ. Introduction to the course. The computer and its workings The algorithm concept

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

Week 6: Review. Java is Case Sensitive

Cpt S 122 Data Structures. Introduction to C++ Part II

More C++ : Vectors, Classes, Inheritance, Templates

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

CGS 2405 Advanced Programming with C++ Course Justification

Abstraction in Software Development

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

What is it? CMSC 433 Programming Language Technologies and Paradigms Spring Approach 1. Disadvantage of Approach 1

Object Oriented Paradigm

10. Abstract Data Types

PIC 20A The Basics of Java

More C++ : Vectors, Classes, Inheritance, Templates. with content from cplusplus.com, codeguru.com

Comments are almost like C++

Object Oriented Programming. Solved MCQs - Part 2

Object Oriented Programming

QUIZ on Ch.5. Why is it sometimes not a good idea to place the private part of the interface in a header file?

Object Oriented Programming in C#

Object-Oriented Programming

CSE 413 Spring Introduction to Ruby. Credit: Dan Grossman, CSE341

12/22/11. Java How to Program, 9/e. public must be stored in a file that has the same name as the class and ends with the.java file-name extension.

OOPS Viva Questions. Object is termed as an instance of a class, and it has its own state, behavior and identity.

Lecture 15: Object-Oriented Programming

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

Object Oriented Programming. Assistant Lecture Omar Al Khayat 2 nd Year

Object-oriented basics. Object Class vs object Inheritance Overloading Interface

Chapter 10 :: Data Abstraction and Object Orientation

엄현상 (Eom, Hyeonsang) School of Computer Science and Engineering Seoul National University COPYRIGHTS 2017 EOM, HYEONSANG ALL RIGHTS RESERVED

An Introduction to C++

Chapter 15: Object Oriented Programming

EMBEDDED SYSTEMS PROGRAMMING OO Basics

Atelier Java - J1. Marwan Burelle. EPITA Première Année Cycle Ingénieur.

CS 6456 OBJCET ORIENTED PROGRAMMING IV SEMESTER/EEE

Written by John Bell for CS 342, Spring 2018

CMSC 4023 Chapter 11

Chapter 10 Introduction to Classes

Principles of Object Oriented Programming. Lecture 4

You must declare all variables before they can be used. Following is the basic form of a variable declaration:

A JavaBean is a class file that stores Java code for a JSP

Chapter 11. Abstract Data Types and Encapsulation Concepts ISBN

Lecturer: William W.Y. Hsu. Programming Languages

CS 251 Intermediate Programming Methods and Classes

CS 251 Intermediate Programming Methods and More

Programming 2. Object Oriented Programming. Daniel POP

CS/B.TECH/CSE(New)/SEM-5/CS-504D/ OBJECT ORIENTED PROGRAMMING. Time Allotted : 3 Hours Full Marks : 70 GROUP A. (Multiple Choice Type Question)

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

QUIZ Friends class Y;

CS304 Object Oriented Programming Final Term

Chapter 4: Writing Classes

c) And last but not least, there are javadoc comments. See Weiss.

Abstract Data Types and Encapsulation Concepts

Exam 1 Prep. Dr. Demetrios Glinos University of Central Florida. COP3330 Object Oriented Programming

Comp 248 Introduction to Programming Chapter 4 & 5 Defining Classes Part B

Classes. Classes. Classes. Class Circle with methods. Class Circle with fields. Classes and Objects in Java. Introduce to classes and objects in Java.

CSC Java Programming, Fall Java Data Types and Control Constructs

public : int min, hour ; T( ) //here constructor is defined inside the class definition, as line function. { sec = min = hour = 0 ; }

Project 1. Java Control Structures 1/17/2014. Project 1 and Java Intro. Project 1 (2) To familiarize with

Transcription:

Objects and Classes Mark Allen Weiss Copyright 2000 8/30/00 1 Basic OO Principles Objects are entities that have structure and state. Each object defines operations that may access or manipulate that state. An object is an atomic unit: Its parts cannot be dissected by the general users of the object. Information hiding makes implementation details, including components of an object inaccessible. Encapsulation is the grouping of data and their operations to form an aggregate, while hiding the implementation of the aggregate. Wednesday, August 30, 2000 Copyright 1996, 1999, M. A. Weiss 2 Classes in Java A class consists of members. The two kinds of members are: Data members Functions that act on the data members Members (both data and functions) can be public or private (or three other things, described later). Unlike C++, there is no separation of interface and implementation. Example: IntCell.java. Wednesday, August 30, 2000 Copyright 1996, 1999, M. A. Weiss 3

Using an Object Objects are always accessed by referenced variables. Objects are defined by using new. This is (more or less) the only way! Example: IntCell m = new IntCell( ); Note parentheses (different than old C++). Some objects are defined with additional parameters; this is controlled by the constructor(s) for the object (as in C++). = for objects is a reference assignment. Wednesday, August 30, 2000 Copyright 1996, 1999, M. A. Weiss 4 Applying Methods A method is a class function that is applied to an object. (The C++ term is member function). Use the. operator to select a member: m.write( 5 ); int n = m.read( ); Private methods may not be selected by a method from another class. Public methods may be selected from anywhere. (The default, if you don t specify public or private, is somewhere between public and private). Wednesday, August 30, 2000 Copyright 1996, 1999, M. A. Weiss 5 Initialization of Fields Fields can be initialized inline Can use obscure initializer block Can use constructors If none of these are done, fields will get defaults: 0 for primitives false for boolean \0 for char null for references Wednesday, August 30, 2000 Copyright 1996, 1999, M. A. Weiss 6

Constructors Constructors are called when, and only when, a new object is allocated via a call to new. Constructors generally should be public. Like C++: no return type, and the name is the class name. THERE'S A COMMON BUG! Constructors can be overloaded No initializer lists or copy constructors needed. A default zero-parameter public constructor is generated only if no other constructor is provided. Wednesday, August 30, 2000 Copyright 1996, 1999, M. A. Weiss 7 this this refers to the current object. A second use of this is for constructors. Example: class Date { public Date( int m, int d, int y ) { month = m; day = d; year = y; } } public Date( int y ) { this( 1, 1, y ); } public Date( ) { this( 2001 ); } // private members month, day, year below Wednesday, August 30, 2000 Copyright 1996, 1999, M. A. Weiss 8 Destructors No destructors. Objects are garbage collected as needed. There is a procedure called finalize, as in Ada95. It is called immediately before garbage collection, BUT: when garbage collection occurs is non-deterministic. MORE ON THIS LATER IN THE COURSE. If resources are scarce, you have to clean up your own mess. For example, you may have to close files yourself. Wednesday, August 30, 2000 Copyright 1996, 1999, M. A. Weiss 9

Constant Things No constant member functions. Everything may alter the object. Java conventions: getmember: an accessor setmember: a mutator Instance fields can also be marked as final. Must set value by end of all paths through all constructors Cannot change value after constructor call Wednesday, August 30, 2000 Copyright 1996, 1999, M. A. Weiss 10 Class-Wide Things: static Members Like C++ A static member (either data or function) applies to the class, rather than a particular instance of the class. In the example below, each Junk object has its own x. But there is only one shared y. class Junk { private int x; static private int y; } Wednesday, August 30, 2000 Copyright 1996, 1999, M. A. Weiss 11 Initialization of Static Data Static data is initialized once, when class is first loaded (prior to creation of any objects of the class type). Cannot try to initialize static data in constructors -- too late; may not even be allowed to call constructors. Initialize fields either inline when declared (if simple) in static initializer (if complex) Wednesday, August 30, 2000 Copyright 1996, 1999, M. A. Weiss 12

Static Functions Same as static data: a controlling object is not needed: Integer.toString( 3 ) Some classes have static methods only provides a convenient location for logically global functions. Often have private constructor Examples: Math System Wednesday, August 30, 2000 Copyright 1996, 1999, M. A. Weiss 13 No destructors C++ Stuff Not In Java No implicit conversions via constructors Friends work differently No worrying about copy constructor and operator= Public/private is on a function by function basis. No separation of interface and implementation. Members automatically 0 for primitives, null for references (this is kind of in new C++) Wednesday, August 30, 2000 Copyright 1996, 1999, M. A. Weiss 14 Packages Used to organize classes. Classes in same package can have friendly visibility, which is default if no public/private. Place at the top of the source file, before the code that defines the class, the statement package PackageName; Classes in the package must be public to be used outside of the package All files of a package must be in a subdirectory that matches the full package name, visible from the CLASSPATH Wednesday, August 30, 2000 Copyright 1996, 1999, M. A. Weiss 15

Using Packages Use the import statement to use a package. Packages are searched for in directories that are branched off any directory named in the CLASSPATH variable. CLASSPATH almost always includes., so: In the main directory that you will work in, Create a subdirectory that will store the various classes in the package Place test programs in the main directory Have the test programs import the package if you want the shorthand If you change main directories, modify CLASSPATH. Wednesday, August 30, 2000 Copyright 1996, 1999, M. A. Weiss 16 Package Visibility Default visibility is package visible Packages are open-ended; anyone can join Package visibility is insecure and should be avoided if possible Wednesday, August 30, 2000 Copyright 1996, 1999, M. A. Weiss 17 Import Directives Allows class name to be used as a shorthand for the complete class name (that includes the package) Two forms: import java.io.fileinputstream; // One shorthand import java.io.*; // Lots of shorthands Packages do not include each other; neither do wildcard imports Wednesday, August 30, 2000 Copyright 1996, 1999, M. A. Weiss 18

javadoc Automatically (more or less) generates documentation from the source code. Makes it easy to have consistent documentation. Removes the need for package specification (class interface). Guarantees uniform documentation. Wednesday, August 30, 2000 Copyright 1996, 1999, M. A. Weiss 19 Comments First, prepare files for javadoc by using /** commenting conventions. Comment packages, classes, public members, and throw exceptions. Can add other info: return values, meaning of parameters, author names, version numbers, etc.. Then run javadoc. Various html pages are generated. (Without commenting, you still get pages with function prototypes). Wednesday, August 30, 2000 Copyright 1996, 1999, M. A. Weiss 20 Inheritance Exceptions Interfaces? Next Time Wednesday, August 30, 2000 Copyright 1996, 1999, M. A. Weiss 21