Presentation is loading. Please wait.

Presentation is loading. Please wait.

Objects and Classes Chapter 6 CSCI 1302. CSCI 1302 – Objects and Classes2 Outline Introduction Defining Classes for Objects Constructing Objects Accessing.

Similar presentations

Presentation on theme: "Objects and Classes Chapter 6 CSCI 1302. CSCI 1302 – Objects and Classes2 Outline Introduction Defining Classes for Objects Constructing Objects Accessing."— Presentation transcript:

1 Objects and Classes Chapter 6 CSCI 1302

2 CSCI 1302 – Objects and Classes2 Outline Introduction Defining Classes for Objects Constructing Objects Accessing Objects –Reference Variables and Types –Accessing an Object’s Data and Methods –The null Value –Differences Between Primitive and Reference Variable Types

3 CSCI 1302 – Objects and Classes3 Outline Using Classes from the Java Library Visibility Modifiers, Accessors, and Mutators Data Field Encapsulation Immutable Objects and Classes Passing Objects to Methods Static Variables, Constants, and Methods

4 CSCI 1302 – Objects and Classes4 Outline The this keyword Array of Objects Class Abstraction and Encapsulation Inner Classes Case Study: StackofIntegers

5 CSCI 1302 – Objects and Classes5 Introduction Procedural programming – data and operations are separate Object-oriented programming (OOP) – groups data and operations into a single entity Improves reusability, development, and maintenance

6 CSCI 1302 – Objects and Classes6 Defining Classes for Objects An object represents a real-world entity Has a unique identity, state, and behaviors State consists of data fields (properties) Behavior is defined by a set of methods State defines the object, behavior defines what it does

7 CSCI 1302 – Objects and Classes7 Defining Classes for Objects

8 CSCI 1302 – Objects and Classes8 Defining Classes for Objects Constructs that define objects of the same type Java classes use variables to define data fields and methods to define behaviors Also provide constructors used to create objects from the class

9 CSCI 1302 – Objects and Classes9 Defining Classes for Objects

10 CSCI 1302 – Objects and Classes10 Defining Classes for Objects

11 CSCI 1302 – Objects and Classes11 Constructing Objects Circle() { } Circle(double newRadius) { radius = newRadius; } Constructors with no parameters are referred to as no-arg constructors.

12 CSCI 1302 – Objects and Classes12 Constructing Objects Constructors Have the same name as the defining class Can be overloaded No return type Invoked using the new operator If no constructor is explicitly declared, a default no-arg constructor is created with an empty body

13 CSCI 1302 – Objects and Classes13 Constructing Objects Examples General new ClassName(); Specific new Circle(); new Circle(5.0);

14 CSCI 1302 – Objects and Classes14 Constructing Objects

15 CSCI 1302 – Objects and Classes15 Accessing Objects Can be accessed through reference variables ClassName objectRefVar; Circle myCircle; Class defines a reference type myCircle = new Circle(); One statement Circle myCircle = new Circle(); An object created without a reference is an anonymous object

16 CSCI 1302 – Objects and Classes16 Accessing Objects Accessing Data and Methods Can be accessed using dot notation; objectRefvar.method(arguments); Circle class: myCircle.radius; myCircle.findArea(); See the code in and for examples

17 CSCI 1302 – Objects and Classes17 The null value Default literal value for reference type variables Default values of data fields: No default values for local variables inside a method Reference typenull Numeric type0 booleanfalse char‘\u0000’

18 CSCI 1302 – Objects and Classes18 Differences Between Variable Types Two main types of variables: Primitive and Reference Primitive –Value is of the primitive type –Assignment sets the real value, copies contents Reference –Value is a reference to where an object is located –Assignment sets reference

19 CSCI 1302 – Objects and Classes19 Differences Between Variable Types Garbage collection reclaims previous c1 object automatically (can assign null to force collection) See code in

20 CSCI 1302 – Objects and Classes20 Using Classes from the Java Library Classes from the Java Library will often be used when developing programs You have already done this: System.out.println(“Some text here”); See Example 2.4 (p. 61) for an example using System.currentTimeMillis(); Promotes reuse of code

