Lecture 8 Classes and Objects Part 2. MIT AITI June 15th, 2005

Similar documents
Lecture 7 Objects and Classes

Lecture 06: Classes and Objects

Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson

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

CS 251 Intermediate Programming Methods and More

Recitation 3 Class and Objects

Introduction to Programming Using Java (98-388)

Lecture 9: Lists. MIT-AITI Kenya 2005

CS 251 Intermediate Programming Methods and Classes

6.092: Java for 6.170

CSE 142 Su 04 Computer Programming 1 - Java. Objects

News and information! Review: Java Programs! Feedback after Lecture 2! Dead-lines for the first two lab assignment have been posted.!

Lecture 3. Lecture

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

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

Java Programming. MSc Induction Tutorials Stefan Stafrace PhD Student Department of Computing

PROGRAMMING FUNDAMENTALS

1.00 Introduction to Computers and Engineering Problem Solving. Quiz 1 March 7, 2003

Object Oriented Design: Identifying Objects

Java Identifiers, Data Types & Variables

Lecture 1: Overview of Java

Chapter 4 Defining Classes I

VARIABLES AND TYPES CITS1001

Lecture 5: Arrays. A way to organize data. MIT AITI April 9th, 2005

CS61BL. Lecture 1: Welcome to CS61BL! Intro to Java and OOP Testing Error-handling

Fundamentals of Programming Data Types & Methods

Argument Passing All primitive data types (int etc.) are passed by value and all reference types (arrays, strings, objects) are used through refs.

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.

Lecture 6 Introduction to Objects and Classes

Methods and Data (Savitch, Chapter 5)

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

Introduction to Java

COMP-202: Foundations of Programming. Lecture 5: Arrays, Reference Type, and Methods Sandeep Manjanna, Summer 2015

Object-Oriented Programming Concepts

Principles of Object Oriented Programming. Lecture 4

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

9 Working with the Java Class Library

1.00 Lecture 6. Java Methods

OOPs Concepts. 1. Data Hiding 2. Encapsulation 3. Abstraction 4. Is-A Relationship 5. Method Signature 6. Polymorphism 7. Constructors 8.

Object Oriented Programming

Objects and Classes. Amirishetty Anjan Kumar. November 27, Computer Science and Engineering Indian Institue of Technology Bombay

Lecture 05: Methods. AITI Nigeria Summer 2012 University of Lagos.

Lecture 2. COMP1406/1006 (the Java course) Fall M. Jason Hinek Carleton University

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

CSC Java Programming, Fall Java Data Types and Control Constructs

The high-level language has a series of primitive (builtin) types that we use to signify what s in the memory

1 Shyam sir JAVA Notes

Programming Language Concepts: Lecture 2

JAVA GUI PROGRAMMING REVISION TOUR III

1.00 Lecture 4. Promotion

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

Object oriented programming. Instructor: Masoud Asghari Web page: Ch: 3

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

CS 11 java track: lecture 1

A Foundation for Programming

1.00/1.001 Introduction to Computers and Engineering Problem Solving Spring Quiz 1

1 Method Signatures and Overloading (3 minutes, 2 points)

6.096 Introduction to C++ January (IAP) 2009

Course Outline. Introduction to java

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

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

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

Remedial classes. G51PRG: Introduction to Programming Second semester Lecture 2. Plan of the lecture. Classes and Objects. Example: Point class

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

Programming for Mobile Computing

ECE 122. Engineering Problem Solving with Java

Contents. I. Classes, Superclasses, and Subclasses. Topic 04 - Inheritance

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

A foundation for programming. Classes and objects. Overview. Java primitive types. Primitive types Creating your own data types

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

CS 231 Data Structures and Algorithms, Fall 2016

Java Magistère BFA

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

5. Defining Classes and Methods

Last Name: Circle One: OCW Non-OCW

Comp 248 Introduction to Programming Chapter 4 - Defining Classes Part A

Following is the general form of a typical decision making structure found in most of the programming languages:

CHAPTER 7 OBJECTS AND CLASSES

Software and Programming 1

Java Object Oriented Design. CSC207 Fall 2014

Array. Prepared By - Rifat Shahriyar

1.00 Lecture 13. Inheritance

CS260 Intro to Java & Android 03.Java Language Basics

Defining Classes and Methods

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

6.170 Lecture 7 Abstract Data Types MIT EECS

13 th Windsor Regional Secondary School Computer Programming Competition

Software Practice 1 - OOP (1) Class and Method What is OOP? Defining Classes Using Classes References vs. Values Encapsulate

Computing Science 114 Solutions to Midterm Examination Tuesday October 19, In Questions 1 20, Circle EXACTLY ONE choice as the best answer

CISC-124. Dog.java looks like this. I have added some explanatory comments in the code, and more explanation after the code listing.

CS 177 Week 15 Recitation Slides. Review

Defining Classes and Methods

Object Oriented Programming

Lec 3. Compilers, Debugging, Hello World, and Variables

McGill University School of Computer Science COMP-202A Introduction to Computing 1

Functions. x y z. f (x, y, z) Take in input arguments (zero or more) Perform some computation - May have side-effects (such as drawing)

Building Java Programs

1.00/1.001 Tutorial 1

