CS0007: Introduction to Computer Programming Introduction to Classes and Objects.

Slides:



Advertisements
Similar presentations
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Advertisements

Composition CMSC 202. Code Reuse Effective software development relies on reusing existing code. Code reuse must be more than just copying code and changing.
Chapter 6: A First Look at classes. Object-Oriented Programming  Object-oriented programming is centered on creating objects rather than procedures.
Chapter 10 THINKING IN OBJECTS 1 Object Oriented programming Instructor: Dr. Essam H. Houssein.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 11 Classes and Object- Oriented Programming.
Road Map Introduction to object oriented programming. Classes
Chapter 5 Part 1 COSI 11a Prepared by Ross Shaull.
CS 106 Introduction to Computer Science I 03 / 21 / 2008 Instructor: Michael Eckmann.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
COMP 110 Introduction to Programming Mr. Joshua Stough October 8, 2007.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
C++ Classes in Depth. Topics Designing Your Own Classes Attributes and Behaviors Writing Classes in C++ Creating and Using Objects.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
1 Fall 2007ACS-1903 Chapter 6: Classes Classes and Objects Instance Fields and Methods Constructors Overloading of Methods and Constructors Scope of Instance.
Chapter 6: A First Look at Classes
UML Basics & Access Modifier
Comp 248 Introduction to Programming Chapter 4 - Defining Classes Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Starting Out With Java 5 (Control Structures to Objects) Chapter 6 By Tony Gaddis Copyright © 2005 Pearson Addison-Wesley. All rights reserved.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java: From Control Structures through Objects Third Edition.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java: Early Objects Third Edition by Tony Gaddis Chapter.
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.
Starting Out with Java: From Control Structures through Objects.
CSCI-383 Object-Oriented Programming & Design Lecture 13.
Chapter 7 Objects and Classes 1 Fall 2012 CS2302: Programming Principles.
Chapter 13. Procedural programming vs OOP  Procedural programming focuses on accomplishing tasks (“verbs” are important).  Object-oriented programming.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
CSC1401 Classes - 2. Learning Goals Computing concepts Adding a method To show the pictures in the slide show Creating accessors and modifiers That protect.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Chapter 4 Introduction to Classes, Objects, Methods and strings
CS0007: Introduction to Computer Programming Classes: Documentation, Method Overloading, Scope, Packages, and “Finding the Classes”
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Chapter 3 (B) 3.5 – 3.7.  Variables declared in a function definition’s body are known as local variables and can be used only from the line of their.
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 12 Object-Oriented Programming Starting Out with Games & Graphics.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program.
© 2010 Pearson Addison-Wesley. All rights reserved. 6-1 Chapter Topics Chapter 6 discusses the following main topics: –Classes and Objects –Instance Fields.
CreatingClasses-SlideShow-part31 Creating Classes part 3 Barb Ericson Georgia Institute of Technology Dec 2009.
Introduction to Object-Oriented Programming Lesson 2.
Lecture 19: Introduction to Classes Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
Object-Oriented Programming (OOP) What we did was: (Procedural Programming) a logical procedure that takes input data, processes it, and produces output.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 3 A First Look at Classes and Objects.
Topic 8Classes, Objects and Methods 1 Topic 8 l Class and Method Definitions l Information Hiding and Encapsulation l Objects and Reference Classes, Objects,
Written by: Dr. JJ Shepherd
1 Static Variable and Method Lecture 9 by Dr. Norazah Yusof.
Starting Out With Java 5 Control Structures to Objects By Tony Gaddis Copyright © 2005, 2005 Pearson Addison-Wesley. All rights reserved. Chapter 6 Slide.
Topics Instance variables, set and get methods Encapsulation
Class Fundamentals BCIS 3680 Enterprise Programming.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Mr H Kandjimi 2016/01/03Mr Kandjimi1 Week 3 –Modularity in C++
Class Definitions and Writing Methods Chapter 3 3/31/16 & 4/4/16.
Computer Programming II Lecture 5. Introduction to Object Oriented Programming (OOP) - There are two common programming methods : procedural programming.
Chapter 6 A First Look at Classes. 2 Contents 1. Classes and objects 2. Instance Fields and Methods 3. Constructors 4. Overloading Methods and Constructors.
Copyright © 2011 Pearson Education, Inc. Starting Out with Java: Early Objects Fourth Edition by Tony Gaddis Chapter 3: A First Look at Classes and Objects.
Chapter 6 A First Look at Classes. 2 Contents 1. Classes and objects 2. Instance Fields and Methods 3. Constructors 4. Overloading Methods and Constructors.
A Second Look at Classes and Objects - 1 Static Class Members
Topics Procedural and Object-Oriented Programming Classes
Chapter 3 Classes & Objects
Java Primer 1: Types, Classes and Operators
A first Look at Classes.
Chapter 3: Using Methods, Classes, and Objects
Methods The real power of an object-oriented programming language takes place when you start to manipulate objects. A method defines an action that allows.
Today’s topics UML Diagramming review of terms
Defining Classes and Methods
Lecture 5- Classes, Objects and Methods
Object Oriented Programming in java
Chapter 6: A First Look at classes
Presentation transcript:

CS0007: Introduction to Computer Programming Introduction to Classes and Objects

Review When a argument is passed by value, what does the corresponding parameter hold? The value of the argument. When a argument is passed by reference, what does the corresponding parameter hold? The same reference as the argument. If a value parameter is changed does the corresponding argument change? No, the parameter only holds the value. If a reference parameter is changed does the corresponding argument change? Yes, because the parameter and argument both reference the same object, unless the object they are pointing to is immutable, like String. How can a method take a variable number of arguments? Make it accept an array, which can be any length.

