Objects and Classes Chapter 6 CSCI 1302. CSCI 1302 – Objects and Classes2 Outline Introduction Defining Classes for Objects Constructing Objects Accessing.

Slides:



Advertisements
Similar presentations
Chapter 6 Objects and Classes F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive data type and.
Advertisements

L3:CSC © Dr. Basheer M. Nasef Lecture #3 By Dr. Basheer M. Nasef.
Chapter 10 THINKING IN OBJECTS 1 Object Oriented programming Instructor: Dr. Essam H. Houssein.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 7 Objects and Classes.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Immutable Objects and Classes.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 8 Objects and Classes.
Road Map Introduction to object oriented programming. Classes
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
1 Chapter 2 Objects and Classes. 2 Objectives F To understand objects and classes and use classes to model objects. F To learn how to declare a class.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 7 Objects and Classes.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 9 Objects and Classes.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 9 Thinking in Objects.
Introduction to Java Programming, 4E Y. Daniel Liang.
Advanced Java and Android Day 1 Object-Oriented Programming in Java Advanced Java and Android -- Day 11.
Chapter 9 Objects and Classes
1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,
Syllabus (1) WeekChapters 1Introduction to the course, basic java language programming concepts: Primitive Data Types and Operations 1, 2 2Methods, Control.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 9 Objects and Classes.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
1 Objects and Classes. 2 OO Programming Concepts Object-oriented programming (OOP) involves programming using objects. An object represents an entity.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 8 Objects and Classes.
1 Chapter 8 – Classes and Object: A Deeper Look Outline 1 Introduction 2 Implementing a Time Abstract Data Type with a Class 3 Class Scope 4 Controlling.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 7 Objects and Classes.
1.  At the end of this slide, student able to:  Object-Oriented Programming  Research on OOP features.  Do a code walkthrough to examine the implementation.
Chapter 8 Objects and Classes Object Oriented programming Instructor: Dr. Essam H. Houssein.
Liang,Introduction to Java Programming,revised by Dai-kaiyu 1 Chapter 6 Objects and Classes She calls out to the man on the street Sir can you help me.
Objects and Classes Mostafa Abdallah
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Chapter 3 Introduction to Classes and Objects Definitions Examples.
IT108 Objects and Classes Part I George Mason University Revised 4/3/2012.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 9 Objects and Classes.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
1 COS240 O-O Languages AUBG, COS dept Lecture 12 Title: Java Classes and Objects Reference: COS240 Syllabus.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 7 Objects and Classes.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Objects and Classes.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 9 Objects and Classes.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 6 Objects and Classes.
1 Chapter 6 Programming with Objects and Classes F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive.
Introduction To Objects Oriented Programming Instructor: Mohammed Faisal.
Chapter 7 Objects and Classes. OO Programming Concepts Object-oriented programming (OOP) involves programming using objects. An object represents an entity.
CS 112 Programming 2 Lecture 02 Objects and Classes (1)
5.1 Basics of defining and using classes A review of class and object definitions A class is a template or blueprint for an object A class defines.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 7 Objects and Classes.
Object-Oriented Programming in Java. 2 CS2336: Object-Oriented Programming in Java Buzzwords interfacejavadoc encapsulation coupling cohesion polymorphic.
1 COS240 O-O Languages AUBG, COS dept Lecture 12 Title: Java Classes and Objects Reference: COS240 Syllabus.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Fall 2013 Chapter 10 Thinking.
Lecture 9: Object and Classes Michael Hsu CSULA. 2 OO Programming Concepts Object-oriented programming (OOP) involves programming using objects. An object.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 9 Introduction of Object Oriented Programming.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Lecture 3: Introduction to Object and Classes Michael Hsu CSULA.
Lecture 3: Introduction to Object and Classes Michael Hsu CSULA.
Objects and Classes. F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive data type and object type.
1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,
Chapter 7 Objects and Classes
Lecture 3: Introduction to Object and Classes
HIGHLEVEL REVIEW Chapter 9 Objects and Classes
Corresponds with Chapter 7
Chapter 9 Objects and Classes
Chapter 9 Objects and Classes
Chapter 6 Objects and Classes
Chapter 6 Objects and Classes
Chapter 9 Objects and Classes Part 01
Chapter 7 Objects and Classes
OO Programming Concepts
Chapter 6 Objects and Classes
Chapter 7 Objects and Classes
Presentation transcript:

