Objects and Classes. F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive data type and object type.

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, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 8 Objects and Classes.
Road Map Introduction to object oriented programming. Classes
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 7 Objects and Classes.
Java Data Types  Everything is an Object  Except Primitive Data Types  For efficiency  Platform independent  Portable  “slow”  Objects are often.
Introduction to Java Programming, 4E Y. Daniel Liang.
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,
1 Inheritance and Polymorphism Chapter 9. 2 Polymorphism, Dynamic Binding and Generic Programming public class Test { public static void main(String[]
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
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.
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
Chapter 5 Programming with Objects and Classes OO Programming Concepts OO Programming Concepts Declaring and Creating Objects Declaring and Creating Objects.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi.
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.
Chapter 7: Class Inheritance F Superclasses and Subclasses F Keywords: super and this F Overriding methods F The Object Class F Modifiers: protected, final.
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.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
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.
1 Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
SourceAnatomy1 Java Source Anatomy Barb Ericson Georgia Institute of Technology July 2008.
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.
Lecture 9: Object and Classes Michael Hsu CSULA. 2 OO Programming Concepts Object-oriented programming (OOP) involves programming using objects. An object.
Lecture 3: Introduction to Object and Classes Michael Hsu CSULA.
Lecture 3: Introduction to Object and Classes Michael Hsu CSULA.
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
Topic: Classes and Objects
Lecture 3: Introduction to Object and Classes
G. Pullaiah College of Engineering and Technology
Chapter 8 Classes and Objects
HIGHLEVEL REVIEW Chapter 9 Objects and Classes
Corresponds with Chapter 7
The Building Blocks Classes: Java class library, over 1,800 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 6 Objects and Classes
Chapter 6 Objects and Classes
Chapter 8 Objects and Classes
Chapter 9 Objects and Classes
Chapter 8 Class Inheritance and Interfaces
Chapter 9 Objects and Classes Part 01
Chapter 7 Objects and Classes
OO Programming Concepts
Chapter 6 Objects and Classes
Chapter 8 Objects and Classes Part 2
Chapter 7 Objects and Classes
Presentation transcript:

Objects and Classes

F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive data type and object type –Automatic garbage collection F Constructors  Modifiers ( public, private and static ) F Instance and Class Variables and Methods F Scope of Variables F Use the this Keyword  Case Studies ( Mortgage class and Count class)

OO Programming Concepts

Class and Objects

Class Declaration class Circle { double radius = 1.0; double findArea(){ return radius * radius * ; }

Declaring Object Reference Variables ClassName objectReference; Example: Circle myCircle;

Creating Objects objectReference = new ClassName(); Example: myCircle = new Circle(); The object reference is assigned to the object reference variable.

Declaring/Creating Objects in a Single Step ClassName objectReference = new ClassName(); Example: Circle myCircle = new Circle();

Differences between variables of primitive Data types and object types

Copying Variables of Primitive Data Types and 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 useful. This object is known as garbage. Garbage is automatically collected by JVM.

Garbage Collection, cont TIP: If you know that an object is no longer needed, you can explicitly assign null to a reference variable for the object. The Java VM will automatically collect the space if the object is not referenced by any variable.

Accessing Objects F Referencing the object’s data: objectReference.data myCircle.radius F Invoking the object’s method: objectReference.method myCircle.findArea()

Constructors Circle(double r) { radius = r; } Circle() { radius = 1.0; } myCircle = new Circle(5.0); Constructors are a special kind of methods that are invoked to construct objects.

Constructors, cont. A constructor with no parameters is referred to as a default constructor.  Constructors must have the same name as the class itself.  Constructors do not have a return type— not even void.  Constructors are invoked using the new operator when an object is created. Constructors play the role of initializing objects.

Visibility Modifiers and Accessor Methods By default, the class, variable, or data can be accessed by any class in the same package.  public The class, data, or method is visible to any class in any package.  private The data or methods can be accessed only by the declaring class. The get and set methods are used to read and modify private properties.

Passing Objects to Methods, cont.

Instance Variables, and Methods Instance variables belong to a specific instance. Instance methods are invoked by an instance of the class.

Class Variables, Constants, and Methods Class variables are shared by all the instances of the class. Class methods are not tied to a specific object. Class constants are final variables shared by all the instances of the class.

Class Variables, Constants, and Methods, cont. To declare class variables, constants, and methods, use the static modifier.

Class Variables, Constants, and Methods, cont.

Scope of Variables F The scope of instance and class variables is the entire class. They can be declared anywhere inside a class. F The scope of a local variable starts from its declaration and continues to the end of the block that contains the variable. A local variable must be declared before it can be used.

The Keyword this F Use this to refer to the current object. F Use this to invoke other constructors of the object.

Array of Objects Circle[] circleArray = new Circle[10]; An array of objects is actually an array of reference variables. So invoking circleArray[1].findArea() involves two levels of referencing as shown in the next figure. circleArray references to the entire array. circleArray[1] references to a Circle object.

Array of Objects, cont. Circle[] circleArray = new Circle[10];

Class Abstraction Class abstraction means to separate class implementation from the use of the class. The creator of the class provides a description of the class and let the user know how the class can be used. The user of the class does not need to know how the class is implemented. The detail of implementation is encapsulated and hidden from the user.

Example 6.8 The Mortgage Class TestMortgageClass Run Mortgage

Example 6.9 The Count Class Run TestVoteCandidate

The Object Class  The Object class is the root of all Java classes.  The equals() method compares the contents of two objects.  The toString() method returns a string representation of the object.  The clone() method copy objects

The Object Class, cont. The equals() method compares the contents of two objects. The default implementation of the equals method in the Object class is as follows: public boolean equals(Object obj) { return (this == obj); }

NOTE The == comparison operator is used for comparing two primitive data type values or for determining whether two objects have the same references. The equals method is intended to test whether two objects have the same contents, provided that the method is modified in the defining class of the objects. The == operator is stronger than the equals method, in that the == operator checks whether the two reference variables refer to the same object.

The Object Class, cont.  The toString() method returns a string representation of the object. The default implementation returns a string consisting of a class name of which the object is an instance, the at sign and a number representing this object. The clone() method copy objects

The Object Class, cont. To create a new object with separate memory space, you need to use the clone() method, as follows: newObject = someObject.clone(); NOTE: Not all objects can be cloned. For an object to be cloneable, its class must implement the java.lang.Cloneable interface. Interfaces are introduced in the section "Interfaces," in this chapter.

The final Modifier  The final class cannot be extended: final class Math {... }  The final variable is a constant: final static double PI = ;  The final method cannot be modified by its subclasses.

Java API and Core Java classes  java.lang Contains core Java classes, such as numeric classes, strings, and objects. This package is implicitly imported to every Java program.  java.awt Contains classes for graphics.  java.applet Contains classes for supporting applets.

 java.io Contains classes for input and output streams and files.  java.util Contains many utilities, such as date.  java.net Contains classes for supporting network communications. Java API and Core Java classes, cont.

 java.awt.image Contains classes for managing bitmap images.  java.awt.peer Platform-specific GUI implementation. F Others: java.sql java.rmi Java API and Core Java classes, cont.