Presentation is loading. Please wait.

Presentation is loading. Please wait.

objects CIS 421 Kutztown University

Similar presentations


Presentation on theme: "objects CIS 421 Kutztown University"— Presentation transcript:

1 objects CIS 421 Kutztown University
7/23/2018 Objects CIS 421 Kutztown University objects

2 What are Objects An aggregation
a heterogeneous collection of data members a set of behaviors to access and manipulate the data Objects often contain objects, since all but simple types are objects (including String)

3 What are Objects Objects are declared in a class
An object is an instantiation of the non-static members of a class Objects model real things For example an object might be an account (abstract thing), a vehicle (real thing), a purchase (transaction), or a mouse click (event)

4 What are Objects Real objects and model objects both have attributes and behavior A model object is an object modeled in Java The attributes are the data values or references to other objects The behaviors are the methods

5 What are Objects The fact that an object contains behavior and data values is called encapsulation To invoke the behavior, a message is sent to the object reference (invocation) The message must have the same spelling as the method name

6 Summary of What Objects Are
Objects encapsulate data values and behaviors upon those data They model real objects They receive messages to invoke their behaviors

7 The Purpose of a Class A class in Java is a mechanism used to define an object, among other possibilities Sometimes they are called templates for objects (Java doesn’t have C++ style templates) They act as a blue print of an object When an object is instantiated (created) the template is used

8 The Purpose of a Class class is a reserved word
In addition there is a visibility (access) modifier for each class and members (data and methods) of the class private, public, or protected

9 The Purpose of a Class A visibility modifier tells the system how to protect the members from the outside world (other objects) To obey the OO concept, data members should be private The class is usually public (it must be designated with an access qualifier) public methods constitute the user interface methods for internal use should be private (or protected)

10 Static Declarations Static declarations belong to the class
Some things MUST be static main() the System class’ declarations System.exit() method System.out stream InputReader is an example of a class containing only static declarations static declarations can be accessed via the class name or via an object of the class’ type

11 Objects vs. Simple Types
All object declarations are just a reference. The object must be instantiated Arrays are objects! Simple types allocate memory for the data by their declaration IntegerObject Example of ridiculously simplistic object

12 Example: Array of 10 int Two ways to declare reference int[] list;
no value inside [] represents a reference to an array of int size undetermined until instantiated Instantiate: list=new int[10] array now has 10 elements

13 Example: Array of 10 int Wait! Not always like this... IntegerObject:
Example of very simple object type IntegerObject Ilist[]= new IntegerObject[10] Array has 10 references, not objects. Must instantiate each separately Examples: MinMax

14 Built-in Java Objects Simple types all have object wrapper type;
Integer wraps int Double wraps double Character wraps char All are immutable Must be constructed with value that can’t be changed

15 Built-in Java Objects (con’t)
String is also immutable Note: String is special; can be constructed via assignment String S=“Hello”; S=“ABCD” S is constructed twice First, with “Hello” Then, with “ABCD” What happened to the first object S referred to (“pointed” at)?

16 Garbage Collection String object “Hello” was abandoned when S was reassigned Memory leak? No. Java has a built-in garbage collector All objects carry info on # of references When #references is 0, memory can be “collected” and placed back onto the heap e.g String S=“Hello” String T=S “Hello” has two references to it No destructors, or memory deallocator (e.g. C++ delete operator)


Download ppt "objects CIS 421 Kutztown University"

Similar presentations


Ads by Google