Corresponds with Chapter 7

Slides:



Advertisements
Similar presentations
Based on Java Software Development, 5th Ed. By Lewis &Loftus
Advertisements

Road Map Introduction to object oriented programming. Classes
Introduction to Java Programming, 4E Y. Daniel Liang.
1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Classes and Class Members Chapter 3. 3 Public Interface Contract between class and its clients to fulfill certain responsibilities The client is an object.
Writing Classes (Chapter 4)
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
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.
Objects and Classes Chapter 6 CSCI CSCI 1302 – Objects and Classes2 Outline Introduction Defining Classes for Objects Constructing Objects Accessing.
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.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
1 Chapter 8 – Classes and Object: A Deeper Look Outline 1 Introduction 2 Implementing a Time Abstract Data Type with a Class 3 Class Scope 4 Controlling.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
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.
Introduction to c++ programming - object oriented programming concepts - Structured Vs OOP. Classes and objects - class definition - Objects - class scope.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
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 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Chapter 5 Introduction to Defining Classes
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.
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 6 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.
Introduction To Objects Oriented Programming Instructor: Mohammed Faisal.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 13: Introduction to Classes.
Lecture 9: Object and Classes Michael Hsu CSULA. 2 OO Programming Concepts Object-oriented programming (OOP) involves programming using objects. An object.
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.
Chapter 5 Introduction to Defining Classes Fundamentals of Java.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Objects and Classes. F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive data type and object type.
Class Inheritance Part II: Overriding and Polymorphism Corresponds with Chapter 10.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Lecture 3: Introduction to Object and Classes
Class Inheritance Part I
OOP: Encapsulation &Abstraction
Programming Logic and Design Seventh Edition
Chapter 3: Using Methods, Classes, and Objects
Introduction to Classes
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
HIGHLEVEL REVIEW Chapter 9 Objects and Classes
Object Based Programming
Introduction to Classes
Chapter 3 Introduction to Classes, Objects Methods and Strings
OBJECT ORIENTED PROGRAMMING II LECTURE 8 GEORGE KOUTSOGIANNAKIS
Chapter 9 Objects and Classes
Chapter 9 Objects and Classes
Chapter 8 Objects and Classes
Classes and Objects Part 2 Static Class Members and Arrays of Objects
Objects and Classes Creating Objects and Object Reference Variables
Outline Anatomy of a Class Encapsulation Anatomy of a Method
Chapter 6 Objects and Classes
Object Oriented Programming in java
Chapter 6 Objects and Classes
Chapter 8 Objects and Classes
Chapter 9 Objects and Classes
OO Programming Concepts
Chapter 6 Objects and Classes
Chapter 7 Objects and Classes
CSG2H3 Object Oriented Programming
Presentation transcript:

Corresponds with Chapter 7 Classes and Objects Corresponds with Chapter 7

Principles of Object Orientation Encapsulation Data and behavior pertaining to an object are bundled together into a single unit Information Hiding Provide an interface to the user of a class via public members Hide details about the implementation of the class via private members Inheritance Grouping classes into hierarchies of categories and subcategories Subclasses inherit the members of the base class Poymorphism Poly = many, Morph = form  Many Forms The same method call (using identical signature) produces different behaviors depending on the class

Classes and Instances (Objects) Class = category Like a data type Declared using the class keyword Contains declarations of members Variables Methods Instance = object An actual piece of data (created on the heap) Member variables contain actual data Method calls apply to the particular instance

