Object Based Programming

Slides:



Advertisements
Similar presentations
 Pearson Education, Inc. All rights reserved static Class Members static fields – Also known as class variables – Represents class-wide.
Advertisements

Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Chapter 10 THINKING IN OBJECTS 1 Object Oriented programming Instructor: Dr. Essam H. Houssein.
Road Map Introduction to object oriented programming. Classes
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
 Pearson Education, Inc. All rights reserved Classes and Objects: A Deeper Look - modified by Eileen Kraemer.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Java Classes Introduction and Chapter 1 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Object Based Programming. Summary Slide  Instantiating An Object  Encapsulation  Inheritance  Polymorphism –Overriding Methods –Overloading vs. Overriding.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
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.
Java Classes Appendix C © 2015 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
 2005 Pearson Education, Inc. All rights reserved Classes and Objects: A Deeper Look.
Object Based Programming Chapter 8. 2 In This Chapter We will learn about classes Garbage Collection Data Abstraction and encapsulation.
Android How to Program, 2/e © Copyright by Pearson Education, Inc. All Rights Reserved.
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.
Visual C# 2012 for Programmers © by Pearson Education, Inc. All Rights Reserved.
Visual C# 2012 for Programmers © by Pearson Education, Inc. All Rights Reserved.
 Pearson Education, Inc. All rights reserved Classes and Objects: A Deeper Look.
Summing Up Object Oriented Design. Four Major Components: Abstraction modeling real-life entities by essential information only Encapsulation clustering.
Dale Roberts Object Oriented Programming using Java - Final and Static Keywords Dale Roberts, Lecturer Computer Science, IUPUI
 2005 Pearson Education, Inc. All rights reserved Classes and Objects: A Deeper Look.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
CIS 270—Application Development II Chapter 8—Classes and Objects: A Deeper Look.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
CSCI 3328 Object Oriented Programming in C# Chapter 9: Classes and Objects: A Deeper Look – Exercises 1 Xiang Lian The University of Texas Rio Grande Valley.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
Chapter 4: More Object Concepts. Objectives Understand blocks and scope Overload a method Avoid ambiguity Create and call constructors with parameters.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
YG - CS Concept of Encapsulation What is encapsulation? - data and functions/methods are packaged together in the class normally.
 Pearson Education, Inc. All rights reserved Classes and Objects: A Deeper Look.
5.1 Basics of defining and using classes A review of class and object definitions A class is a template or blueprint for an object A class defines.
Jozef Goetz Credits: Copyright  Pearson Education, Inc. All rights reserved. expanded by J. Goetz, 2016.
Object-Oriented Programming: Classes and Objects Chapter 1 1.
Object Based Programming Chapter 8. 2 Contrast ____________________ Languages –Action oriented –Concentrate on writing ________________ –Data supports.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
CompSci 230 S Programming Techniques
Chapter 10 Thinking in Objects
More About Objects and Methods
Classes and Objects: A Deeper Look
Chapter 10 Thinking in Objects
Object-Oriented Programming: Classes and Objects
Chapter 10 Thinking in Objects
Methods Chapter 6.
Classes and Objects: A Deeper Look (Chapter 8)
Chapter 3: Using Methods, Classes, and Objects
More about OOP and ADTs Classes
Object-Oriented Programming: Classes and Objects
Chapter 9 Thinking in Objects
Corresponds with Chapter 7
Chapter 6 Methods: A Deeper Look
IFS410: Advanced Analysis and Design
Lecture 22 Inheritance Richard Gesick.
10.2 const (Constant) Objects and const Member Functions
More about OOP and ADTs Classes
CSCI 3328 Object Oriented Programming in C# Chapter 9: Classes and Objects: A Deeper Look – Exercises UTPA – Fall 2012 This set of slides is revised from.
More On Enumeration Types
Introduction to Classes and Objects
Object Oriented Programming in java
Chapter 9 Thinking in Objects
The University of Texas – Pan American
More About Objects and Methods
Outline Anatomy of a Class Encapsulation Anatomy of a Method
CS360 Client/Server Programming Using Java
Object Oriented Programming in java
OO Programming Concepts
Chapter 8 Classes and Objects: A Deeper Look
Classes and Objects Systems Programming.
Presentation transcript:

Object Based Programming Chapter 8

In This Chapter We will learn about classes Garbage Collection Data Abstraction and encapsulation

Contrast Procedural Languages Object Oriented Languages Action oriented Concentrate on writing functions Data supports the actions Object Oriented Languages Concentrate on creating reference types Look for nouns which indicate classes Fields are data members Methods support manipulation of the objects

Find the Objects … the Verbs What are verbs that represent things that the objects do? In this game, what are nouns that are potential classes?

Abstract Data Types (ADTs) Classes in Java make creation of ADTs easier Implementation details hidden from users of the class Client code not dependent on implementation Example of a class Figure 8.1 Time class – keeps track of time in 24 hour format

