Object-Oriented Programming. Agenda Classes & Objects Attributes Methods Objects in memory Visibility Properties (get & set) Constructors.

Slides:



Advertisements
Similar presentations
Chapter 22 Implementing lists: linked implementations.
Advertisements

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
Introduction to Java Objects CSIS 3701: Advanced Object Oriented Programming.
C++ Classes & Data Abstraction
Arrays Session 05 Mata kuliah: M0874 – Programming II Tahun: 2010.
Memory Management & Method Calls in Java Program Execution © Allan C. Milne v
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.
Chapter 3 Classes and Methods. 2 Knowledge Goals Appreciate the difference between a class in the abstract sense and a class as a Java construct Know.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,
Constructors A constructor is a method that creates an object in Java. It does not return anything and it is not void. It’s only purpose is to create an.
Object Oriented Software Development
Programming Languages and Paradigms Object-Oriented Programming.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
BPJ444: Business Programming Using Java Classes and Objects Tim McKenna
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.
CSCI 3328 Object Oriented Programming in C# Chapter 3: Introduction to Classes and Objects UTPA – Fall
Lecture Set 11 Creating and Using Classes Part B – Class Features – Constructors, Methods, Fields, Properties, Shared Data.
Tuc Goodwin  Object and Component-Oriented Programming  Classes in C#  Scope and Accessibility  Methods and Properties  Nested.
Objects, Classes and Syntax Dr. Andrew Wallace PhD BEng(hons) EurIng
CSE 1301 Lecture 5 Writing Classes Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
Chapter 10 Defining Classes. The Internal Structure of Classes and Objects Object – collection of data and operations, in which the data can be accessed.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
 Constructor  Finalize() method  this keyword  Method Overloading  Constructor Overloading  Object As an Argument  Returning Objects.
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
CSE 1301 Lecture 6 Writing Classes Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
Working With Objects Tonga Institute of Higher Education.
Static. 2 Objectives Introduce static keyword –examine syntax –describe common uses.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Objects and Classes.
CSCI 3328 Object Oriented Programming in C# Chapter 9: Classes and Objects: A Deeper Look – Exercises 1 Xiang Lian The University of Texas Rio Grande Valley.
CS 139 Objects Based on a lecture by Dr. Farzana Rahman Assistant Professor Department of Computer Science.
Object Oriented Programming Session # 03.  Abstraction: Process of forming of general and relevant information from a complex scenarios.  Encapsulation:
Java Programming, Second Edition Chapter Three Using Methods, Classes, and Objects.
Topics Instance variables, set and get methods Encapsulation
Class Fundamentals BCIS 3680 Enterprise Programming.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Creating and Using Objects, Exceptions, Strings
Classes (Part 1) Lecture 3
Intro To Classes Review
Chapter 3: Using Methods, Classes, and Objects
Road Map Introduction to object oriented programming. Classes
Creating Your OwnClasses
Lecture 14 Writing Classes part 2 Richard Gesick.
Lecture 13 Writing Classes Richard Gesick.
Classes & Objects: Examples
CSCI 3328 Object Oriented Programming in C# Chapter 9: Classes and Objects: A Deeper Look – Exercises UTPA – Fall 2012 This set of slides is revised from.
© A+ Computer Science - OOP © A+ Computer Science -
The University of Texas – Pan American
Assessment – Java Basics: Part 1
Object-Oriented Programming and class Design
CIS 199 Final Review.
Object-Oriented Programming
Object-Oriented Programming and class Design
Object-Oriented Programming and class Design
Object-Oriented Design AND CLASS PROPERTIES
Object-Oriented Programming and class Design
Object-Oriented Programming and class Design
Object-Oriented Design AND CLASS PROPERTIES
Creating and Using Classes
Chapter 7 Objects and Classes
CSG2H3 Object Oriented Programming
Object-Oriented Programming and class Design
Object-Oriented Design AND CLASS PROPERTIES
Presentation transcript:

Object-Oriented Programming

Agenda Classes & Objects Attributes Methods Objects in memory Visibility Properties (get & set) Constructors

Classes Encapsulate data and functions Enable more complex programming –Large-scale programming Requires “Object think” –Security/reliability –Design by contract

Defining a Class class NAME { ATTRIBUTES (data) METHODS (functions) }