21 CSCI 1302 – Objects and Classes21 Using Classes from the Java Library Example: java.util.Date Provides a system-independent encapsulation of date and time Can be used to create an instance of the current date and time, use its toString() method to print time out Format: Mon Aug 15 13:19:37 EDT 2005 See and

22 CSCI 1302 – Objects and Classes22 Visibility Modifiers Java provides several modifiers to control access to data, methods, and classes –public – Accessible from any class –private – Accessible only from within its own class –Package-private – used if public or private is not specified, accessible by any class in the same package

23 CSCI 1302 – Objects and Classes23 Visibility Modifiers Only used for members of a class, not local variables inside methods

24 CSCI 1302 – Objects and Classes24 Data Field Encapsulation Declare the field private Use get (or accessor) methods to return values of data fields public returnType getProperty(); public boolean isProperty(); Use set (or mutator) methods to update data fields public void setProperty(dataType property); See

25 CSCI 1302 – Objects and Classes25 Immutable Objects and Classes An object, that once created, cannot be changed is an immutable object The class it is derived from is an immutable class A class with all private data fields and no mutator methods is not necessarily immutable (p. 227) See for an example of a immutable class

26 CSCI 1302 – Objects and Classes26 Passing Objects to Methods Objects can be passed to methods Reference to the object is what is actually passed Java passes all arguments using pass-by- value See for a simple example and for a more complex example

27 CSCI 1302 – Objects and Classes27 Static Variables, Constants, and Methods Instance variables are tied to specific instances of a class Static variables are shared between all instances of a class Static methods can be called without creating an instance of a class Declared using static keyword Class constants can be declared using the final keyword

28 CSCI 1302 – Objects and Classes28 Static Variables, Constants, and Methods

29 CSCI 1302 – Objects and Classes29 Scope of Variables Class variables have a scope of the entire class Methods and variables can be declared in any order in the class except when their value is based on other variables Local variables take precedence over class variables although it is bad form to reuse variable names in most cases

30 CSCI 1302 – Objects and Classes30 The this keyword Used to refer to the invoking class Used most often when parameter name in a method matches a data field in a class public void setRadius(double radius) { this.radius = radius; } See for example of constructor using this See

31 CSCI 1302 – Objects and Classes31 Array of Objects Object arrays can be created the same way that primitive arrays are created Circle[] circleArray = new Circle[10]; To initialize the array, use a for loop similar to this one for (int i = 0; i < circleArray.length; i++) { circleArray[i] = new Circle(); }

32 CSCI 1302 – Objects and Classes32 Array of Objects Actually involves two levels of referencing circleArray is an array of reference variables See

33 CSCI 1302 – Objects and Classes33 Class Abstraction and Encapsulation Class abstraction – Separation of class implementation from the use of a class Class contract – Collection of methods and fields that are accessible from outside the class, along with the description of their behavior Class encapsulation – Details of implementation are encapsulated and hidden from the user

34 CSCI 1302 – Objects and Classes34 Inner Classes A class defined within the scope of another class (AKA nested class) Can reference the data and methods defined in the outer class Can be declared public, protected, or private Can be declared static with some restrictions on access to the outer class

35 CSCI 1302 – Objects and Classes35 Inner Classes Objects for inner classes are often created in the outer class Can be created from outside classes Non-static: OuterClass.InnerClass innerObject = InnerClass(); Static: OuterClass.InnerClass innerObject = new OuterClass.InnerClass();

36 CSCI 1302 – Objects and Classes36 Case Study: StackofIntegers Data structure that holds objects in a last-in first-out (LIFO) fashion Stack operations: pop(), push(), and peek() Array-based implementation Stacks are used often in programming Java provides a full featured version in java.util.Stack

37 CSCI 1302 – Objects and Classes37 Case Study: StackofIntegers

Download ppt "Objects and Classes Chapter 6 CSCI 1302. CSCI 1302 – Objects and Classes2 Outline Introduction Defining Classes for Objects Constructing Objects Accessing."

Similar presentations

Ads by Google