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.

Slides:



Advertisements
Similar presentations
Final and Abstract Classes
Advertisements

Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
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.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 9 Objects and Classes.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Programming Languages and Paradigms Object-Oriented Programming.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Classes and Class Members Chapter 3. 3 Public Interface Contract between class and its clients to fulfill certain responsibilities The client is an object.
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
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.
Chapter 6 Object-Oriented Java Script JavaScript, Third Edition.
1 Introduction Modules  Most computer programs solve much larger problem than the examples in last sessions.  The problem is more manageable and easy.
Objects and Classes Chapter 6 CSCI CSCI 1302 – Objects and Classes2 Outline Introduction Defining Classes for Objects Constructing Objects Accessing.
 2005 Pearson Education, Inc. All rights reserved Classes and Objects: A Deeper Look.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Introduction To Classes Chapter Procedural And Object Oriented Programming Procedural programming focuses on the process/actions that occur in a.
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.
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.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
Visual C# 2012 for Programmers © by Pearson Education, Inc. All Rights Reserved.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo CET 3640 © Copyright by Pearson Education, Inc. All Rights Reserved.
 Pearson Education, Inc. All rights reserved Classes and Objects: A Deeper Look.
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.
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
CS170 ygao JAVA, C4Slide 1. CS170 ygao JAVA, C4Slide 2.
Copyright © 2002 W. A. Tucker1 Chapter 10 Lecture Notes Bill Tucker Austin Community College COSC 1315.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
Object Oriented Programming
CIS 270—Application Development II Chapter 8—Classes and Objects: A Deeper Look.
CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
Object-Based Programming in VB.NET. Must Understand Following: Encapsulation Information hiding Abstract Data Type Class, Instance, Reference Variable.
Classes, Interfaces and Packages
YG - CS Concept of Encapsulation What is encapsulation? - data and functions/methods are packaged together in the class normally.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 13: Introduction to Classes.
Jozef Goetz Credits: Copyright  Pearson Education, Inc. All rights reserved. expanded by J. Goetz, 2016.
Object-Oriented Programming: Classes and Objects Chapter 1 1.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 9 Introduction of Object Oriented Programming.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Lecture 3: Introduction to Object and Classes Michael Hsu CSULA.
Chapter 5 Introduction to Defining Classes Fundamentals of Java.
Object Based Programming Chapter 8. 2 Contrast ____________________ Languages –Action oriented –Concentrate on writing ________________ –Data supports.
Object Oriented Programming. Constructors  Constructors are like special methods that are called implicitly as soon as an object is instantiated (i.e.
Object-Oriented Programming: Classes and Objects
Classes and Objects: A Deeper Look (Chapter 8)
Chapter 3: Using Methods, Classes, and Objects
Introduction to Classes
Object-Oriented Programming: Classes and Objects
Object Based Programming
Corresponds with Chapter 7
IFS410: Advanced Analysis and Design
Week 3 Object-based Programming: Classes and Objects
Object Oriented Programming in java
Week 4 Object-based Programming (2) Classes and Objects: A Deeper Look
Final and Abstract Classes
2.1 Introduction to Object-Oriented Programming
Chapter 7 Objects and Classes
Presentation transcript:

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 Access to Members 5 Referring to the Current Object’s Members with this 6 Initializing Class Objects: Constructors 7 Using Overloaded Constructors 8 Using Set and Get Methods 9 Composition 10 Garbage Collection 11 Static Class Members 12 Final Instance Variables 13 Creating Packages 14 Package Access

2 1 Introduction Object Oriented Programming (OOP) –Encapsulates data (attributes) and methods (behaviors) Objects –Allows objects to communicate Well-defined interfaces

3 8.1 Introduction (cont.) Procedural programming language –C is an example –Action-oriented –Functions are units of programming Object-oriented programming language –Java is an example –Object-oriented –Classes are units of programming Functions, or methods, are encapsulated in classes

4 1 Introduction (cont.) This chapter discusses –How to create objects –How to use objects –You know something about it, but not everything…

5 2 Implementing a Time Abstract Data Type with a Class We introduce classes Time1 and TimeTest –Time1.java declares class Time1 –TimeTest.java declares class TimeTest –public classes must be declared in separate files –Class Time1 will not execute by itself Does not have method main TimeTest, which has method main, creates (instantiates) and uses Time1 object

6 2 Implementing a Time Abstract Data Type with a Class (cont.) Every Java class must extend another class –Time1 extends java.lang.Object –If class does not explicitly extend another class class implicitly extends Object Class constructor –Same name as class –Initializes instance variables of a class object –Called when program instantiates an object of that class –Can take arguments, but cannot return values –Class can have several constructors, through overloading –Class Time1 constructor

7 3 Class Scope Class scope –Class variables and methods –Members are accessible to all class methods –Members can be referenced by name objectReferenceName.objectMemberName –Shadowed (hidden) class variables this.variableName

8 4 Controlling Access to Members Member access modifiers –Control access to class’s variables and methods –public Variables and methods accessible to clients of the class –private Variables and methods not accessible to clients of the class

9 5 Referring to the Current Object’s Members with this Keyword this ( this reference) –Allows an object to refers to itself

10 6 Initializing Class Objects: Constructors Class constructor –Same name as class –Initializes instance variables of a class object –Call class constructor to instantiate object of that class new ClassName ( argument1, argument2, …, arugmentN ); new indicates that new object is created ClassName indicates type of object created arguments specifies constructor argument values

11 7 Using Overloaded Constructors Overloaded constructors –Methods (in same class) may have same name –Must have different parameter lists

12 8 Using Set and Get Methods Accessor method (“get” method) –public method –Allow clients to read private data Mutator method (“set” method) –public method –Allow clients to modify private data

13 9 Composition Composition –Class contains references to objects of other classes These references are members

Garbage Collection Garbage collection –Returns memory to system –Java performs this automatically object marked for garbage collection if no references to object Finalizer method –Returns resources to system –Java provides method finalize Defined in java.lang.Object Receives no parameters Returns void

15 11 Static Class Members static keyword –static class variable Class-wide information –All class objects share same data Access to a class’s public static members –Qualify the member name with the class name and a dot (.) e.g., Math.random()

16 12 Final Instance Variables final keyword –Indicates that variable is not modifiable Any attempt to modify final variable results in error private final int INCREMENT = 5; Declares variable INCREMENT as a constant –Enforces principle of least privilege

17 13 Creating Packages We can import packages into programs –Group of related classes and interfaces –Help manage complexity of application components –Facilitate software reuse –Provide convention for unique class names Popular package-naming convention –Reverse Internet domain name e.g., com.deitel

18 14 Package Access Package access –Variable or method does not have member access modifier