Example – The SimpleCircle Class (similar to Listing 7. 1, 7 Example – The SimpleCircle Class (similar to Listing 7.1, 7.2 pp 234-235) This is the application class with main method This is the another class representing a specific kind of object.

Overloaded Constructor Class Declaration Member variable declaration Default constructor (no parameters) Overloaded Constructor Method declaration Entire class declaration: bundles the data (member variables) with the behavior (methods)  encapsulation

Unified Modeling Language Diagrams for representing object oriented systems. Static class diagram: Represents classes in the system Boxes for representing classes Top part of box contains name Middle part contains attributes (member variables) Lower part contains operations (methods, including constructors)

UML Class Diagram for SimpleCircle Class Class name Attributes Operations Class Visio drawing of UML diagram

Declaring Objects Example: ClassName objectName; SimpleCircle myCircle; Class is like data type Variable name indicates a reference to an instance of the class

Creating Objects Instantiation objectName=new ClassName(); Example: myCircle=new SimpleCircle(); assignment operation puts the reference into the variable. new keyword creates the object on the heap (instantiation) Note the need for ()….instantiation automatically invokes a constructor method (more on this later) Specify the class of object being instantiated.

Declaring/Creating Objects in a Single Step ClassName objectName = new ClassName(); Example: SimpleCircle myCircle = new SimpleCircle();

The SimpleCircle Class (cont.) Instantiating an object: new keyword creates the object on the heap Overloaded constructor is invoked to initialize member variables

Constructors A constructor is a special method that is automatically called when an instance of the class is created. Constructor’s purpose – perform initialization operations (e.g. set initial values for variables). The constructor’s identifier is EXACTLY the same as the class’s name. Constructors have no return type (not even void). When the new operator creates an instance of a class, the constructor is automatically invoked. Default constructor – no parameters Can overload constructor, just like any other method.

Frame Stack Heap Declaring the reference variable Creating the object myCircle Creating the object on the heap (instantiation) SimpleCircle object radius main’s frame args Frame Stack Heap

Constructor method is invoked SimpleCircle constructor frame newRadius 5.0 this main’s frame SimpleCircle object radius args myCircle Frame Stack Heap

The this pointer An instance method is a method that is associated with a specific instance (NOT a static method). Constructors are instance methods. Every instance method has a special reference called this. this points to the instance for which the method was invoked. this is the way that an instance method knows which object to access when using or manipulating its data this is also a keyword in the Java language, and can be used to access members from within the instance (more on this later).

The constructor (an instance method) can access the member variables of the object via the this pointer. SimpleCircle constructor frame newRadius 5.0 this main’s frame SimpleCircle object radius args 5.0 myCircle Frame Stack Heap

After constructor terminates (and its frame popped from the frame stack), the new operator returns the address where the object was created. This is assigned into the variable myCircle. main’s frame SimpleCircle object radius args 5.0 myCircle Frame Stack Heap

Accessing Objects Referencing the object’s data: objectName.data myCircle.radius Referencing the object’s method: objectName.method myCircle.findArea() The dot (.) is a member access operator. (Sometimes just called the “dot operator”)

The SimpleCircle Class (cont.) Variable used outside the class. Accessed via dot operator. Member variable declaration. Available to multiple methods (scope is wider than a single method). Variable used inside the class. Note: radius is an instance variable. Every instance of the Circle class will have a variable called radius. Later we’ll compare this to static, or class variables.

Instance Methods objectName.methodName(); Instance methods belong to instances. These methods can only be applied after the instances are created. Later we’ll compare these to static, or class methods. Instance methods are called by the following: objectName.methodName(); The name of the reference variable The dot operator The name of the method.

The SimpleCircle Class (cont.) The method call The method declaration

Calling the method Frame Stack Heap NOTE: the instance method knows which object to access via its this pointer. This is identical to the reference variable through which the method was called. findArea’s frame this main’s frame SimpleCircle object radius args 5.0 myCircle Frame Stack Heap

The SimpleCircle Class (cont.) Instantiating an second object: new keyword creates the object on the heap Default constructor is invoked to initialize member variables

Frame Stack Heap Creating the second object Declaring the reference variable yourCircle Creating the second object on the heap (instantiation) SimpleCircle object radius main’s frame SimpleCircle object radius args 5.0 myCircle Frame Stack Heap

The constructor (this time the default constructor) can access the member variables of the object via the this pointer. SimpleCircle constructor frame SimpleCircle object radius 1.0 this main’s frame SimpleCircle object radius args 5.0 myCircle yourCircle Frame Stack Heap

After constructor terminates (and its frame popped from the frame stack), the new operator returns the address where the object was created. This is assigned into the variable yourCircle. SimpleCircle object radius 1.0 main’s frame SimpleCircle object radius args 5.0 myCircle yourCircle Frame Stack Heap

The SimpleCircle Class (cont.) Accessing the member variable via the reference variable

We know which radius variable is being accessed because the reference variable points us to the appropriate object. SimpleCircle object radius 100.0 main’s frame SimpleCircle object radius args 5.0 myCircle yourCircle Frame Stack Heap

The SimpleCircle Class (cont.) Invoking the method via the reference variable

The instance method’s this pointer will point at the same object as the reference variable through which the method was invoked. This is how the method knows which object it is associated with. findArea’s frame SimpleCircle object radius 100.0 this main’s frame SimpleCircle object radius args 5.0 myCircle yourCircle Frame Stack Heap

Principles of Object Orientation Encapsulation Data and behavior pertaining to an object are bundled together into a single unit Information Hiding Provide an interface to the user of a class via public members Hide details about the implementation of the class via private members Inheritance Grouping classes into hierarchies of categories and subcategories Subclasses inherit the members of the base class Poymorphism Poly = many, Morph = form  Many Forms The same method call (using identical signature) produces different behaviors depending on the class

Visibility Modifiers for Members 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. protected The data or methods can be accessed by the declaring class, its subclasses, and other classes in the same package. (more on this later) By default, a class’s members are protected

Public vs. Private Use public is you want everyone to be able to access a member Use private if you want to restrict access to a member: For example, you may want to make member variables private so that users of a class cannot arbitrarily set values For these private members, access would be done via public methods called “accessor methods” (for obtaining values) and “mutator methods” of changing values. Mutator methods can perform input verification to ensure that values being assigned to a member variable are valid.

Accessor and Mutator Methods Purpose of accessor/mutator methods is to provide controlled access to private member variables Two types: get – accessor returns the value in a private member. Return type is same as data type of the member variable. set – mutator changes the contents of the private member. Takes a parameter of the same type as the private member. Often does some validation to ensure that entered the input parameter value is legitimate before placing it into the member variable. Typically returns a void.  Each private member variable that should be accessed from outside the class will need accessor and mutator methods associated with it.

Set mutator validates parameter value The Circle Class (includes private member variable and accessor/mutator methods) The member variable is private Public constructors Using accessor and mutator to manipulate private member variable Public accessor and mutator Set mutator validates parameter value

Unified Modeling Language Diagrams for representing object oriented systems. Static class diagram: In class box, visibility modifiers are represented with + for public - for private * for protected

UML Class Diagram for Circle Class Attributes are private ( - ) Operations are public (+) Accessor/mutator methods as operations Visio drawing of UML diagram

Passing Objects to Methods Object reference variables can be passed as arguments, just like other variables Since these are reference variables, the method receiving them has access to the original data (similar to when we passed arrays)

Passing a Circle Object (similar to Listing 7.9 p249) Reference to an instance Reference as an actual parameter The corresponding formal parameter