Chapter2 bag2

16
2.1 Introduction to Object- Oriented Programming

description

 

Transcript of Chapter2 bag2

Page 1: Chapter2 bag2

2.1 Introduction to Object-

Oriented Programming

Page 2: Chapter2 bag2

2.1.1 Procedural versus OOP languages

• Procedural programming involves

creating a sequence of instructions

• OOP uses a collection of

interacting objects

• Functions are logically grouped,

making enhancements easy

• OOP can model human problem-

solving

Page 3: Chapter2 bag2

2.1.2 Basic Java terminology

Object. contains data and instructions

Class. blueprint for an object

Attribute. describe the state of objects

Data Type. describes what kind of information a certain attribute is

Behavior. describe what objects can do

Method. a set of instructions

Inheritance. Some objects derive attributes and behaviors from other objects

Encapsulation. Combining data and methods together

Page 4: Chapter2 bag2

2.2 What are Objects? 2.2.1 Introduction to objects

• Anything tangible or abstract that is

relevent

• Objects can have attributes and

behaviors

• Attributes describe the object

• Behaviors describe what the object

can do

Page 5: Chapter2 bag2

2.2.2 Classification of objects

• User Interface objects

– Objects that the user interacts directly with

• Operating environment objects

– Provide services to other components

• Task Related objects– Documents, multimedia, problem

domain

Page 6: Chapter2 bag2

2.2.3 Objects - identifying, defining, creating and operating on

• Identifying – Requires needs assessment

• Defining – Classification, relationships,

operations. The class keyword

• Creating – The constructor and the new

keyword

• Operating – Using an object’s methods

Page 7: Chapter2 bag2

2.2.7 Encapsulation

• To hide the details, package together

• Access modifiers – public, private

and protected

Page 8: Chapter2 bag2

2.2.8 Object relationships • Association

– Objects knowabout each other

• Whole-part

– Existance of an object relies on another

• Inheritance– Attributes &

behaviors can be inherited

Page 9: Chapter2 bag2

2.2.9 Inheritance

Page 10: Chapter2 bag2

2.2.10 Object mutability and destruction

• Some object attributes should be immutable, or unchangable

• To make an item immutable, use the finalkeyword

• The JVM automatically releases memory when objects are no longer required

• The garbage collector will reclaim memory by objects that are no longer referenced

• Garage collection is not controllable

Page 11: Chapter2 bag2

2.3.1 Modeling languages and symbols

• Unified Modeling Language (UML) standardizes symbols & terminology for

diagramming objects

Page 12: Chapter2 bag2

2.3.2 Basic Class symbol

• Rectangle represents a class of objects

• 3 compartments: Name, attributes , and operations or methods

• Symbols indicate accessibility

Page 13: Chapter2 bag2

2.4.1 Class definition

public class Person {

private String name;

public Person(String theName) {

name = theName;

}

}

•Defined by using

the class keyword

•Public classes

defined in separate

files

•Filename must be

the same as the

class name

Page 14: Chapter2 bag2

2.4.2 Creating objects

• Constructor methods are called when an

object is created

• All object data is stored in memory

• Variables are a reference to the memory

location

Page 15: Chapter2 bag2

2.4.3 Object methods

• Mutator – Changes object data

• Accessor – Retrieves object data

• Methods with no arguments

• Methods that require arguments

• Method return values – Provide a result to

the caller

Page 16: Chapter2 bag2

2.5.1 System class

• Data stored by an object is member data

• Data present in an object’s methods is

called local or temporary data

• Java.lang.System is a pre-defined object

that can be used to perform system tasks

System.out.println(“Hello World!”);