Procedural vs. Object Oriented Programming Recall what we learned about the two methods of programming: Procedural Made up of procedures. Procedure – set of programming statements that perform a specific task. Data and procedures are logically separate. Data is passed from one procedure to another. The focus is on creating procedures to act on the data. We have been doing this. Our methods have been used as procedures! Procedural Programming is often useful, and it is still used today. However, in some situations it does not work well. oExample: If the format of the data changes, then all the procedures that use the data must also change. The problem lies in the fact that the data and the code that acts on the data are logically separate. oSolution: Object-Oriented Programming.

Procedural vs. Object Oriented Programming Object-Oriented Programming Instead of focusing on creating procedures, the focus is on creating objects. Object – a software entity that contains data and procedures. oData is known as attributes (or fields) oProcedures are known as methods. OOP handles code/data separation through two design principles in which it was built upon: Encapsulation – The combining of data and code into a single object. Data Hiding – The ability for the object to hide its data from code outside of the object. oCode outside of the object uses the object’s data by using the object’s methods. oThis has two benefits: Outside sources cannot corrupt the object’s data. Code outside of the object does not need to know anything about the internal structure of the object’s data. If a programming changes how the object’s data is kept internally, she also changes the methods, so that others using the object need not change their code.

Alarm Clock Example Exercise: Describe the attributes and methods for an alarm clock object.

Alarm Clock Example Alarm clock Attributes The current second (0-59) The current minute (0-59) The current hour (1-24) The minute the alarm is set for (0-59) The hour the alarm is set for (1-24) Whether the alarm is on or off These attributes are simply data kept about the object that reflect the state of the object. You cannot directly change these values, you must use the object’s methods in order to change them. It is said these attributes are private, meaning they cannot be changed directly by outside sources.

Alarm Clock Example Alarm clock Methods Set current time Set alarm time Turn alarm on Turn alarm off These methods can be used by you to change the values of the alarm clock’s attributes. It is said these methods are public. However, there are methods that are part of the internal workings of the clock: Methods Increment the current second Increment the current minute Increment the current hour Sound Alarm You cannot use these methods directly, but they are part of how the clock modifies its own attributes. It is said these methods are private. If the internal workings of the clock are changed, these methods are the ones to be changed.

Classes and Objects Before we can create objects, a programmer must be design the object. The programmer decides what attributes and methods an object will need Then, the programmer writes a class. A class specifies the attributes and methods and object can have. You can think of it as a blueprint for an object. When a program is running, it can use a class to create, in memory, as many objects of a specific type as needed. An object created from a class is said to be an instance of the class. An object will have all the methods and attributes defined by the class from which it was created. One of the best metaphors for this is the cookie-cutter. You can think of the class being the cookie-cutter. Each object created by the class is a cookie cut from the cookie-cutter (class).

Class Example Rectangle Class: Attributes: length width Methods: setLength setWidth getLength getWidth getPerimeter getArea

Object Notes The class header has three parts to it public class Rectangle An access specifier ( public in this case) An access specifier defines where an entity can be used. We will be only using public classes in this course (probably…). The keyword class (says that the following definition is a class) The name of the class (follows normal identifier naming rules, usually starts with a capital letter) Attributes and methods inside of a class are often called members of the class. Access specifiers on class members: public – Can be accessed by code inside or outside of the class private – Can only be accessed by methods in the class in which it is defined.

Object Notes Attributes are defined at the top of the class definition, outside of any method. They should have an access specifier before the data type. If not the default is protected We may talk about protected later Method members are defined like any other method we’ve defined before except: The access specifier should be public if it meant to be public. The word static is not used if the method is intended to be used on objects created by the class. Non-static methods inside of classes are called instance methods.

Object Notes setLength and setWidth are known as mutator methods, because they change the attributes of the class. More specifically, these are often called “setters”. getLength and getWidth are known as accessor methods, because they access private attributes. More specifically, these are often called “getters”. Remember the difference between object and primitive variables! Object variables hold references to objects, primitive variables only hold the value that is stored in them. Object variables point to objects which have methods and possibly multiple values, primitive variables only hold one value and do no have methods,

Instance Fields and Methods Lets take a look at another example: MultipleRectangleDemo.java Notice that each instance of the Rectangle class has its own length and width variables. When a single instance’s length or width attribute is changed, its copy is the only one that is changed. This is because length and width are known as instance variables (fields). Every instance of a class has its own set of instance fields. Notice also that all the methods act upon instances of the class and not the class itself. For this reason, these methods are called instance methods.

Constructor Ever notice that when we create an object it looks like we call a method? Rectangle myRectangle = new Rectangle(); Actually, this is calling a method. When you create an object a special method called a constructor is called. A constructor always has the same name of the class. Constructors typically perform initialization or setup operations, such as initializing instance fields. You usually do this by passing the constructor arguments to initialize the instance fields. It is good practice to create a constructor that initializes the values of the instance variables. Example: Rectangle2.java (Second One) Notice that the constructor does not have a return type! A constructor only has an access specifier, the name of the constructor (also the name of the class), then the parameter list. The exception to this is String, but not really: String name = "Eric Heim"; is just a shortcut Java provides for calling String name = new String("Eric Heim");

Default Constructor But, we didn’t define a constructor before and we defined and created objects…what is being called? Answer: The default constructor. When you define a class in Java, if you do not define a constructor, Java implicitly creates one for you. The default constructor will set all numeric instance variables to 0 or 0.0, all boolean instance variables to true, and all reference variables to null. The ONLY time Java provides a default constructor is when we do not define one. So if you define a constructor, you must use that one. It is good practice to define your own constructor so that you have control over what happens when someone creates an instance of your class. But, what if I don’t want the others to give values to my instance variables when they create my object? oAnswer: No-Arg Constructor. Example: NoArgConstructor.java