Chapter 9 Objects and Classes Part 01

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.
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 Chapter 8 Objects and Classes.
Road Map Introduction to object oriented programming. Classes
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.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 6 Arrays (continued)
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.
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.
Objects and Classes Chapter 6 CSCI CSCI 1302 – Objects and Classes2 Outline Introduction Defining Classes for Objects Constructing Objects Accessing.
1 Introducing Methods A method is a collection of statements that are grouped together to perform an operation.
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.
Objects and Classes Mostafa Abdallah
COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi.
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.
Liang, Introduction to Java Programming, Sixth Edition1 Objects and Classes Gang Qian Department of Computer Science University of Central Oklahoma.
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 Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 6 Objects and Classes.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 7 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.
CS 139 Objects Based on a lecture by Dr. Farzana Rahman Assistant Professor Department of Computer Science.
Introduction To Objects Oriented Programming Instructor: Mohammed Faisal.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 8 Objects and Classes.
1 Class and Object Lecture 7. 2 Classes Classes are constructs that define objects of the same type. A Java class uses instance variables to define data.
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)
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.
1 Chapter 9 Objects and Classes. 2 OO Programming in Java Other than primitive data types (byte, short, int, long, float, double, char, boolean), everything.
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 Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. P ART 1: O BJECTS AND C LASSES 1.
Dr. Majed Abdouli © Objects and Classes 1 Dr. Majed Abdouli © 2015, adapted from Liang, Introduction to Java Programming, Eighth Edition, (c) 2011.
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,
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 8 Objects and Classes.
Chapter 7 Objects and Classes
Lecture 3: Introduction to Object and Classes
Chapter 7 Object-Oriented Programming
Road Map Introduction to object oriented programming. Classes
HIGHLEVEL REVIEW Chapter 9 Objects and Classes
Chapter 8 Objects and Classes Part 1
Chapter 9 Objects and Classes
Chapter 9 Objects and Classes
Chapter 8 Objects and Classes
Objects and Classes Creating Objects and Object Reference Variables
Chapter 9 Objects and Classes
Chapter 6 Objects and Classes
Chapter 6 Objects and Classes
Chapter 8 Objects and Classes
Chapter 7 Objects and Classes
OO Programming Concepts
Chapter 6 Objects and Classes
Chapter 9 Objects and Classes
Chapter 7 Objects and Classes
Presentation transcript:

Chapter 9 Objects and Classes Part 01

Object-Oriented Programming Object-oriented programming (OOP) involves programming using objects An object represents an entity in the real world that can be distinctly identified Examples: A student, a desk, a circle, and even a loan can all be viewed as objects Each object has a unique identity, state, and behaviors: State of an object consists of a set of data fields (also known as properties) with their current values Behavior of an object is defined by a set of methods

Classes Classes are constructs that define objects of the same type A Java class uses variables to define data fields and methods to define behaviors Additionally, a class provides a special type of methods, known as constructors, which are invoked to construct objects from that class

A Java Class

Example 1: Defining Classes & Creating Objects Objective: Demonstrate creating objects, accessing data, and invoking methods of the SimpleCircle class TestSimpleCircle Run

Example 2: Defining Classes & Creating Objects TV TestTV Run

Constructors What are they? They are a special kind of methods that are invoked to construct and initialize objects How do we define them? They must have the same name as the class itself. They do not have a return type - not even void Circle() { } Circle(double newRadius) { radius = newRadius; How do we invoke them? They are invoked using the new operator new Circle(); new Circle(5.0);

Default & No-Args Constructors A constructor without parameters is called a no-args constructor Example: Circle() { } A class may be defined without constructors In that case, a no-arg constructor with an empty body is implicitly defined in the class This constructor, called the default constructor, is provided automatically only if no constructors are explicitly defined in the class

Declaring Object Reference Variables To reference an object, assign the object to a reference variable To declare a reference variable, use the syntax: ClassName objectRefVar; Example: Circle myCircle;

Declaring & Constructing Objects in a Single Step ClassName objectRefVar = new ClassName(); Example: Circle myCircle = new Circle(); Assign object reference Create an object

Accessing Object’s Members Referencing the object’s data: objectRefVar.data Example: myCircle.radius Invoking the object’s method: objectRefVar.methodName(arguments) Example: myCircle.getArea()

Trace Code animation Declare myCircle Circle myCircle = new Circle(5.0); Circle yourCircle = new Circle(); yourCircle.radius = 100; myCircle no value

Trace Code, cont. animation Circle myCircle = new Circle(5.0); Circle yourCircle = new Circle(); yourCircle.radius = 100; myCircle no value Create a circle

Assign object reference to myCircle animation Trace Code, cont. Circle myCircle = new Circle(5.0); Circle yourCircle = new Circle(); yourCircle.radius = 100; myCircle reference value Assign object reference to myCircle

Trace Code, cont. animation Circle myCircle = new Circle(5.0); Circle yourCircle = new Circle(); yourCircle.radius = 100; myCircle reference value yourCircle no value Declare yourCircle

Create a new Circle object animation Trace Code, cont. Circle myCircle = new Circle(5.0); Circle yourCircle = new Circle(); yourCircle.radius = 100; myCircle reference value yourCircle no value Create a new Circle object

Assign object reference to yourCircle animation Trace Code, cont. Circle myCircle = new Circle(5.0); Circle yourCircle = new Circle(); yourCircle.radius = 100; myCircle reference value yourCircle reference value Assign object reference to yourCircle

Change radius in yourCircle animation Trace Code, cont. Circle myCircle = new Circle(5.0); Circle yourCircle = new Circle(); yourCircle.radius = 100; myCircle reference value yourCircle reference value Change radius in yourCircle

Caution Recall that, to invoke a method in the Math class, we use Math.methodName(arguments) (e.g., Math.pow(3, 2.5)) Can we invoke getArea() using SimpleCircle.getArea()? The answer is no. All the methods used before this chapter were static methods, which are defined using the static keyword. However, getArea() is not static. It must be invoked from an object using objectRefVar.methodName(arguments) Example: myCircle.getArea()

Reference Data Fields The data fields can be of primitive type or reference type Example: The following Student class contains a data field name of the String type, which is a reference type public class Student { String name; // name has default value null int age; // age has default value 0 boolean isMathMajor; // isMathMajor default value is false char gender; // gender has default value '\u0000' }

Default Value for a Data Field Type Default Value Numeric boolean false char '\u0000' Reference null If a data field of a reference type does not reference any object, the data field holds a special literal value, null Example: public class Test { public static void main(String[] args) { Student student = new Student(); System.out.println("name? " + student.name); System.out.println("age? " + student.age); System.out.println("isMathMajor? " + student.isMathMajor); System.out.println("gender? " + student.gender); } name? null age? 0 isMathMajor? false gender?

Example: Local Variables inside Methods Java assigns no default value to local variables inside methods public class Test { public static void main(String[] args) { int x; // x has no default value String y; // y has no default value System.out.println("x is " + x); System.out.println("y is " + y); } Compile-time error: variable not initialized

Differences between Variables of Primitive Data Types & Object Types

Copying Variables of Primitive Data Types & Object Types

Garbage Collection As shown in the previous figure, after the assignment statement c1 = c2, c1 points to the same object referenced by c2. The object previously referenced by c1 is no longer referenced. That object is now garbage Garbage is automatically collected by JVM TIP: If you know that an object is no longer needed, you can explicitly assign null to its reference variable. The JVM will automatically collect the space if the object is not referenced by any variable