Chapter 10 THINKING IN OBJECTS 1 Object Oriented programming Instructor: Dr. Essam H. Houssein.

Slides:



Advertisements
Similar presentations
CS0007: Introduction to Computer Programming Introduction to Classes and Objects.
Advertisements

Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 1 Object-Oriented.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Immutable Objects and Classes.
Road Map Introduction to object oriented programming. Classes
1 Programming for Engineers in Python Autumn Lecture 5: Object Oriented Programming.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 9 Objects and Classes.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 9 Thinking in Objects.
ASP.NET Programming with C# and SQL Server First Edition
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 7 Object-Oriented Programming.
OBJECT ORIENTED PROGRAMMING IN C++ LECTURE
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 12 Object-Oriented Design.
UFCEUS-20-2 : Web Programming Lecture 5 : Object Oriented PHP (1)
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 10 Thinking in Objects.
Classes and Class Members Chapter 3. 3 Public Interface Contract between class and its clients to fulfill certain responsibilities The client is an object.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved COS240 O-O Languages AUBG,
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 12 Object-Oriented.
COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 10 Thinking in Objects.
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.
CSCI-383 Object-Oriented Programming & Design Lecture 13.
Objects and Classes Chapter 6 CSCI CSCI 1302 – Objects and Classes2 Outline Introduction Defining Classes for Objects Constructing Objects Accessing.
Chapter 9 Object-Oriented Software Development F Software Development Process F Analyze Relationships Among Objects F Class Development F Class Design.
Object Based Programming Chapter 8. 2 In This Chapter We will learn about classes Garbage Collection Data Abstraction and encapsulation.
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.
Reformatted slides from the textbook, C++ How to Program, 6/e Pearson Education, Inc. All rights reserved Chapter 3. [Lecture 02] Introduction to.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
Chapter 8 Objects and Classes Object Oriented programming Instructor: Dr. Essam H. Houssein.
Chapter 10 Defining Classes. The Internal Structure of Classes and Objects Object – collection of data and operations, in which the data can be accessed.
Introduction to c++ programming - object oriented programming concepts - Structured Vs OOP. Classes and objects - class definition - Objects - class scope.
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.
© 2007 Lawrenceville Press Slide 1 Chapter 8 Objects  A variable of a data type that is a class. Also called an instance of a class.  Stores data  Can.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 9 Objects and Classes.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 9 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, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 11 Object-Oriented.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 10 More on Objects and Classes.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Object Oriented Programming. OOP  The fundamental idea behind object-oriented programming is:  The real world consists of objects. Computer programs.
Introduction To Objects Oriented Programming Instructor: Mohammed Faisal.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Fall 2013 Chapter 10 Thinking.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 9 Introduction of Object Oriented Programming.
Lecture 3: Introduction to Object and Classes Michael Hsu CSULA.
Lecture 3: Introduction to Object and Classes Michael Hsu CSULA.
Object Based Programming Chapter 8. 2 Contrast ____________________ Languages –Action oriented –Concentrate on writing ________________ –Data supports.
Chapter 10 Thinking in Objects
Chapter 7 Object-Oriented Programming
Programming Logic and Design Seventh Edition
Chapter 10 Thinking in Objects
Reference: COS240 Syllabus
Object-Oriented Programming: Classes and Objects
Chapter 10 Thinking in Objects
Chapter 9 More on Objects and Classes
Chapter 3: Using Methods, Classes, and Objects
About the Presentations
HIGHLEVEL REVIEW Chapter 9 Objects and Classes
Object Based Programming
Chapter 9 Thinking in Objects
Chapter 10 Thinking in Objects
Chapter 10 Thinking in Objects
Chapter 9 Objects and Classes
Chapter 9 Thinking in Objects
Object Oriented Programming in java
Chapter 9 Objects and Classes
CS 112 Programming 2 Lecture 02 Abstract Classes & Interfaces (2)
Object-Oriented PHP (1)
OO Programming Concepts
Chapter 10 Thinking in Objects
Presentation transcript:

Chapter 10 THINKING IN OBJECTS 1 Object Oriented programming Instructor: Dr. Essam H. Houssein

Immutable Objects and Classes Normally, you create an object and allow its contents to be changed later. Occasionally it is desirable to create an object whose contents cannot be changed, once the object is created. We call such an object an immutable object and its class an immutable class. The String class, for example, is immutable. If you deleted the set method in the Circle class in Listing 8.9, the class would be immutable, because radius is private and cannot be changed without a set method. 2