Class Example class BMW_Z4 { int ModelYear; string LicensePlate; bool TopUp; void Drive() { Console.WriteLine(“Roadin’ and Rockin’“); } void OpenTop() { TopUp = false; }

Class Example class BMW_Z4 { int ModelYear; string LicensePlate; bool TopUp; void Drive() { Console.WriteLine(“Roadin’ and Rockin’“); } void OpenTop() { TopUp = true; } Methods (functions) Attributes (data)

Instantiating Classes Defining the class is akin to defining a type An object is an instance of a class Class:Object::Type:Variable Use new() to instantiate the class

Instantiating Class & Using Object BMW_Z4 myCar; myCar = new BMW_Z4(); myCar.LicensePlate = "BMR4ME"; myCar.ModelYear = 2004; myCar.Drive(); myCar.OpenTop(); myCar.Drive();

Memory Trace Dog d1, d2; d1 = new Dog (5, “bob”); d2 = new Dog (5, “bob”); d1 = d2; null

Memory Trace (declare the variables; they are dead) Dog d1, d2; d1 = new Dog (5, “bob”); d2 = new Dog (5, “bob”); d1 = d2; null d1 d2

Memory Trace (bring d1 to life) Dog d1, d2; d1 = new Dog (5, “bob”); d2 = new Dog (5, “bob”); d1 = d2; // Remember, new opens // up space and calls the // class constructor null d1 d2

Memory Trace (bring d1 to life) Dog d1, d2; d1 = new Dog (5, “bob”); d2 = new Dog (5, “bob”); d1 = d2; // Remember, new opens // up space and calls the // class constructor null d1 d2

Memory Trace (bring d1 to life) Dog d1, d2; d1 = new Dog (5, “bob”); d2 = new Dog (5, “bob”); d1 = d2; // Remember, new opens // up space and calls the // class constructor null d1 d2 rabid=false weight=5 name=“bob”

Memory Trace (bring d2 to life) Dog d1, d2; d1 = new Dog (5, “bob”); d2 = new Dog (5, “bob”); d1 = d2; // Remember, new opens // up space and calls the // class constructor null d1 d2 rabid=false weight=5 name=“bob”

Memory Trace (bring d2 to life) Dog d1, d2; d1 = new Dog (5, “bob”); d2 = new Dog (5, “bob”); d1 = d2; // Remember, new opens // up space and calls the // class constructor null d1 d2 rabid=false weight=5 name=“bob” rabid=false weight=5 name=“bob”

Memory Trace (comparison) d1 and d2 do NOT occupy the same space in memory, so they are NOT == Example: d1 == d2 evaluates to false null d1 d2 rabid=false weight=5 name=“bob” rabid=false weight=5 name=“bob”

Memory Trace (have d1 point to d2) Dog d1, d2; d1 = new Dog (5, “bob”); d2 = new Dog (5, “bob”); d1 = d2; null d1 d2 rabid=false weight=5 name=“bob” rabid=false weight=5 name=“bob”

Memory Trace (have d1 point to d2) Dog d1, d2; d1 = new Dog (5, “bob”); d2 = new Dog (5, “bob”); d1 = d2; null d1 d2 rabid=false weight=5 name=“bob” rabid=false weight=5 name=“bob”

Memory Trace (have d1 point to d2) Now, d1 and d2 are == But no one is pointing to this Because no one is pointing to it, we can no longer reference it. We call this piece of memory garbage. The garbage collector will deallocate it. null d1 d2 rabid=false weight=5 name=“bob” rabid=false weight=5 name=“bob”

Visibility OO motivation: protection/security We need a way of selectively “publishing” parts of a class and “hiding” other parts of the class Public & private

class BMW_Z4 { private int ModelYear; public string LicensePlate; private bool TopUp; public void Drive() { Console.WriteLine("Roadin’ and Rockin’"); } public void OpenTop() { TopUp = false; } Visibility Example Method are typically public Note the visibility change (most attributes will be private)

Object Method & Attribute Visibility BMW_Z4 myCar; myCar = new BMW_Z4(); myCar.LicensePlate = "BMR4ME"; myCar.ModelYear = 2004; myCar.Drive(); myCar.OpenTop(); myCar.Drive(); Illegal b/c private

Properties Combines field/attribute with method Standard: –Make attributes private –Lower-case first letter of attribute –Make properties public –Upper-case first letter of properties –Define “get” and “set” for each property (selectively)

class BMW_Z4 { private int modelYear; private string licensePlate; private bool topUp; public int ModelYear { get { return modelYear; } set { if (value >= 2003) modelYear = value; } } public string LicensePlate { get { return licensePlate; } set { if (value.Length() == 6) licensePlate = value; } }... } Properties Example

Constructors So far, we’ve seen attributes and methods Constructor is a unique method –Named same as the class name –Automatically called when class is instantiated –Useful for setting attributes & initializing the instance –No return type (not even void) –Must be public Default constructor is used unless you specify a constructor For each attribute in the class, assign something to it in the constructor (initialize)

class BMW_Z4 { private int modelYear; private string licensePlate; private bool topUp; public BMW_Z4 () { ModelYear = 2004; TopUp = false; LicensePlate = "DEALER"; }... } Constructor Example

FIN