Declaring Classes Usually data fields are specified as private Methods are declared as public Any class member (data or method) which does not need to be accessed outside the class should be private It does not matter which comes first, data fields or methods The author prefers data fields first

Constructors Methods which have the same name as the class Included in a class to ensure that instance variables contain valid values when objects are created The constructor is called when a class object is created with the new Note: Do not have the constructor return a value

Using Classes View Figure 8.2 Note Advantages of classes Creation of a time object with new Call of the constructor Use of public methods to get and set the private data values Use of toString functions Advantages of classes Simplifies client perception of classes Reduces number of parameters

Class Scope Variables and methods belong to the class's scope Inside the class's scope All class members are available, accessible Outside the class's scope Client code that uses the class Only public members are available, accessible Access modifiers public and private control this availability

Controlling Access to Members The following would not be allowed – why? The data members are private. public class TimeTest2 { public static void main( String args[] ) { Time1 t = new Time1(); t.hour = 7; }

Using the this Reference Every object can access a reference to itself Use the keyword this If a method has a local variable with same name as class variable, use the this to access the class variable Explicit use of this can increase program clarity for human readers Note example, Figure 8.4

Using Overloaded Constructors Multiple constructors for the same class are allowed The signature of the constructors must be different Different numbers and/or types of parameters See example in Figure 8.5 and 8.6

Using Set and Get Methods Private data fields manipulated only by provided public methods Some methods used to set the data values Others used to get the values Return the value directly Print the value Note: this is not the same as making the data values public The set methods are written to ensure valid data values

Composition A class can have references to objects of other classes as members Example: An alarm clock class contains a Time object as one of its members Employee class has Date object as one of its members See Date Class, Figure 8.7 Employee Class Figure 8.8 Employee Test program Figure 8.9

Enumerations – enum types Declared with an enum declaration A comma-separated list of enum constants Declares an enum class with the following restrictions: enum types are implicitly final enum constants are implicitly static Attempting to create an object of an enum type with new is a compilation error

Enumerations – enum types enum constants can be used anywhere constants can enum constructor Like class constructors, can specify parameters and be overloaded Note use from chapter 6, Figure 6.9 See example, Figure 8.10 Note test program, Figure 8.11

Enumerations – enum types static method values Generated by the compiler for every enum Returns an array of the enum’s constants in the order in which they were declared static method range of class EnumSet Takes two parameters, the first and last enum constants in the desired range Returns an EnumSet containing the constants in that range, inclusive An enhanced for statement can iterate over an EnumSet as it can over an array

Enumeration as a Class When an enumeration is defined A class is created Default methods include toString equals ordinal valueOf Also possible to provide additional methods

Enumeration as a Class Consider the following example of an enumeration of card suits View definition of class Suit Note constructor, getColor View demonstration program

Enumeration as a Class View class LetterGrade An enumeration class Note class elements Consider the following code LetterGrade myGrade = LetterGrade.B_PLUS; Discuss the results of calls to various LetterGrade methods

Garbage Collection Garbage collection finalize method JVM marks an object for garbage collection when there are no more references to that object JVM’s garbage collector will retrieve those objects memory so it can be used for other objects See lines 27 – 32 of Figure 8.13 finalize method All classes in Java have the finalize method Inherited from the Object class finalize is called by the garbage collector when it performs termination housekeeping finalize takes no parameters and has return type void

static Class Members Normally each class object has its own copy of the instance variables Possible to specify a variable that is shared by all existing objects of the class Called a static variable Also called a class variable – class wide information Also possible to have a static method Can be called even if no object of the class has been instantiated Use class name, then dot, then static method name Note examples in Fig. 8.12, test pgm Fig. 8.13

static Class Members String objects are immutable String concatenation operations actually result in the creation of a new String object static methods cannot access non-static class members Also cannot use the this reference

static Class Members static method gc of class System Indicates that the garbage collector should make a best-effort attempt to reclaim objects eligible for garbage collection It is possible that no objects or only a subset of eligible objects will be collected Note example call in Fig. 8.13

static Import Recall static fields and methods of class Math J2SE 5.0 enables importing static members as if declared in class where used Note Figure 8.14 import static java.lang.Math.*

Final Instance Variables Principle of least privilege Code should have only the privilege and access it needs to accomplish its task, but no more final instance variables Keyword final Specifies that a variable is not modifiable (is a constant) final instance variables can be initialized at their declaration If they are not initialized in their declarations, they must be initialized in all constructors – Fig. 8.15

Packages Packages enable grouping together multiple related classes Specify a class to be part of a package with first line package myStuff; Place all classes in same directory which is named with the name of the package In your program which uses the package import myStuff.*;

Software Reusability Many classes exist in the Java API Avoid "reinventing the wheel" Study capabilities of Java API If it has a class that fits your needs, use it rather than creating your own

Data Abstraction & Encapsulation Information hiding Classes hide details of implementation from client Example: Stack can be implemented with array or with linked list Client program which depends on one implementation will have problems if new version with different implementation comes out Only use the methods provided