If a class is immutable, then all its data fields must be private and it cannot contain public set methods for any data fields. 3

For a class to be immutable, it must meet the following requirements: ■ all data fields private; ■ no mutator methods; ■ no accessor method that returns a reference to a data field that is mutable. 4

The Scope of Variables Instance and static variables in a class are referred to as the class’s variables or data fields. A variable defined inside a method is referred to as a local variable. The scope of a class’s variables is the entire class, regardless of where the variables are declared. A class’s variables and methods can appear in any order in the class. The exception is when a data field is initialized based on a reference to another data field. 5

You can declare a class’s variable only once, but you can declare the same variable name in a method many times in different non nesting blocks. If a local variable has the same name as a class’s variable, the local variable takes precedence and the class’s variable with the same name is hidden. For example, in the following program, x is defined as an instance variable and as a local variable in the method. 6

7

The this Reference class’s hidden data fields The this keyword is the name of a reference that refers to a calling object itself. One of its common uses is to reference a class’s hidden data fields. For example, a data-field name is often used as the parameter name in set method for the data field. In this case, the data field is hidden in the set method. You need to reference the hidden data-field name in the method in order to set a new value to it. 8

A hidden static variable can be accessed simply by using the keyword this ClassName.StaticVariable reference. A hidden instance variable can be accessed by using the keyword this, as shown in Figure 10.2(a). 9

10

Another common use of the this keyword is to enable a constructor to invoke another constructor of the same class. For example, you can rewrite the Circle class as follows: 11

12

Class Abstraction and Encapsulation Java provides many levels of abstraction. Class abstraction is the separation of class implementation from the use of a class. The creator of a class describes it and lets the user know how it can be used. The collection of methods and fields that are accessible from outside the class, together with the description of how these members are expected to behave, serves as the class’s contract. 13

The details of implementation are encapsulated and hidden from the user. This is known as class encapsulation. 14

Object-Oriented Thinking Classes provide more flexibility and modularity for building reusable Software. The procedural paradigm focuses on designing methods. The object oriented paradigm couples data and methods together into objects. Software design using the object-oriented paradigm focuses on objects and operations on objects. The object-oriented approach combines the power of the procedural paradigm with an added dimension that integrates data with operations into objects. 15

In procedural programming, data and operations on the data are separate, and this methodology requires sending data to methods. Object-oriented programming places data and the operations that pertain to them in an object. This approach solves many of the problems inherent in procedural programming. 16

The object-oriented programming approach organizes programs in a way that mirrors the real world, in which all objects are associated with both attributes and activities. Using objects improves software reusability and makes programs easier to develop and easier to maintain. Programming in Java involves thinking in terms of objects; a Java program can be viewed as a collection of cooperating objects. 17

Class Design Guidelines 1- Cohesion A class should describe a single entity, and all the class operations should logically fit together to support a coherent purpose. You can use a class for students, for example, but you should not combine students and staff in the same class, because students and staff are different entities. 18

2- Consistency -Follow standard Java programming style and naming conventions. - Choose informative names for classes, data fields, and methods. - A popular style is to place the data declaration before the constructor and place constructors before methods. - naming consistency - no-arg constructor - If you want to prevent users from creating an object for a class, you may declare a private constructor in the class, 19

3- Encapsulation A class should use the private modifier to hide its data from direct access by clients. This makes the class easy to maintain. Provide a get method only if you want the field to be readable, and provide a set method only if you want the field to be updateable. 20

4- Clarity Cohesion, consistency, and encapsulation are good guidelines for achieving design clarity. Additionally, a class should have a clear contract that is easy to explain and easy to understand. 21

5- Completeness Classes are designed for use by many different customers. In order to be useful in a wide range of applications, a class should provide a variety of ways for customization through properties and methods. 22

6- Instance vs. Static A variable or method that is dependent on a specific instance of the class should be an instance variable or method. A variable that is shared by all the instances of a class should be declared static. 23

Always reference static variables and methods from a class name (rather than a reference variable) to improve readability and avoid errors. Do not pass a parameter from a constructor to initialize a static data field. It is better to use a set method to change the static data field. 24

Instance and static are integral parts of object-oriented programming. A data field or method is either instance or static. Do not mistakenly overlook static data fields or methods. It is a common design error to define an instance method that should have been static. For example, the factorial(int n) method for computing the factorial of n should be defined static, because it is independent of any specific instance. 25

Thanks for Attention