CMSC131. Library Classes

Similar documents
CMSC131. Inheritance. Object. When we talked about Object, I mentioned that all Java classes are "built" on top of that.

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

CMSC131. Objects: Designing Classes and Creating Instances

Chapter 4 Defining Classes I

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

Review questions. Review questions, cont d. Class Definition. Methods. Class definition: methods. April 1,

Java Fundamentals (II)

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

CS 251 Intermediate Programming Methods and Classes

CS 251 Intermediate Programming Methods and More

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

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

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

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

CSC9T4: Object Modelling, principles of OO design and implementation

COMP 250 Fall inheritance Nov. 17, 2017

Introduction to Programming Using Java (98-388)

CS201 - Assignment 3, Part 1 Due: Friday February 28, at the beginning of class

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

The class Object. Lecture CS1122 Summer 2008

CS162: Introduction to Computer Science II

ECE 122. Engineering Problem Solving with Java

Introduction to Locks. Intrinsic Locks

CS162: Introduction to Computer Science II. Primitive Types. Primitive types. Operations on primitive types. Limitations

11 HashMap: Overriding equals ; JUnit; Vistors

CMSC 341 Lecture 7 Lists

For this section, we will implement a class with only non-static features, that represents a rectangle

Defining Classes and Methods

INHERITANCE. Spring 2019

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

Ch 7 Designing Java Classes & Class structure. Fields have data values define/describe an instance

10 Generating Javadocs; HashMap: Overriding equals

Homework 5. Yuji Shimojo CMSC 330. Instructor: Prof. Reginald Y. Haseltine

COP 3330 Final Exam Review

Programming Languages and Techniques (CIS120)

Announcements/Follow-ups

CS 302 Week 9. Jim Williams

CMSC 132: Object-Oriented Programming II

Lecture 5 Abstract Data Types

Chapter 4. Defining Classes I

Java Foundations. 7-2 Instantiating Objects. Copyright 2015, Oracle and/or its affiliates. All rights reserved.

CS 200 More Classes Jim Williams, PhD

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

Declarations and Access Control SCJP tips

Day 5. COMP1006/1406 Summer M. Jason Hinek Carleton University

CSE 142 Su 04 Computer Programming 1 - Java. Objects

Object-Oriented Design Lecture 3 CSU 370 Fall 2007 (Pucella) Friday, Sep 14, 2007

Appendix: Common Errors

Chapter 4: Writing Classes

CS 302: Introduction to Programming in Java. Lecture 15

Lecture 10 Declarations and Scope

CMSC131. Seating Chart

Inheritance. Lecture 11 COP 3252 Summer May 25, 2017

Chapter 6 Introduction to Defining Classes

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

Overview CSE 142. Naming Revisited Declarations. Defining Parts of Objects

BM214E Object Oriented Programming Lecture 8

Bags. Chapter 1. Copyright 2012 by Pearson Education, Inc. All rights reserved

CSE115 / CSE503 Introduction to Computer Science I. Dr. Carl Alphonce 343 Davis Hall Office hours:

Programming II (CS300)

CMSC131. A Simple Problem?

Objects as a programming concept

An Introduction to C++

Programming II (CS300)

Java Interfaces. 6 April 2016 OSU CSE 1

CSC Java Programming, Fall Java Data Types and Control Constructs

Procedural Java. Procedures and Static Class Methods. Functions. COMP 210: Object-Oriented Programming Lecture Notes 2.

Object Oriented Programming in Java. Jaanus Pöial, PhD Tallinn, Estonia

Lesson 10A OOP Fundamentals. By John B. Owen All rights reserved 2011, revised 2014

10. Object-oriented Programming. 7. Juli 2011

Polymorphism. return a.doublevalue() + b.doublevalue();

C12a: The Object Superclass and Selected Methods

Assertions, pre/postconditions

Copyright 2007 Pearson Addison-Wesley Copyright 2018 Aiman Hanna All rights reserved

Java Memory Management

COMP 202 Recursion. CONTENTS: Recursion. COMP Recursion 1

Chapter 15: Object Oriented Programming

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

Chapter 4. Defining Classes I

Name Return type Argument list. Then the new method is said to override the old one. So, what is the objective of subclass?

The Java Type System (continued)

Equality for Abstract Data Types

