IT108 Objects and Classes Part I George Mason University Revised 4/3/2012.

Slides:



Advertisements
Similar presentations
Chapter 6 Objects and Classes F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive data type and.
Advertisements

L3:CSC © Dr. Basheer M. Nasef Lecture #3 By Dr. Basheer M. Nasef.
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 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 Chapter 8 Objects and Classes.
Road Map Introduction to object oriented programming. Classes
1 Chapter 2 Objects and Classes. 2 Objectives F To understand objects and classes and use classes to model objects. F To learn how to declare a class.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 7 Objects and Classes.
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,
Chapter 8 Objects & Classes. Definition of Object-Oriented Programming (OOP) Object-Oriented Programming (OOP) uses the analogy of real objects as a template.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Syllabus (1) WeekChapters 1Introduction to the course, basic java language programming concepts: Primitive Data Types and Operations 1, 2 2Methods, Control.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 9 Objects and Classes.
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.
Objects and Classes Chapter 6 CSCI CSCI 1302 – Objects and Classes2 Outline Introduction Defining Classes for Objects Constructing Objects Accessing.
1 Introducing Methods A method is a collection of statements that are grouped together to perform an operation.
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
COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi.
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 9 Objects and Classes.
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.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 7 Objects and Classes.
1 Chapter 6 Programming with Objects and Classes F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive.
Topic 8Classes, Objects and Methods 1 Topic 8 l Class and Method Definitions l Information Hiding and Encapsulation l Objects and Reference Classes, Objects,
Introduction To Objects Oriented Programming Instructor: Mohammed Faisal.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 8 Objects and Classes.
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.
Chapter 7 Objects and Classes. OO Programming Concepts Object-oriented programming (OOP) involves programming using objects. An object represents an entity.
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.
Object-Oriented Programming in Java. 2 CS2336: Object-Oriented Programming in Java Buzzwords interfacejavadoc encapsulation coupling cohesion polymorphic.
1 COS240 O-O Languages AUBG, COS dept Lecture 12 Title: Java Classes and Objects Reference: COS240 Syllabus.
1 Chapter 9 Objects and Classes. 2 OO Programming in Java Other than primitive data types (byte, short, int, long, float, double, char, boolean), everything.
Lecture 9: Object and Classes Michael Hsu CSULA. 2 OO Programming Concepts Object-oriented programming (OOP) involves programming using objects. An object.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 9 Introduction of Object Oriented Programming.
Dr. Majed Abdouli © Objects and Classes 1 Dr. Majed Abdouli © 2015, adapted from Liang, Introduction to Java Programming, Eighth Edition, (c) 2011.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
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
Lecture 3: Introduction to Object and Classes
Chapter 3: Using Methods, Classes, and Objects
HIGHLEVEL REVIEW Chapter 9 Objects and Classes
Corresponds with Chapter 7
Chapter 8 Objects and Classes Part 1
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
Chapter 7 Objects and Classes
OO Programming Concepts
Chapter 6 Objects and Classes
Chapter 7 Objects and Classes
Presentation transcript:

IT108 Objects and Classes Part I George Mason University Revised 4/3/2012

The Qualities/State and Behavior of Objects Qualities/State- adjectives that describe the object Examples: color, radius, position Instance variables (also known as properties or fields) that contain values Behavior- verbs that denote what tasks an object can do (or can be done to it) Examples: calculate, close, show Usually initiated through a set of methods

Class Encapsulation and Abstraction Encapsulation- the qualities/state and behaviors are bound together into the same class The detail of implementation is hidden from the user Class abstraction- to separate class implementation from the use of the class The creator of the class provides a description of the class which the user can then use The user of the class does not need to know how the class is implemented Real-life example: Computer hardware

Classes to Objects Class is a template/recipe that defines the qualities/state and behaviors that an actual object can have Object is a particular instance of a class Example: A class of people- Children Instances of the Child class- Billy, Julie, Peggy, Bobby A Class is a datatype (Reference) as it defines what type of values the object can hold

Classes to Objects Visualization An implementation will create an object, request information to be put inside of it, then use the special purpose methods to accomplish the data manipulation of an object

Java Implementation of OOP How is Object-Oriented Programming done in in Java? Datatypes of variables 4 types of methods Usually separate classes for class definition and use of class