Objects and Classes Chapter 6 CSCI 1302

CSCI 1302 – Objects and Classes2 Outline Introduction Defining Classes for Objects Constructing Objects Accessing Objects –Reference Variables and Types –Accessing an Object’s Data and Methods –The null Value –Differences Between Primitive and Reference Variable Types

CSCI 1302 – Objects and Classes3 Outline Using Classes from the Java Library Visibility Modifiers, Accessors, and Mutators Data Field Encapsulation Immutable Objects and Classes Passing Objects to Methods Static Variables, Constants, and Methods

CSCI 1302 – Objects and Classes4 Outline The this keyword Array of Objects Class Abstraction and Encapsulation Inner Classes Case Study: StackofIntegers

CSCI 1302 – Objects and Classes5 Introduction Procedural programming – data and operations are separate Object-oriented programming (OOP) – groups data and operations into a single entity Improves reusability, development, and maintenance

CSCI 1302 – Objects and Classes6 Defining Classes for Objects An object represents a real-world entity Has a unique identity, state, and behaviors State consists of data fields (properties) Behavior is defined by a set of methods State defines the object, behavior defines what it does

CSCI 1302 – Objects and Classes7 Defining Classes for Objects

CSCI 1302 – Objects and Classes8 Defining Classes for Objects Constructs that define objects of the same type Java classes use variables to define data fields and methods to define behaviors Also provide constructors used to create objects from the class

CSCI 1302 – Objects and Classes9 Defining Classes for Objects

CSCI 1302 – Objects and Classes10 Defining Classes for Objects

CSCI 1302 – Objects and Classes11 Constructing Objects Circle() { } Circle(double newRadius) { radius = newRadius; } Constructors with no parameters are referred to as no-arg constructors.

CSCI 1302 – Objects and Classes12 Constructing Objects Constructors Have the same name as the defining class Can be overloaded No return type Invoked using the new operator If no constructor is explicitly declared, a default no-arg constructor is created with an empty body

CSCI 1302 – Objects and Classes13 Constructing Objects Examples General new ClassName(); Specific new Circle(); new Circle(5.0);

CSCI 1302 – Objects and Classes14 Constructing Objects

CSCI 1302 – Objects and Classes15 Accessing Objects Can be accessed through reference variables ClassName objectRefVar; Circle myCircle; Class defines a reference type myCircle = new Circle(); One statement Circle myCircle = new Circle(); An object created without a reference is an anonymous object

CSCI 1302 – Objects and Classes16 Accessing Objects Accessing Data and Methods Can be accessed using dot notation objectRefVar.data; objectRefvar.method(arguments); Circle class: myCircle.radius; myCircle.findArea(); See the code in Circle.java and TestCircle.java for examples

CSCI 1302 – Objects and Classes17 The null value Default literal value for reference type variables Default values of data fields: No default values for local variables inside a method Reference typenull Numeric type0 booleanfalse char‘\u0000’

CSCI 1302 – Objects and Classes18 Differences Between Variable Types Two main types of variables: Primitive and Reference Primitive –Value is of the primitive type –Assignment sets the real value, copies contents Reference –Value is a reference to where an object is located –Assignment sets reference

CSCI 1302 – Objects and Classes19 Differences Between Variable Types Garbage collection reclaims previous c1 object automatically (can assign null to force collection) See code in TestVarTypes.java

CSCI 1302 – Objects and Classes20 Using Classes from the Java Library Classes from the Java Library will often be used when developing programs You have already done this: System.out.println(“Some text here”); See Example 2.4 (p. 61) for an example using System.currentTimeMillis(); Promotes reuse of code