School of Computer Science CPS109 Course Notes 5 Alexander Ferworn Updated Fall 15

Software and Programming 1

Transcription:

Lecture 8 Classes and Objects Part 2 MIT AITI June 15th, 2005 1

What is an object? A building (Strathmore university) A desk A laptop A car Data packets through the internet 2

What is an object? Objects have two parts: State: Properties of an object. Behavior: Things the object can do. Car Example: State: Color, engine size, automatic Behavior: Brake, accelerate, shift gear Person Example: State: Height, weight, gender, age Behavior: Eat, sleep, exercise, study 3

Why use objects? Modularity: Once we define an object, we can reuse it for other applications. Information Hiding: Programmers don t need to know exactly how the object works. Just the interface. Example: Different cars can use the same parts. You don t need to know how an engine works in order to drive a car. 4

Classes A class is a template or pattern from which objects are created A class contains Data members (Properties/Characteristics of the objects/class) Methods (Determines the behavior of the objects created from the class) Constructor (Special Method) 5

Anatomy of a class You have all seen classes in your labs Basic anatomy public class classname{ Data members Constructor Methods 6

Constructors Constructors provide objects with the data they need to initialize themselves, like How to Assemble instructions. Objects have a default constructor that takes no arguments, like LightSwitch(). We can define our own constructors that take any number of arguments. Constructors have NO return type and must be named the same as the class: ClassName(argument signature) { body 7

Recall the LightSwitch Class class LightSwitch { boolean on = true; The keyword class tells java that we re defining a new type of Object. Classes are a blueprint. Objects are instances of classes. Everything in Java (except primitives) are Objects and have a Class. 8

Using Objects public static void main(string[] args) { LightSwitch s = new LightSwitch(); System.out.println(s.isOn); s.flip(); System.out.println(s.isOn); The new keyword creates a new object. new must be followed by a constructor. We call methods like: variablename.methodname(arguments) 9

The LightSwitch Class class LightSwitch { boolean on = true; boolean ison() { return on; void switch() { on =!on; 10

A Different LightSwitch Class class LightSwitch { int on = 1; boolean ison() { return on == 1; void switch() { on = 1 - on; 11

Abstraction Both LightSwitch classes behave the same. We treat LightSwitch as an abstraction: we do not care about the internal code of LightSwitch, only the external behavior Internal code = implementation External behavior = interface 12

Why is Abstraction Important? We can continue to refine and improve the implementation of a class so long as the interface remains the same. All we need is the interface to an Object in order to use it, we do not need to know anything about how it performs its prescribed behavior. In large projects involving several teams, programmers only need to know what is necessary for their part of the code (eg. Microsoft, Google, Goldman Sachs, Morgan Stanley and other financial companies) 13

Breaking the Abstraction Barrier A user of LightSwitch that relied on the boolean field would break if we changed to an integer field class AbstractionBreaker { public static void main(string[] args) { LightSwitch ls = new LightSwitch(); if (ls.on) // now broken! System.out.println("light is on"); else System.out.println("light is off"); 14

Public versus Private Label fields and methods private to ensure other classes can't access them Label fields and methods public to ensure other classes can access them. If they are not labeled public or private, for now consider them public. 15

A Better LightSwitch class LightSwitch { private boolean on = true; public boolean ison() { return on; public void switch() { on =!on; 16

Enforcing the Abstraction Barrier By labeling the on field private... class LightSwitch { private boolean on = true; //... Now AbstractionBreaker's attempt to access the on field would not have compiled to begin with. if (ls.on) // would never have compiled 17

Primitives vs Objects Two datatypes in Java: primitives and objects Primitives: byte, short, int, long, double, float, boolean, char == tests if two primitives have the same value Objects: defined in Java classes == tests if two objects are the same object 18

References The new keyword always constructs a new unique instance of a class When an instance is assigned to a variable, that variable is said to hold a reference or point to that object Person g = new Person( Mwangi", 21); Person h = new Person( Mwangi", 21); g and h hold references to two different objects that happen to have identical state 19

Reference Inequality g!= h because g and h hold references to different objects Person g = new Person( Mwangi", 21); Person h = new Person( Mwangi", 21); g h Mwangi" 21 20 Mwangi" 21

Reference Equality greg1 == greg2 because greg1 and greg2 hold references to the same object Person greg1 = new Person("Greg", 23); Person greg2 = greg1; greg1 "Greg" 23 greg2 21

Is (a == b)? int a = 7; int b = 7; Answer: Yes Is (g == h)? Equality Quiz 1 Person g = new Person( Mwangi", 21); Person h = new Person( Mwangi", 21); Answer: No 22

true or false? Equality Quiz 2 Person g = new Person( James", 22); Person h = new Person("James", 22); Person lucy1 = new Person( Lucy", 19); Person lucy2 = lucy1; a) g == h b) g.getage() == h.getage() c) lucy1 == lucy2 d) lucy1.getage() == lucy2.getage(); false true true true 23

Java API You can get information on all in-built Java classes/methods by browsing the Java Application Programming Interface (API) This documentation is essential to building any substantial Java application 24

MIT OpenCourseWare http://ocw.mit.edu EC.S01 Internet Technology in Local and Global Communities Spring 2005-Summer 2005 For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms.