Classes. Classes as Code Libraries. Classes as Data Structures. Classes/Objects/Interfaces (Savitch, Various Chapters)

Similar documents
Classes. Classes as Code Libraries. Classes as Data Structures

Methods and Data (Savitch, Chapter 5)

Methods. Methods. Mysteries Revealed

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

5. Defining Classes and Methods

Principles of Object Oriented Programming. Lecture 4

Defining Classes and Methods

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

OBJECTS AND CLASSES CHAPTER. Final Draft 10/30/2011. Slides by Donald W. Smith TechNeTrain.com

Lecture Static Methods and Variables. Static Methods

Objectives. Defining Classes and Methods. Objectives. Class and Method Definitions: Outline 7/13/09

Lecture Static Methods and Variables. Static Methods

Defining Classes and Methods. Objectives. Objectives 6/27/2014. Chapter 5

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

5. Defining Classes and Methods

Defining Classes and Methods

Defining Classes and Methods

Defining Classes and Methods

CS 251 Intermediate Programming Methods and Classes

CS 251 Intermediate Programming Methods and More

Java Basics. Object Orientated Programming in Java. Benjamin Kenwright

by Pearson Education, Inc. All Rights Reserved. 2

COMP Information Hiding and Encapsulation. Yi Hong June 03, 2015

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

Object Oriented Programming

Ch 7 Designing Java Classes & Class structure. Methods: constructors, getters, setters, other e.g. getfirstname(), setfirstname(), equals()

Objects as a programming concept

Object Oriented Programming in C#

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

Object Oriented Programming

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

Fundamentos de programação

Programming II (CS300)

Ch 7 Designing Java Classes & Class structure

To define methods, invoke methods, and pass arguments to a method ( ). To develop reusable code that is modular, easy-toread, easy-to-debug,

Software and Programming 1

Programming II (CS300)

Lecture 3. COMP1006/1406 (the Java course) Summer M. Jason Hinek Carleton University

CSC 1051 Algorithms and Data Structures I. Midterm Examination February 25, Name: KEY A

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

Programming II (CS300)

Object-Oriented Programming Concepts

Encapsulation in Java

