Classes and Objects Introduction Life Cycle Creation Example.

Slides:



Advertisements
Similar presentations
Introduction to classes Sangeetha Parthasarathy 06/11/2001.
Advertisements

Written by Paul Pu All Right Reservedwww.torontocollege.com Controlling Access to a Member(Java) public, private, protected (default)
Introduction to Java 2 Programming Lecture 3 Writing Java Applications, Java Development Tools.
Basic -2 Classes and Objects. Classes and Objects A class is a complex data TYPE An object is an instance of a class. Example: Class: Person Objects:
Based on Java Software Development, 5th Ed. By Lewis &Loftus
Constructor. 2 constructor The main use of constructors is to initialize objects. A constructor is a special member function, whose name is same as class.
A C LOSER L OOK AT C LASSES 1. A SSIGNING O BJECTS One object can be assigned to another provided that both objects are of the same type. It is not sufficient.
Object Oriented Programming in Java. Characteristics of Object Oriented Programming  Object Programming classes  Encapsulation  Polymorphism  Inheritance.
CS-1030 Dr. Mark L. Hornick 1 Constructors Copy Constructors.
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
Rounding Out Classes The objectives of this chapter are: To discuss issues surrounding passing parameters to methods What is "this"? To introduce class.
Road Map Introduction to object oriented programming. Classes
CSE 2501 Review Declaring a variable allocates space for the type of datum it is to store int x; // allocates space for an int int *px; // allocates space.
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.
Review 4 View classes as modules Encapsulate operations Functions are static methods 4 View classes as struct types Encapsulate data 4 View classes as.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
UML Class Diagram: class Rectangle
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Java Methods By J. W. Rider. Java Methods Modularity Declaring methods –Header, signature, prototype Static Void Local variables –this Return Reentrancy.
Object-Oriented Programming Concepts
Programming Languages and Paradigms Object-Oriented Programming.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Methods CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
BPJ444: Business Programming Using Java Classes and Objects Tim McKenna
Object Oriented Programming: Java Edition By: Samuel Robinson.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
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.
Constructors CMSC 202. Object Creation Objects are created by using the operator new in statements such as… The following expression invokes a special.
Centre for Computer Technology ICT214 Object Oriented Design and Programming Week 02 – Classes, Objects, Instances Richard Salomon and Umesh Patel Centre.
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.
Static Methods. 2 Objectives Look at how to build static (class) methods Study use of methods calling, parameters, returning values Contrast reference.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
 Classes in c++ Presentation Topic  A collection of objects with same properties and functions is known as class. A class is used to define the characteristics.
Programming in Java CSCI-2220 Object Oriented Programming.
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 4.
 Constructor  Finalize() method  this keyword  Method Overloading  Constructor Overloading  Object As an Argument  Returning Objects.
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
08 Encapsulation and Abstraction. 2 Contents Defining Abstraction Levels of Abstraction Class as Abstraction Defining a Java Class Instantiating a Class.
CS170 ygao JAVA, C4Slide 1. CS170 ygao JAVA, C4Slide 2.
 Objects versus Class  Three main concepts of OOP ◦ Encapsulation ◦ Inheritance ◦ Polymorphism  Method ◦ Parameterized ◦ Value-Returning.
Java Classes Chapter 1. 2 Chapter Contents Objects and Classes Using Methods in a Java Class References and Aliases Arguments and Parameters Defining.
Access Modifiers Control which classes use a feature Only class-level variables may be controlled by access modifiers Modifiers 1. public 2. protected.
CMSC 202 Advanced Section Classes and Objects: Object Creation and Constructors.
Classes, Interfaces and Packages
Introduction to C# By: Abir Ghattas Michel Barakat.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Chapter 4: More Object Concepts. Objectives Understand blocks and scope Overload a method Avoid ambiguity Create and call constructors with parameters.
YG - CS Concept of Encapsulation What is encapsulation? - data and functions/methods are packaged together in the class normally.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
Object Lifetimes and Dynamic Objects Lecture-7. Object Lifetimes and Dynamic Objects External (Global) Objects Persistent (in existence) throughout the.
JAVA ACCESS MODIFIERS. Access Modifiers Access modifiers control which classes may use a feature. A classes features are: - The class itself - Its member.
Unit 2. Constructors It initializes an object when it is created. It has same as its class and syntactically similar to a method. Constructor have no.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
Object Based Programming Chapter 8. 2 Contrast ____________________ Languages –Action oriented –Concentrate on writing ________________ –Data supports.
Chapter No. : 2 Classes and Objects.
Modern Programming Tools And Techniques-I
Topic: Classes and Objects
UML Class Diagram: class Rectangle
This pointer, Dynamic memory allocation, Constructors and Destructor
group work #hifiTeam
Object Based Programming
Overloading and Overriding
Java Inheritance.
Java Programming Language
Classes and Objects Object Creation
SPL – PS3 C++ Classes.
CMSC 202 Constructors Version 9/10.
Presentation transcript:

Classes and Objects Introduction Life Cycle Creation Example

Introduction Objects are created from classes An objects type is its class “is a” versus “has a” relationships Examples

Life Cycle Creation Use Destruction

Creation Object is instance of a class Declaration, Instantiation, Initialization Rectangle rect = new Rectangle(); Type is class. Object reference. new allocates memory (instantiates) Constructor initializes object Default constructor takes no arguments

Using Manipulate or inspect an object’s variables objectReference.variable Use an object’s methods to access variables objectReference.methodName(argumentList)

Destroy Garbage Collector that frees memory used by no longer needed objects. A mark-sweep garbage collector –Scans dynamic memory for object references –Unreferenced objects cleaned up –own thread finalize()

Details (1) Variables and methods are members constructors are not members Can overload constructors super() class constructor this.keyWord

Access to Classes private: No other class can instantiate this class public: Any class can instantiate this class protected: Only subclasses can instantiate package: only classes in the same package can instantiate

Member Variable Declarations static: Class member rather than instance member final: Value cannot be changed (constant) transient: non-Serializable volatile: For optimization

Details (2) return : can return object of subclass Overloaded methods distinguished by number and type of arguments Comma delimited set of arguments of any valid Java data type. this, super Local Variables

Pass by Value Arguments passed by value not reference cannot change value of primitive types Access values not actual variables Reference types passed by reference Reference a Kind of Pointer No Pointer to Anonymous Memory Value of reference types are the reference

... int r = -1, g = -1, b = -1; pen.getRGBColor(r, g, b); System.out.println("red = " + r + ", green = " + g + ", blue = " + b);... class Pen { int redValue, greenValue, blueValue; void getRGBColor(int red, int green, int blue) { // red, green, and blue have been created // and their values are }

class Pen { int redValue, greenValue, blueValue;... // this method does not work as intended void getRGBColor(int red, int green, int blue) { red = redValue; green = greenValue; blue = blueValue; }... int r = -1, g = -1, b = -1; pen.getRGBColor(r, g, b); System.out.println("red = " + r + ", green = " + g + ", blue = " + b);...

class RGBColor { public int red, green, blue; } class Pen { int redValue, greenValue, blueValue; void getRGBColor(RGBColor aColor) { aColor.red = redValue; aColor.green = greenValue; aColor.blue = blueValue; }

... RGBColor penColor = new RGBColor(); pen.getRGBColor(penColor); System.out.println("red = " + penColor.red + ", green = " + penColor.green + ", blue = " + penColor.blue);...

Access

Instance and Class Members Instance is default Each new object gets new copy of each of classes instance variables A single copy is created of a class member (static). Accessible from class itself. System.out.println(), Integer.parseInt(String s) Class methods can only operate on class variables Class members for constants and efficiency

class AnIntegerNamedX { int x; public int x() { return x; } public void setX(int newX) { x = newX; }... AnIntegerNamedX myX = new AnIntegerNamedX(); AnIntegerNamedX anotherX = new AnIntegerNamedX(); myX.setX(1); anotherX.x = 2; System.out.println("myX.x = " + myX.x()); System.out.println("anotherX.x = " + anotherX.x());...

myX.x = 1 anotherX.x = 2 class AnIntegerNamedX { static int x; public int x() { return x; } public void setX(int newX) { x = newX; } myX.x = 2 anotherX.x = 2

class AnIntegerNamedX { int x; static public int x() { return x; } static public void setX(int newX) { x = newX; } AnIntegerNamedX.java:4: Can't make a static reference to nonstatic variable x in class AnIntegerNamedX. return x; ^

class AnIntegerNamedX { static int x; static public int x() { return x; } static public void setX(int newX) { x = newX; } myX.x = 2 anotherX.x = 2