A first Look at Classes.

Slides:



Advertisements
Similar presentations
Chapter 13 – Introduction to Classes
Advertisements

Lesson 14 Classes, Continued CS1 Lesson More Classes1.
CS0007: Introduction to Computer Programming Introduction to Classes and Objects.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 9 Classes.
Chapter 6: A First Look at classes. Object-Oriented Programming  Object-oriented programming is centered on creating objects rather than procedures.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 11 Classes and Object- Oriented Programming.
COMP 110 Introduction to Programming Mr. Joshua Stough October 8, 2007.
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
An Object-Oriented Approach to Programming Logic and Design
A First Look At Classes Chapter 6. Procedural Programming In procedural- programming all the program functionality is written in a few modules of code.
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.
Starting Out with Java: From Control Structures through Objects.
I NTRODUCTION TO PROGRAMMING Starting Out with Java: From Control Structures through Objects.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13: Introduction to Classes.
Chapter 13. Procedural programming vs OOP  Procedural programming focuses on accomplishing tasks (“verbs” are important).  Object-oriented programming.
CS0007: Introduction to Computer Programming Classes: Documentation, Method Overloading, Scope, Packages, and “Finding the Classes”
Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 12 Object-Oriented Programming Starting Out with Games & Graphics.
CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program.
Page 4.1 – Autumn 2010Steffen Vissing Andersen SDJ I1, Autumn 2010 Agenda – Session 4 – 7. September 2009 Java fundamentals, checkpoint from chapter 2.
Some odds and ends about Classes and Objects. 6-2 Object-Oriented Programming Object Data (Fields) Methods That Operate on the Data.
© 2010 Pearson Addison-Wesley. All rights reserved. 6-1 Chapter Topics Chapter 6 discusses the following main topics: –Classes and Objects –Instance Fields.
Lecture 19: Introduction to Classes Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Introduction to Classes, Objects, Methods and Attributes Lecture # 5.
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.
Separating Class Specification tMyn1 Separating Class Specification from Implementation Usually class declarations are stored in their own header files.
Starting Out With Java 5 Control Structures to Objects By Tony Gaddis Copyright © 2005, 2005 Pearson Addison-Wesley. All rights reserved. Chapter 6 Slide.
CS 106 Introduction to Computer Science I 03 / 22 / 2010 Instructor: Michael Eckmann.
Topics Instance variables, set and get methods Encapsulation
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 13: Introduction to Classes.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley The Unified Modeling Language
Object Based Programming Chapter 8. 2 Contrast ____________________ Languages –Action oriented –Concentrate on writing ________________ –Data supports.
CS16: UML’s Unified Modeling Language. UML Class Diagram A UML class diagram is a graphical tool that can aid in the design of a class. The diagram has.
Introduction to Constructors Lecture # 4. Copyright © 2011 Pearson Education, Inc. 3-2 Arguments Passed By Value In Java, all arguments to a method are.
CSIS 123A Lecture 1 Intro To Classes Glenn Stevenson CSIS 113A MSJC.
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
Introduction to Object-oriented Programming
Pointer to an Object Can define a pointer to an object:
Procedural and Object-Oriented Programming
Programming Logic and Design Seventh Edition
Topics Procedural and Object-Oriented Programming Classes
Chapter 3 Classes & Objects
Lecture 6 D&D Chapter 7 & 8 Intro to Classes and Objects Date.
classes and objects review
Introduction to Classes
Chapter 6: A First Look at Classes
CS 200 Creating Classes Jim Williams, PhD.
Object Based Programming
Introduction to Classes
Advanced Programming Behnam Hatami Fall 2017.
Programming paradigms
Today’s topics UML Diagramming review of terms
Lecture 5- Classes, Objects and Methods
Object Oriented Programming in java
CS 200 Creating Classes Jim Williams, PhD.
CS 200 Creating Classes Jim Williams, PhD.
Lecture 8 Object Oriented Programming (OOP)
Chapter 6: A First Look at classes
Presentation transcript:

A first Look at Classes

There are two types of programming used today: Procedural – This is what we have been doing Object Oriented – uses objects that are made up of methods and procedures

Encapsulation refers to the combining of data and code into a single object. Data hiding refers to an object’s ability to hide its data from code that is outside the object.

OOP also helps with object reusability OOP also helps with object reusability. An object is not a stand-alone program, but is used by programs that need its service.

Example Think of your alarm clock as an object. It has the following fields: The current second (a value in the range of 0-59) The current minute ( a value in the range of 0-59) The current hour (a value in the range of 1-12) The time the alarm is set for (a valid hour and minute Whether the alarm is on or off (“on” or “off”)

Fields are data values that define the state that the alarm clock is currently in. You (the user) cannot directly manipulate these fields because they are private. To change a value, you must use one of the object’s methods, which are: Set time Set alarm time Turn alarm on Turn alarm off

You can change the data by using these methods You can change the data by using these methods. Since you can activate these methods and you are outside of the alarm clock, these methods are public. The alarm clock also has private methods…ones that are internal that you do not have access to. Such methods are: Increment the current second Increment the current minute Increment the current hour Sound alarm

Classes can be more than a container for a program Classes can be more than a container for a program. A class can specify the fields and methods that a particular type of object may have. A class is not an object, but it can be a description of an object. Each object that is created from a class is called an instance of the class.

Creating a class When writing the code for a class, it is often helpful to draw a UML diagram (Unified Modeling Language). The diagram is a box that is divided into 3 sections (class name, fields, and methods).

Example Rectangle length width setLength() setWidth() getLength() getWidth() getArea()

Writing the code public class Rectangle { } The key word public is an access specifier. Public means that code that is written outside of the Rectangle.java file may access this code.

public class Rectangle { private double length; private double width; } Here, the access specifier private means that code outside of this program cannot access the variables length and width.

It is common practice in OOP to make all of a class’s fields private and to provide access to those fields through methods.

public class Rectangle { private double length; private double width; /*The setLength method stores a value in the length field */ public void setLength(double len) length = len; }