CS451 - Lecture 2 1 CS451 Lecture 2: Introduction to Object Orientation Yugi Lee STB #555 (816)...

28
CS451 - Lecture 2 1 CS451 Lecture 2: Introduction to Object Orientation Yugi Lee STB #555 (816) 235-5932 [email protected] www.sice.umkc.edu/~leeyu *Acknowledgement: This lecture is based on material courtesy of Monash University

Transcript of CS451 - Lecture 2 1 CS451 Lecture 2: Introduction to Object Orientation Yugi Lee STB #555 (816)...

Page 1: CS451 - Lecture 2 1 CS451 Lecture 2: Introduction to Object Orientation Yugi Lee STB #555 (816) 235-5932 leeyu@umkc.edu leeyu * Acknowledgement:

CS451 - Lecture 21

CS451Lecture 2: Introduction to Object Orientation

Yugi Lee

STB #555(816) 235-5932

[email protected]

www.sice.umkc.edu/~leeyu

*Acknowledgement: This lecture is based on material courtesy of Monash University

Page 2: CS451 - Lecture 2 1 CS451 Lecture 2: Introduction to Object Orientation Yugi Lee STB #555 (816) 235-5932 leeyu@umkc.edu leeyu * Acknowledgement:

2CS451 - Lecture 2

Content

• Basic OOConcepts

AbstractDataTypes

Encapsulation

MessageSending

• Classes, Objects and State

Attributes

Instance

Class

Object creation & destruction

• Object Behaviour

Methods

InstanceMethods

ClassMethods

MethodOverloading

• Object Interfaces

Interface classes

AccessModifiers

Inheritance

Page 3: CS451 - Lecture 2 1 CS451 Lecture 2: Introduction to Object Orientation Yugi Lee STB #555 (816) 235-5932 leeyu@umkc.edu leeyu * Acknowledgement:

3CS451 - Lecture 2

Object-oriented programming

• Construction of system which is structured collections of "classes", or abstract data type implementations.

• These classes produce runtime instances called "objects" which communicate by sending messages to each other.

• Each ADT encapsulates the state and behavior of its real-world counterpart.

• Using Java, OO concepts will be explanined.• Compare this with how structured programs are

developed!

Page 4: CS451 - Lecture 2 1 CS451 Lecture 2: Introduction to Object Orientation Yugi Lee STB #555 (816) 235-5932 leeyu@umkc.edu leeyu * Acknowledgement:

4CS451 - Lecture 2

Object Oriented Concepts

• Abstract Data Type (ADT) – an abstract representation of a real-world entity

• Encapsulation – the combination of state and behavior into a single structure, whi

lst hiding the implementation

• Message sending – the process by which objects communicate

• Classes and objects – ADT implementations and their runtime equivalents

Page 5: CS451 - Lecture 2 1 CS451 Lecture 2: Introduction to Object Orientation Yugi Lee STB #555 (816) 235-5932 leeyu@umkc.edu leeyu * Acknowledgement:

5CS451 - Lecture 2

Abstract Data Types (ADTs)

• Information Hiding and ADTs (1960-70) An ADT is a specification of a real-world "object"

with both hidden state and public behavior Implementation (class body) hides the data

representation and function realization at lower level The name of a class names an ADT (the interface(s)),

the type of data (the instances of the class), and the implementation (the hidden details)

Page 6: CS451 - Lecture 2 1 CS451 Lecture 2: Introduction to Object Orientation Yugi Lee STB #555 (816) 235-5932 leeyu@umkc.edu leeyu * Acknowledgement:

6CS451 - Lecture 2

Abstract Data Types (ADTs)

E.g. int:primitive Java datatype with state (i.e., the value it holds), but no internal behaviour

E.g.

Page 7: CS451 - Lecture 2 1 CS451 Lecture 2: Introduction to Object Orientation Yugi Lee STB #555 (816) 235-5932 leeyu@umkc.edu leeyu * Acknowledgement:

7CS451 - Lecture 2

Encapsulation: state plus behavior

• the combination of data and functionality into a single entity, with the implementation hidden from external entities

the other side of the ADT coin, of the separation of interface and implementation

a "black-box" approach to designing OO systems

Page 8: CS451 - Lecture 2 1 CS451 Lecture 2: Introduction to Object Orientation Yugi Lee STB #555 (816) 235-5932 leeyu@umkc.edu leeyu * Acknowledgement:

8CS451 - Lecture 2

Encapsulation: state plus behavior

can only be achieved by "hiding" details of a class' implementation and letting other classes communicate with it only through a well defined set of messages (message sending)

Page 9: CS451 - Lecture 2 1 CS451 Lecture 2: Introduction to Object Orientation Yugi Lee STB #555 (816) 235-5932 leeyu@umkc.edu leeyu * Acknowledgement:

9CS451 - Lecture 2

Message sending

Message sending is the technique objects use to communicate with each other (similar to calling a function)

Message handling: the same message can be sent to different objects, each may "respond" in a different way (polymorphism)

Methods of objects - comparable to function definitions - define the "response" of an object to a message.

Page 10: CS451 - Lecture 2 1 CS451 Lecture 2: Introduction to Object Orientation Yugi Lee STB #555 (816) 235-5932 leeyu@umkc.edu leeyu * Acknowledgement:

10CS451 - Lecture 2

Message Sending

For example, if a bank Customer wished to deposit some money into their Account, the Customer object would send the "deposit" message to their Account object, along with an amount to deposit

Page 11: CS451 - Lecture 2 1 CS451 Lecture 2: Introduction to Object Orientation Yugi Lee STB #555 (816) 235-5932 leeyu@umkc.edu leeyu * Acknowledgement:

11CS451 - Lecture 2

Classes, Objects and State

A class

• may only exist at compile time

• is a template/pattern for an object

• "only exists once"

• is an implementation of an ADT

• is by a class declaration

An Object• exists at runtime only

• is created/instantiated from a class specification

• can be created many times from one class

• is a runtime instance of a class

• is returned by the new operator

Page 12: CS451 - Lecture 2 1 CS451 Lecture 2: Introduction to Object Orientation Yugi Lee STB #555 (816) 235-5932 leeyu@umkc.edu leeyu * Acknowledgement:

12CS451 - Lecture 2

Attributes

• ... are used to represent state.

• part of the interface and accessible only through methods or hidden and represent the internal state of objects.

variables which have some connection to a class and are declared, initialized, referenced and assigned

primitive, user-defined, or a class from the JDK API

Page 13: CS451 - Lecture 2 1 CS451 Lecture 2: Introduction to Object Orientation Yugi Lee STB #555 (816) 235-5932 leeyu@umkc.edu leeyu * Acknowledgement:

13CS451 - Lecture 2

Attributes

Instance variables store information pertaining to one particular object's state (by far the most common of the two types of attribute)

Class variables store information pertaining to all objects of one class

Page 14: CS451 - Lecture 2 1 CS451 Lecture 2: Introduction to Object Orientation Yugi Lee STB #555 (816) 235-5932 leeyu@umkc.edu leeyu * Acknowledgement:

14CS451 - Lecture 2

Instance variables

• Visible inside class; each instance object has its own values associated to the instance variable

To access an instance variable from within it's class, the name of the variable itself is

used

from outside its class, through a valid object reference

Initialization performed when object is created

Page 15: CS451 - Lecture 2 1 CS451 Lecture 2: Introduction to Object Orientation Yugi Lee STB #555 (816) 235-5932 leeyu@umkc.edu leeyu * Acknowledgement:

15CS451 - Lecture 2

Class variables

Value shared by all instances of the class; sort of a "global variable" to all instances

Details Declared with the static keyword Initialized before main runs Writable only for methods in class (unless public)

If public, use dotting ClassName.ClassAttribute to access from outside class;

Page 16: CS451 - Lecture 2 1 CS451 Lecture 2: Introduction to Object Orientation Yugi Lee STB #555 (816) 235-5932 leeyu@umkc.edu leeyu * Acknowledgement:

16CS451 - Lecture 2

Object creation and destruction

• Objects often require special initialization when they are first created.

• constructors are automatically called once a new instance object is created.

• finalize is required when an object is just about to be de-allocated. (Java: not allow manual memory allocation)

Page 17: CS451 - Lecture 2 1 CS451 Lecture 2: Introduction to Object Orientation Yugi Lee STB #555 (816) 235-5932 leeyu@umkc.edu leeyu * Acknowledgement:

17CS451 - Lecture 2

Object creation and destruction

new: allocates the object and returns a reference to the newly-instantiated object invokes all instance variable initialization

invokes the constructor to initialize the object.

• finalize not equivalent to a destructor (release all critical

resources implicitly)

Called before the garbage collector frees up the object's memory

Page 18: CS451 - Lecture 2 1 CS451 Lecture 2: Introduction to Object Orientation Yugi Lee STB #555 (816) 235-5932 leeyu@umkc.edu leeyu * Acknowledgement:

18CS451 - Lecture 2

Object behavior (Method)

• defined by its methods, which have some connection to a class

associated with a signature: a unique identifying name and parameter list, and a return type

all parameters are passed by value instance methods (invoked on an object), and

class methods (invoked on the class itself, even if no objects of that type exist)

Page 19: CS451 - Lecture 2 1 CS451 Lecture 2: Introduction to Object Orientation Yugi Lee STB #555 (816) 235-5932 leeyu@umkc.edu leeyu * Acknowledgement:

19CS451 - Lecture 2

Instance methods

• methods you call on instances of a class. They make up the majority of the behavior in most classes.

• Encapsulation "requires" accessor and mutator methods for all instance variables

• accessing an instance method from outside the class is through a current object reference with the dot (.) notation

Page 20: CS451 - Lecture 2 1 CS451 Lecture 2: Introduction to Object Orientation Yugi Lee STB #555 (816) 235-5932 leeyu@umkc.edu leeyu * Acknowledgement:

20CS451 - Lecture 2

Class methods

Can only operate on class variables (e.g., static)

To access a class method from outside the class, you must use the name of the class with the dot (.) notation as in Class.foo

Use class method name to access from within the class

Page 21: CS451 - Lecture 2 1 CS451 Lecture 2: Introduction to Object Orientation Yugi Lee STB #555 (816) 235-5932 leeyu@umkc.edu leeyu * Acknowledgement:

21CS451 - Lecture 2

Method overloading

• Method overloading occurs whenever two or more methods have the same name, but different parameter lists in terms of number and/or type of parameters

resolved by matching number and types of parameters in method invocation against arguments in method definition

if no exact match, type coercions (type casts) are used to find the closest match

Page 22: CS451 - Lecture 2 1 CS451 Lecture 2: Introduction to Object Orientation Yugi Lee STB #555 (816) 235-5932 leeyu@umkc.edu leeyu * Acknowledgement:

22CS451 - Lecture 2

Object Interfaces

• The interface keyword directly supports the separation of interface definition (of abstract data types) from their implementation.

• The implements keyword indicates that a class implements one or more interfaces.

• A class must conform with any interface it implements. This means every method specified in the interface must be implemented.

Page 23: CS451 - Lecture 2 1 CS451 Lecture 2: Introduction to Object Orientation Yugi Lee STB #555 (816) 235-5932 leeyu@umkc.edu leeyu * Acknowledgement:

23CS451 - Lecture 2

Interface Classes

• Interface classes list only the signatures not bodies of methods.

• Interfaces support a kind of multiple inheritance: an interface or an implementation class can be subclass (can implement) several interfaces.

• No code is inherited from any of the interface class.

Page 24: CS451 - Lecture 2 1 CS451 Lecture 2: Introduction to Object Orientation Yugi Lee STB #555 (816) 235-5932 leeyu@umkc.edu leeyu * Acknowledgement:

24CS451 - Lecture 2

Interface Classes

The larger a class' public interface, the closer the coupling of that class to other classes

The closer a class' coupling, the more work is required by client classes when the supplier changes

to decrease coupling and increase modularity Keep interface classes small

Use Java access modifiers wisely to maintain encapsulation and reduce coupling

Keep all state information inside the class

Page 25: CS451 - Lecture 2 1 CS451 Lecture 2: Introduction to Object Orientation Yugi Lee STB #555 (816) 235-5932 leeyu@umkc.edu leeyu * Acknowledgement:

25CS451 - Lecture 2

Access modifiers: private and public

• In implementation classes, access modifier is attached to both methods and attributes at declaration time to govern their visibility to other classes.

• A class' public interface is the set of methods and attributes (accessor methods) accessible from outside the class.

• Public/Private: Accessible from any other class - provided an object reference is available / Accessible only from within the class in which they are declared

Page 26: CS451 - Lecture 2 1 CS451 Lecture 2: Introduction to Object Orientation Yugi Lee STB #555 (816) 235-5932 leeyu@umkc.edu leeyu * Acknowledgement:

26CS451 - Lecture 2

Inheritance

• class A is the...

– parent

– ancestor

– superclass

– base class

• class B is the…– child

– descendent

– subclass

– derived class

Page 27: CS451 - Lecture 2 1 CS451 Lecture 2: Introduction to Object Orientation Yugi Lee STB #555 (816) 235-5932 leeyu@umkc.edu leeyu * Acknowledgement:

27CS451 - Lecture 2

Inheritance

• A child class will inherit all non-private attributes and methods from it's parent

Subclasses are said to "specialize" superclasses' functionality and/or state

Base classes are often left unimplemented, and serve merely as a specification for a minimum subset of data and functionality in derived classes

Page 28: CS451 - Lecture 2 1 CS451 Lecture 2: Introduction to Object Orientation Yugi Lee STB #555 (816) 235-5932 leeyu@umkc.edu leeyu * Acknowledgement:

28CS451 - Lecture 2

Inheritance

accomplished through the use of the extends keyword

Inheritance implies an "is-a" relationship between two classes (i.e., class B "is-a" type of class A, if class B inherits from class A)

If the derived class implements additional functionality to the parent class, the relationship is between the two classes is referred to as is-a-kind-of rather than just "is-a".