CSCI 1302 – Objects and Classes21 Using Classes from the Java Library Example: java.util.Date Provides a system-independent encapsulation of date and time Can be used to create an instance of the current date and time, use its toString() method to print time out Format: Mon Aug 15 13:19:37 EDT 2005 See TestDate.java and TestFrame.java

CSCI 1302 – Objects and Classes22 Visibility Modifiers Java provides several modifiers to control access to data, methods, and classes –public – Accessible from any class –private – Accessible only from within its own class –Package-private – used if public or private is not specified, accessible by any class in the same package

CSCI 1302 – Objects and Classes23 Visibility Modifiers Only used for members of a class, not local variables inside methods

CSCI 1302 – Objects and Classes24 Data Field Encapsulation Declare the field private Use get (or accessor) methods to return values of data fields public returnType getProperty(); public boolean isProperty(); Use set (or mutator) methods to update data fields public void setProperty(dataType property); See ExtendedCircle.java

CSCI 1302 – Objects and Classes25 Immutable Objects and Classes An object, that once created, cannot be changed is an immutable object The class it is derived from is an immutable class A class with all private data fields and no mutator methods is not necessarily immutable (p. 227) See Chair.java for an example of a immutable class

CSCI 1302 – Objects and Classes26 Passing Objects to Methods Objects can be passed to methods Reference to the object is what is actually passed Java passes all arguments using pass-by- value See TestPassObject.java for a simple example and TestPassObjectExtended.java for a more complex example

CSCI 1302 – Objects and Classes27 Static Variables, Constants, and Methods Instance variables are tied to specific instances of a class Static variables are shared between all instances of a class Static methods can be called without creating an instance of a class Declared using static keyword Class constants can be declared using the final keyword

CSCI 1302 – Objects and Classes28 Static Variables, Constants, and Methods

CSCI 1302 – Objects and Classes29 Scope of Variables Class variables have a scope of the entire class Methods and variables can be declared in any order in the class except when their value is based on other variables Local variables take precedence over class variables although it is bad form to reuse variable names in most cases

CSCI 1302 – Objects and Classes30 The this keyword Used to refer to the invoking class Used most often when parameter name in a method matches a data field in a class public void setRadius(double radius) { this.radius = radius; } See ExtendedChair.java for example of constructor using this See TestChair.java

CSCI 1302 – Objects and Classes31 Array of Objects Object arrays can be created the same way that primitive arrays are created Circle[] circleArray = new Circle[10]; To initialize the array, use a for loop similar to this one for (int i = 0; i < circleArray.length; i++) { circleArray[i] = new Circle(); }

CSCI 1302 – Objects and Classes32 Array of Objects Actually involves two levels of referencing circleArray is an array of reference variables See TestCircleArray.java

CSCI 1302 – Objects and Classes33 Class Abstraction and Encapsulation Class abstraction – Separation of class implementation from the use of a class Class contract – Collection of methods and fields that are accessible from outside the class, along with the description of their behavior Class encapsulation – Details of implementation are encapsulated and hidden from the user

CSCI 1302 – Objects and Classes34 Inner Classes A class defined within the scope of another class (AKA nested class) Can reference the data and methods defined in the outer class Can be declared public, protected, or private Can be declared static with some restrictions on access to the outer class

CSCI 1302 – Objects and Classes35 Inner Classes Objects for inner classes are often created in the outer class Can be created from outside classes Non-static: OuterClass.InnerClass innerObject = outerObject.new InnerClass(); Static: OuterClass.InnerClass innerObject = new OuterClass.InnerClass();

CSCI 1302 – Objects and Classes36 Case Study: StackofIntegers Data structure that holds objects in a last-in first-out (LIFO) fashion Stack operations: pop(), push(), and peek() Array-based implementation Stacks are used often in programming Java provides a full featured version in java.util.Stack

CSCI 1302 – Objects and Classes37 Case Study: StackofIntegers