method method public class Temperature { public static void main(string[] args) { // your code here } new

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

Programming Languages: Encapsulation

MIDTERM REVIEW. midterminformation.htm

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

CIS 110: Introduction to Computer Programming

Outline. CIS 110: Introduction to Computer Programming. Any questions? My life story. A horrible incident. The awful truth

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

CS110: PROGRAMMING LANGUAGE I

Midterms Save the Dates!

Chapter 5 Methods. Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved.

Java and OOP. Part 2 Classes and objects

CS112 Lecture: Defining Instantiable Classes

CS1083 Week 2: Arrays, ArrayList

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

Chapter 5 Methods / Functions

Chapter 4 Defining Classes I

COMSC-051 Java Programming Part 1. Part-Time Instructor: Joenil Mistal

Midterms Save the Dates!

Mathematical Functions, Characters, and Strings. CSE 114, Computer Science 1 Stony Brook University

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

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

JAVA Programming Concepts

This exam is open book. Each question is worth 3 points.

Classes and Methods: Classes

CS 302 Week 9. Jim Williams

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

Software and Programming 1

Java Magistère BFA

Mathematical Functions, Characters, and Strings. CSE 114, Computer Science 1 Stony Brook University

Introduction to Programming Using Java (98-388)

Chapter 6 Methods. Dr. Hikmat Jaber

CSC Algorithms and Data Structures I. Midterm Examination February 25, Name:

CS 2530 INTERMEDIATE COMPUTING

Recitation 3 Class and Objects

Chapter 4: Writing Classes

Week 11: Class Design

CISC-124. Passing Parameters. A Java method cannot change the value of any of the arguments passed to its parameters.

Assignment 1 due Monday at 11:59pm

AP Computer Science Unit 1. Programs

Computer Science II (20073) Week 1: Review and Inheritance

Software and Programming 1

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

SOAPScript For squeaky-clean code, use SOAPScript Language Summary Bob Nisco Version 0.5 β

CIS3023: Programming Fundamentals for CIS Majors II Summer 2010

Java. Classes 3/3/2014. Summary: Chapters 1 to 10. Java (2)

University of Cape Town ~ Department of Computer Science. Computer Science 1015F ~ 2007

Lecture 12: Classes II

Chapter 4. Defining Classes I

Simple Java Reference

Anatomy of a Class Encapsulation Anatomy of a Method

Chapter 5 Methods. Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved.

Encapsulation. Mason Vail Boise State University Computer Science

Object Oriented Programming

Preview from Notesale.co.uk Page 3 of 36

CS111: PROGRAMMING LANGUAGE II

Chapter 6 Lab Classes and Objects

Transcription:

Classes Classes/Objects/Interfaces (Savitch, Various Chapters) TOPICS Classes Public versus Private Static Data Static Methods Interfaces Classes are the basis of object-oriented (OO) programming. They encapsulate functionality to form powerful abstractions of real world objects. What can classes be used for? Classes have many different uses, for example: Data Structures Code Libraries Java Programs Complex Objects 2 Classes as Data Structures Just like a struct in C and C++ (no code), for example: public class Student { public String firstname; public String lastname; public Date birthdate; public Address homeaddress; public double gradepointaverage; Classes as Code Libraries Just like a library in a procedural language (no data) like C or C++, for example: public class Math { public static final double PI = 3.14159; public static double sin(double a) { public static double exp(double a) { public static double log(double a) { public static double sqrt(double a) { 3 4

Classes as Small Programs Classes as Large Programs Just like a program in a procedural language like C or C++, for example: public class MySmallProgram{ public static void main(string args[]) { System.out.println( Hello, World! ); Just like a program in a procedural language like C or C++, for example: public class MyLargeProgram{ // lots of data public static void main(string args[]) { // lots of code // lots of methods 5 6 Classes as Complex Objects Using Different Class Types No comparable example in a procedural language like C or Pascal! public class MyClass { // class variables (static) // instance variables (non-static) // no main method // class methods (static) // instance methods (non-static) // Data Structure Student students[] = new Student[100]; students[0].firstname = Christopher ; // Code Library System.out.println(Math.sin(1.0)); // Java Programs $ java MySmallProgram // Complex Objects MyClass myclass = new MyClass(); myclass.initialize(); 7 8

public Public versus Private Can access the class, method, or data by name outside defining class private Can access the class, method, or data by name only inside defining class Classes generally specified as public Instance variables usually are private Methods can be public or private Static (versus non-static) Local data resides in a method, class and instance data resides in the class. Data defined in the class can be of two types: Data may belong to the class (only one copy that has the same value for all objects of that class) Data may belong to the object (separate storage and different values for each instance) Class data is identified by static Instance data is not be static 9 10 Class versus Instance: Data Class Methods public class MyClass { static int int0; double real0; int0 resides in the class (1 copy), use class name to access: MyClass.int0 = 123; real0 resides in an instance (many copies): MyClass m = new MyClass; m.real0 = 1.234; Notation: class.method() Does not require instantiation, should be called using the class name. Class name is not necessary if within the class itself. Marked as static, and cannot access instance data, since no instance is used to call them. Useful for library methods that do not need any associated data: for example Math.sin(); 11 12

Instance Methods Notation: objectname.method() Must be called on an object instantiated from a class. Most objects are instantiated with the new keyword: String word = new String( Whatever ); For example, calling word.length() requires an object of type String called word. The length() method uses the data for the specific instance it is called on. Constructors Q: How to initialize instance data when creating an object? A: Use a constructor. Constructors are used to create objects and send them data. Always named after the class, arbitrary parameters, but no return value! Example: Scanner s = new Scanner(System.in); 13 14 Methods inside a class Encapsulation Order of writing methods is arbitrary Generally constructors are written first. What if two methods need to share data? Can pass data around as parameters and return values. Alternative: specifying class or instance data allows access from multiple methods. Consider example of driving a car We see and use break pedal, accelerator pedal, steering wheel we know what they do. We do not see mechanical details of how they do their jobs. We can divide class definition into different parts to support encapsulation: Class interface Class implementation 15 16

Interface/Implementation A class interface Tells the user or programmer what the class does Gives headings for public methods and comments about them A class implementation Contains private variables Includes definitions of public and private methods, i.e. how the class does what it does Interfaces Interface definition is pure functionality, a set of methods and a description of what they do (Javadoc). Instance variables in the class usually are declared as private to prevent direct access. Provide public accessor (getter) methods to read data. Provide public mutator (setter) methods to write data. Add comments before each public method heading that fully specifies how to use method. Hide everything else: data structures, helper methods, algorithms - all implementation details. 17 18 Interface Example Encapsulation/Interfaces public interface Trigonometry { public double sin(double angle); public double cos(double angle); public double tan(double angle); public class Math implements Trigonometry { public double sin(double angle) { code public double cos(double angle) { code public double tan(double angle) { code Trigonometry trig = new Math(); A well encapsulated class definition: Programmer who uses the class 19 20

Why Interfaces? Interfaces are pure functionality, a designer can specify what the software does without worrying about how. Provides a methodology for partitioning large systems without having to consider all of the details. Interfaces can (and should) be designed based on user requirements and the intended functionality. Interfaces hide implementation details, user of class does not need to see internal code or data structures. Allows shipment of class files to protect intellectual property while still communicating functionality. 21