CS2011 Introduction to Programming I Objects and Classes

Slides:



Advertisements
Similar presentations
Looking inside classes Fields, Constructors & Methods Week 3.
Advertisements

Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-3: Encapsulation, toString reading: self-checks: #13-18,
Road Map Introduction to object oriented programming. Classes
1 Classes Overview l Classes as Types l Declaring Instance Variables l Implementing Methods l Constructors l Accessor and Mutator Methods.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
Introduction to Data Structures, Spring 2007 Slide- 1 California State University, Fresno Introduction to Data Structure Object Oriented Programming Concepts.
Object Oriented Concepts in Java Objects Inheritance Encapsulation Polymorphism.
© The McGraw-Hill Companies, 2006 Chapter 7 Implementing classes.
1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,
Object Oriented Software Development
Programming Languages and Paradigms Object-Oriented Programming.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.
1 OOP in C#: Object Interaction. Inheritance and Polymorphism OOP in C# - Del 1.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
Classes CS 21a: Introduction to Computing I First Semester,
Chapter 7 Objects and Classes 1 Fall 2012 CS2302: Programming Principles.
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Behavior (Methods) and Constructors, Encapsulation, this reading:
Object Based Programming Chapter 8. 2 In This Chapter We will learn about classes Garbage Collection Data Abstraction and encapsulation.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Chapter 4 Introduction to Classes, Objects, Methods and strings
Session 02 and 03: C# OOP 1 OOP in C#: Classes and Objects in C#. Object-Oriented Design. UML Class Diagram. Object Interaction. FEN AK IT:
CSE 501N Fall ‘09 04: Introduction to Objects 08 September 2009 Nick Leidenfrost.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
IT108 Objects and Classes Part I George Mason University Revised 4/3/2012.
CIS 270—Application Development II Chapter 8—Classes and Objects: A Deeper Look.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 6 Objects and Classes.
Computer Programming 2 Lecture 8: Object Oriented Programming Creating Classes & Objects Prepared & Presented by: Mahmoud Rafeek Alfarra MINISTRY OF EDUCATION.
CS1101 Group1 Discussion 6 Lek Hsiang Hui comp.nus.edu.sg
Object Oriented Programming Session # 03.  Abstraction: Process of forming of general and relevant information from a complex scenarios.  Encapsulation:
Object Oriented Programming and Data Abstraction Rowan University Earl Huff.
OOP Basics Classes & Methods (c) IDMS/SQL News
CS 100Lecture71 CS100J Lecture 7 n Previous Lecture –Computation and computational power –Abstraction –Classes, Objects, and Methods –References and aliases.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
OOP in C# - part 1 OOP in C#: –Object Interaction. –Inheritance and Polymorphism (next module). FEN 20121UCN Technology: Computer Science.
Object Based Programming Chapter 8. 2 Contrast ____________________ Languages –Action oriented –Concentrate on writing ________________ –Data supports.
Java 5 Class Anatomy. User Defined Classes To this point we’ve been using classes that have been defined in the Java standard class library. Creating.
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Behavior (Methods) and Constructors, Encapsulation, this reading:
Andrew(amwallis) Classes!
Classes and OOP.
Object-Oriented Programming: Classes and Objects
Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“ “Inheritance is new code that reuses old code. Polymorphism.
CSC 143 Inheritance.
Object Based Programming
Chapter 3 Introduction to Classes, Objects Methods and Strings
Chapter 4: Writing classes
An Introduction to Java – Part II
Variables and Their scope
Implementing Classes Chapter 3.
Building Java Programs
CS2011 Introduction to Programming I Arrays (II)
CS2011 Introduction to Programming I Methods (II)
CS2011 Introduction to Programming I Methods (I)
CS100J Lecture 7 Previous Lecture This Lecture Java Constructs
COP 3330 Object-oriented Programming in C++
JAVA CLASSES.
By Rajanikanth B OOP Concepts By Rajanikanth B
Object Oriented Programming in java
Classes CS 21a: Introduction to Computing I
Dr. R Z Khan Handout-3 Classes
CS3220 Web and Internet Programming About Data Models
Chapter 11 Inheritance and Encapsulation and Polymorphism
Object-Oriented Programming and class Design
Chengyu Sun California State University, Los Angeles
Chengyu Sun California State University, Los Angeles
Introduction to Computer Science and Object-Oriented Programming
Presentation transcript:

CS2011 Introduction to Programming I Objects and Classes Chengyu Sun California State University, Los Angeles

Represent Data To process data using computers, we first need to represent the data in a programming language 10 int 3.1415 double "hello" String 90, 100, 95 int[] 90, 100, 95 85, 90, 100 79, 80, 90 int[][]

Example: Banking System Create a banking system that helps a bank to manage customers and accounts, e.g. open an account, get the balance of an account, deposit, withdraw, transfer, and so on How do we represent customers and accounts??

What Information Do We Need to Keep? Customer Account name String id int address String balance double owner ??

One Way to Implement a Bank String[] customerNames; String[] customerAddresses; int[] accountIds; double[] accountBalances; String[] accountOwnerNames;

A Better Way to Implement A Bank class Customer { String name; String address; } class Account { int id; double balance; Customer owner; } Customer[] customer; Account[] accounts;

The Object-Oriented Paradigm … Computer programs are usually created to facilitate real-world tasks A good programming language should make it easier to mimic (i.e. model) real-world matters

… The Object-Oriented Paradigm The world consists of objects Each object has some attributes, and is often associated with some operations The same type of objects share the same set of attributes and/or operations

From Concept to Code Type Class Attributes Variables Operations Methods Objects in the real world Objects in programs

Class in Java Class name Access modifier Fields, a.k.a. instance variables public class Customer { String name; String address; public Customer(String cname, String caddress) { name = cname; address = caddress; } Constructor

About Constructor A constructor is a special method: no return type, and same name as class Constructors are used to create new objects (often called instances) of a class A class must have at least one constructor If a class doesn’t have a constructor, JVM will automatically creates one for the class, known as the default constructor

Create and Access An Object Customer c = new Customer("John", "123 Main St"); Use the new keyword like when creating an array Call one of the constructors System.out.println( c.name ); Use the . operator to access object fields and methods

Class As Type A class can be considered a user-defined type Type Customer c = new Customer("John", "123 Main St"); Type Customer[] customers = new Customer[100];

Banking System: Data A bank manages a number of customers and accounts Use arrays to store customers and accounts Use counts to keep track the actual number of customers and accounts

Classes Are More Than Just Data Example: add account operations like deposit, withdraw, and get balance

static vs Non-static public class Foo { static int a = 0; int b; Foo() { b = 0; } public void inc() { ++a; ++b; } public void print() System.out.println(a); System.out.println(b); A static member of a class is shared by all objects of the class Foo f1 = new Foo(); Foo f2 = new Foo(); f1.print(); f2.print(); // ?? f1.inc(); f1.print(); // ?? f2.inc(); f2.print(); // ??

Return An Object Example: open an account in a bank (name, address, initial deposit) Just like arrays, objects are created on the heap Returning an object means returning the reference (i.e. the address) of the object

Pass Object to Method Just like arrays, object parameters are pass-by-reference Example: transfer money from one account to another

Keyword this A reference to an object itself De-shadowing A reference to a constructor public class Foo { int x; public Foo( int x ) { this.x = x; } public Foo() { this(-1); The parameter x shadows the field x, meaning x inside the method refers to the parameter instead of the field

More About OO Programming Encapsulation Inheritance Polymorphism

Readings Chapter 9 of the textbook