Classes Classes 2 / 35

Making New instances of Classes

CS250 Intro to CS II. Spring CS250 - Intro to CS II 1

Discover how to get up and running with the Java Development Environment and with the Eclipse IDE to create Java programs.

WA1278 Introduction to Java Using Eclipse

Java Review. Fundamentals of Computer Science

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

In this lab we will practice creating, throwing and handling exceptions.

CS106A, Stanford Handout #18. Writing a Class

Announcements. Lab tomorrow. Quiz Thursday P3 due Friday. Exceptions and unit testing

10 Java Collections; Equality, JUnit

Day 6. COMP1006/1406 Summer M. Jason Hinek Carleton University

Introduction to Java. Handout-1d. cs402 - Spring

Distributed Systems Recitation 1. Tamim Jabban

CS1004: Intro to CS in Java, Spring 2005

a data type is Types

CS-140 Fall Binghamton University. Methods. Sect. 3.3, 8.2. There s a method in my madness.

Generic BST Interface

Transcription:

CMSC131 Designing Classes Library Classes Due to Java being 100% object-oriented, all code must live inside a class but there is some functionality/information that might be best kept in a more central location. Consider mathematical concepts such as Floor that can be applied to any numeric value and constants such as π. Classes that contain only static fields and methods (such as java.lang.math) are sometimes called library classes and no objects can be created of that type. 1

Datatype Classes Most classes that you will encounter and build will be Datatype Classes where the class defines a structure to hold information as well as functionality on objects with that structure. We have also seen that there are generally useful classes from which we can instantiate objects, such as the String class. Today we will start to talk about creating our own datatype classes for instantiation Designing a Datatype Class Some general considerations when design a new datatype class: Which fields should be static (one shared by all objects and the outside world ) versus instance (one per object, inside the object). Which methods should be static (not associated with a particular object, only have access to static fields) versus instance (have to be invoked by an object, have access to instance fields of that object as well as all static fields). How might objects need to be initialized by our code when first created by a new instruction. What standard functionality should be built. 2

Static Fields With static fields all instances of the class, as well as any other part of the program, share the same single copy of that variable (the data is not part of any individual object). If there is a constant value associated with the class, a final static field would be a very efficient way to provide it. If you wanted to keep track of how many objects of this type were create, a static field can be very useful. These live in a part of Java s memory space called metaspace. Once allocated there, it is never deleted during the execution of the program. Instance Fields With instance fields each instantiated object of the class type has its own unique copy of that variable within it. These are more commonly used since they are what we use to hold information that is specific to an object. These live in a part of Java s memory space called heap. A set of these fields are allocated each time an object of the type is instantiated and are said to live inside the object. They can be primitives or be references to other objects. 3

Static Methods Static methods can be invoked either via the class name or (no recommended) via an object of that class type and the method can only access variables which are static members of the class regardless of which invocation technique is used. Instance Methods Instance methods can be invoked only via an instantiated object of that class type and it can access both variables which are static and variables which are instance These are where most functionality associated with an object is provided. When working inside an instance method, you can refer to fields within the object that invoked the method using a special reference called this which is automatically created. 4

public vs. private We will explore the reasons why we might make some variables and methods public or private (or something else) as we see more about object oriented programming. A common style is to: Make all fields non-public and provide public getter/setter methods if you want to allow other parts of the program to access them. Only make officially supported methods that you want other parts of the program to directly access public. Constructors When a new instance of an object is created, its instance variables can be automatically initialized via a special type of method called a constructor. The name of the constructor method is the same as the name of the class. There is no return value (not even void). There can be multiple constructors, each with different parameter lists (this is known as overloading the constructor method). A highly recommended one is called a copy constructor and is used to make a duplicate of an existing object. 5

Common Standard Functionality There are two instance methods that are considered standard to provide when writing Java datatype classes: public String tostring(); This method is what Java will use (for example) if you try to print the object. public boolean equals(object other); This method is used in placed to allow program and JUnit tests to check whether the information in two objects is the same. What if we wanted a class Point3D Take a moment and think about what you think a Point3D class should have in terms of: data fields stored in each object public methods that could be invoked Chat with your neighbor about this before we go to Eclipse to see what the full class might look like. 6

Copyright 2010-2017 : Evan Golub 7