CS 139 Objects Based on a lecture by Dr. Farzana Rahman Assistant Professor Department of Computer Science.

Slides:



Advertisements
Similar presentations
1 Classes and Objects in Java Basics of Classes in Java.
Advertisements

Chapter 6 Objects and Classes F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive data type and.
L3:CSC © Dr. Basheer M. Nasef Lecture #3 By Dr. Basheer M. Nasef.
Tutorial 6 February 4th/5th, 2015
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.
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)
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,
Lecture 3. 2 Introduction Java is a true OO language -the underlying structure of all Java programs is classes. Everything must be encapsulated in a class.
Syllabus (1) WeekChapters 1Introduction to the course, basic java language programming concepts: Primitive Data Types and Operations 1, 2 2Methods, Control.
BPJ444: Business Programming Using Java Classes and Objects Tim McKenna
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 9 Objects and Classes.
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.
Lecture # 5 Methods and Classes. What is a Method 2 A method is a set of code which is referred to by name and can be called (invoked) at any point in.
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 Classes and Objects in C++ 2 Introduction Java is a true OO language and therefore the underlying structure of all Java programs is classes. Anything.
1 Classes and Objects in Java Basics of Classes in Java.
1 Software Construction Lab 3 Classes and Objects in Java Basics of Classes in Java.
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
Classes: user-defined types. Organizing method with a class A class is used to organize methods * Methods that compute Mathematical functions * The Scanner.
COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi.
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
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.
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.
Object-Oriented Programming (OOP) What we did was: (Procedural Programming) a logical procedure that takes input data, processes it, and produces output.
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.
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.
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.
1 COS240 O-O Languages AUBG, COS dept Lecture 12 Title: Java Classes and Objects Reference: COS240 Syllabus.
Lecture 9: Object and Classes Michael Hsu CSULA. 2 OO Programming Concepts Object-oriented programming (OOP) involves programming using objects. An object.
Dr. Majed Abdouli © Objects and Classes 1 Dr. Majed Abdouli © 2015, adapted from Liang, Introduction to Java Programming, Eighth Edition, (c) 2011.
INTRODUCTION Java is a true OO language the underlying structure of all Java programs is classes. Everything must be encapsulated in a class that defines.
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
Topic: Classes and Objects
Lecture 3: Introduction to Object and Classes
Classes and Objects in Java
Classes and Objects in Java
Classes and Objects in Java
Road Map Introduction to object oriented programming. Classes
HIGHLEVEL REVIEW 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 8 Objects and Classes
Chapter 9 Objects and Classes Part 01
OO Programming Concepts
Chapter 6 Objects and Classes
Chapter 7 Objects and Classes
Presentation transcript:

CS 139 Objects Based on a lecture by Dr. Farzana Rahman Assistant Professor Department of Computer Science

Classes A class is a collection of fields (data) and methods (procedure or function) that operate on that data. The basic syntax for a class definition: Bare bone class – no fields, no methods public class Circle { // my circle class } public class ClassName { [fields/var declaration] [methods declaration] }

Adding Fields: Class Circle with fields Add fields The fields (data) are also called the instance variables/class variables public class Circle { public double x, y; // centre coordinate public double r; // radius of the circle }

Instance Variables, and Methods Instance variables belong to a specific instance of a class (i.e. any object) Instance methods are invoked by an instance of the class (i.e. any object)

Adding Methods to Class Circle public class Circle { public double x, y; // centre of the circle public double r; // radius of circle //Methods to return circumference and area public double circumference() { return 2*3.14*r; } public double area() { return 3.14 * r * r; }

Instantiating Objects Example: Circle myCircle = new Circle(); ClassName var_name; var_name = new ClassName(); Example: Circle myCircle; myCircle = new Circle(); or

Visibility Modifiers 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.

Class of Circle Circle aCircle; Circle bCircle; aCircle, bCircle simply refers to a null, not an actual object. aCircle Points to nothing (Null Reference) bCircle Points to nothing (Null Reference) null

Creating objects of a class Objects are created dynamically using the new keyword. bCircle = new Circle()aCircle = new Circle() Now aCircle and bCircle refer to Circle objects

Assigning one object to the other P aCircle Q bCircle Before Assignment P aCircle Q bCircle Before Assignment aCircle = new Circle(); bCircle = new Circle() ; bCircle = aCircle;

Garbage Collection As shown in the previous figure, after the assignment statement bCircle = aCircle, aCircle points to the same object referenced by bCircle. The object previously referenced by aCircle is no longer useful. This object is known as garbage. Java automatically collects garbage periodically and releases the memory used to be used in the future.

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.

public class Word { private String inword; public Word(String str) { inword = str; } public String makePossessive() { return inword+“’s” ; } … } Different objects will have different instance variables Different objects will execute their own method

Word name = new Word(“Farzana”); Word fname = new Word(“Bob”); Word aname = new Word(“Cat”); Farzana makePossessive() … Bob makePossessive() … Cat makePossessive() … Instance variable tied to name object Instance variable tied to fname object Instance variable tied to aname object

name.makePossessive(); fname.makePossessive(); aname.makePossessive(); Farzana makePossessive() … Bob makePossessive() … Cat makePossessive() … Farzana’s Bob’s Cat’s Method works on object’s attribute