Datatypes within Objects Can be of primitive data types (int, boolean, char, etc.) Can be of reference types (String, etc.) Example: The following class contains a mix of data types (the default values depends on the datatype) Poor programming practice to rely on default values; makes code harder to understand public class Student { String name; // name has default value null int age; // age has default value 0 boolean isScienceMajor; // isScienceMajor has default value false char gender; // gender has default value '\u0000' }

OOP Behavior Design Four categories of behavior (methods): Constructors – create objects of the class Special purpose methods – relating to the object (e.g. find area of a circle, deposit into a checking account, …) Accessor (get) methods – returns quality/state value of an attribute to an external class Mutator (set) methods – change the quality/state value of an attribute based upon information given by an external class

Class Definition

Constructors Default Constructor No parameters Used when no state values specific to the particular object are known at object creation time Example: Circle() { radius = 1; } Note difference in approach from previous slide for default constructor; either approach is appropriate Specific Constructor At least one parameter Used when state values specific to a particular object are known by the application at object creation time Example: Circle(double newRadius) { radius = newRadius; }

Constructor Notes Constructors must have the same name as the class itself Constructors do not have a return type… not even void Unlike other methods, constructors can only be invoked with the new keyword/operator

Constructor Notes Classes have a default constructor available without having to define it unless there are other constructors specified, in which case the default constructor must be specified for it to be available

Creating Objects (through Invoking Constructors) 1.Declare type of variable: Classname objectRefVar; Example: Circle myCircle; 2.Create object and assign variable to object: objectRefVar = new ClassName(); Examples: myCircle = new Circle(); myOtherCircle = new Circle(5.0); In one step… ClassName objectRefVar = new ClassName(); Example: Circle myCircle = new Circle();

Accessing Data and Methods through Objects Referencing the object’s data: objectRefVar.datafieldname Example: myCircle.radius Invoking the object’s method: objectRefVar.methodName(arguments) Example: myCircle.findArea()

This is a different way of invoking methods than so far in the course Previously we called methods using notations like: Class.methodName(arguments) (e.g., Math.pow(3, 2.5)) Those methods had the keyword static in their method definition header. Although static methods can be invoked through an object, the preferred way is through the name of the class, as shown above However, class methods are often non-static. Non- static methods must be invoked from an object using the notation: objectRefVar.methodName(arguments) (e.g., myCircle.getArea()) NOT Circle1.getArea()

Instance vs. Static- Identifiers & Methods Instance: Variables (qualities/state) belong to a specific instance Methods (behaviors) are called by an instance of the class Static: Variables values are shared by all the instances of the class Methods are not tied to a specific object Constants are final variables shared by all the instances of the class, therefore usually static Declared using the static modifier Examples: static int numberOfObjects = 0; int getNumberOfObjects() { … }  static int getNumberOfObjects() { … }

Instance vs. Static- Identifiers & Methods Methods (behaviors) and Qualities/State (variables) values that are particular to a specific object should be created as instance, those common to all items of a class should be static Example: Each child has a particular height (instance) however all children are under the age of 13 (static)

public class TestSimpleCircle { public static void main(String[] args) { // Create a circle with radius 5.0 SimpleCircle myCircle = new SimpleCircle(5.0); System.out.println("The area of the circle of radius " + myCircle.radius + " is " + myCircle.findArea()); // Create a circle with radius 1 SimpleCircle yourCircle = new SimpleCircle(); System.out.println("The area of the circle of radius " + yourCircle.radius + " is " + yourCircle.findArea()); // Modify circle radius yourCircle.radius = 100; System.out.println("The area of the circle of radius " + yourCircle.radius + " is " + yourCircle.findArea()); } } // Define the circle class with two constructors class SimpleCircle { double radius; /** Construct a circle with radius 1 */ SimpleCircle() { radius = 1.0; } /** Construct a circle with a specified radius */ SimpleCircle(double newRadius) { radius = newRadius; } /** Return the area of this circle – note we don’t need to pass values into the method, because the necessary data is already in the object that is invoking the method */ double findArea() { return radius * radius * ; } } Class definition and implementation; Constructors and special purpose method

Placement of Classes into Java Files The definition of the class will often have no main method; the main method will be in the class that uses the class definition Each of these two classes are often placed in their own.java file; one can compile the class definition file, and then compile the other file and then run the latter; alternatively one can compile the latter and the class definition is automatically compiled, however under this latter approach one needs to pay attention to which file produces a particular compilation error

Placement of Classes into Java Files If one places both classes in the same.java file, both of the classes can't be declared public; declare the class public that matches the name of the java file; this will be the one with the main method since you want it to execute