+ All Categories
Home > Documents > Basic Object-Oriented Concepts – towards implementation CS3340.

Basic Object-Oriented Concepts – towards implementation CS3340.

Date post: 17-Jan-2016
Category:
Upload: tobias-allen
View: 233 times
Download: 0 times
Share this document with a friend
Popular Tags:
32
Basic Object- Basic Object- Oriented Oriented Concepts – Concepts – towards towards implementation implementation CS3340
Transcript
Page 1: Basic Object-Oriented Concepts – towards implementation CS3340.

Basic Object-Basic Object-Oriented Concepts Oriented Concepts

– towards – towards implementationimplementation

CS3340

Page 2: Basic Object-Oriented Concepts – towards implementation CS3340.

The goals of OOPThe goals of OOP• Objectives:modular programming.• reusable code/modules.• inheritance.• can define interfaces

2

Page 3: Basic Object-Oriented Concepts – towards implementation CS3340.

OOPOOP

• Abstraction• Encapsulation• Information

Hiding• Polymorphism• Hierarchy

3

• Class• Object• Inheritance• Interface

ConceptualConceptual

ImplementatioImplementatio

nn

Page 4: Basic Object-Oriented Concepts – towards implementation CS3340.

Procedural (like C) vs. Object-Procedural (like C) vs. Object-

Oriented Programming -- Oriented Programming --

HISTORYHISTORY• The unit in procedural programming is function,

and unit in object-oriented programming is class

• Procedural programming concentrates on creating functions, while object-oriented programming starts from isolating the classes, and then look for the methods inside them.

• Procedural programming separates the data of the program from the operations that manipulate the data, while object-oriented programming focus on both of them

figure1: procedural figure2: object-oriented

Page 5: Basic Object-Oriented Concepts – towards implementation CS3340.

Going conception Going conception implementationimplementation

defining terms firstdefining terms first

5

Page 6: Basic Object-Oriented Concepts – towards implementation CS3340.

Some TermsSome Terms

• OOP = object oriented Programming• Class = a template representing a self-contained

element of a computer program (i.e. a module) that represents related information and has the capability to accomplish specific tasks through the invocation of methods (operations). 

6

Page 7: Basic Object-Oriented Concepts – towards implementation CS3340.

An class has behaviorsAn class has behaviors• In old style programming, you had:

o data, which was completely passiveo functions, which could manipulate any data

• An class contains both data (elements) and operations (methods) that manipulate that datao An object is active, not passive; it does thingso An object is responsible for its own data

• But: it can expose that data to other objects

7

Page 8: Basic Object-Oriented Concepts – towards implementation CS3340.

Class: data and Class: data and

operations/methodsoperations/methods

• An object contains both data and methods that manipulate that datao The data represent the state of the objecto Data can also describe the relationships between this object and other

objects

• Example: A CheckingAccount might haveo A balance (the internal state of the account)o An owner (some object representing a person)

• Example: A Dog might haveo A breedo A nameo An owner

8

Page 9: Basic Object-Oriented Concepts – towards implementation CS3340.

Object: An “instance” of a Object: An “instance” of a classclass

• An instance will have a state representing values of the data.

• Example: The Dogs in the image –each are different.

9

Instance is another wordfor object…they are synonyms

Page 10: Basic Object-Oriented Concepts – towards implementation CS3340.

Example: A “Rabbit” Example: A “Rabbit” classclass

• You could (in a game, for example) create a class representing a rabbit

• It would have data:o How hungry it iso How frightened it iso Where it is

• And methods:o eat, hide, run, dig

• Now create an instance/object of our Rabbit class to representBugsBunny

10

Page 11: Basic Object-Oriented Concepts – towards implementation CS3340.

Wait – are you Wait – are you confued? Class and confued? Class and

ObjectObject• “Class” refers to a blueprint. It defines the

variables and methods the objects support

• “Object” is an instance of a class. Each object has a class which defines its data and behavior

Page 12: Basic Object-Oriented Concepts – towards implementation CS3340.

Concept: Classes are Concept: Classes are like Abstract Data like Abstract Data

TypesTypes• An Abstract Data Type (ADT) bundles together:

o some data, representing an object or "thing"o the operations on that data

• The operations defined by the ADT are the only operations permitted on its data

• Example: a CheckingAccount, with operations deposit, withdraw, getBalance, etc.

• Classes enforce this bundling togethero If all data values are private, a class can also enforce the rule that its

defined operations are the only ones permitted on the data

12

This slide relates Class to OOP concepts

Page 13: Basic Object-Oriented Concepts – towards implementation CS3340.

Special note: in the following Special note: in the following slides I will discuss points and slides I will discuss points and show you EARLY some java –show you EARLY some java –

don’t worry if you don’t don’t worry if you don’t understand now ---you will learn understand now ---you will learn

java in this classjava in this class

13

Page 14: Basic Object-Oriented Concepts – towards implementation CS3340.

Example of a classExample of a class

14

class Employee { // Fields private String name; //Can get but not change private double salary; // Cannot get or set // Constructor Employee(String n, double s) { name = n; salary = s; } // Methods void pay () { System.out.println("Pay to the order of " + name + " $" + salary); } public String getName() { return name; } // getter}

Don’t worry I will teach you java

Page 15: Basic Object-Oriented Concepts – towards implementation CS3340.

Objects must be Objects must be createdcreated

• int n; does two things:o It declares that n is an integer variableo It allocates space to hold a value for no For a primitive, this is all that is needed

• Employee secretary; also does two thingso It declares that secretary is type Employeeo It allocates space to hold a reference to an Employeeo For an object, this is not all that is needed

• secretary = new Employee ( );o This allocate space to hold a value for the Employeeo Until you do this, the Employee is null

15

Don’t worry I will teach you java

Page 16: Basic Object-Oriented Concepts – towards implementation CS3340.

Class MembersClass Members• A class can have three kinds of

members:

o fields/data/variables: data variables which determine the status of the class or an object

o methods: executable code of the class built from statements. It allows us to manipulate/change the status of an object or access the value of the data member

o nested classes and nested interfacesWe will learn later about nested classes—basically is a class defined in a class

Page 17: Basic Object-Oriented Concepts – towards implementation CS3340.

InheritanceInheritance

• a mechanism that enables one class to inherit all the behaviors and attributes of another class.

• subclass = a class that inherits from another class. 

• superclass = a class that is its inheritance to another class.

17

Page 18: Basic Object-Oriented Concepts – towards implementation CS3340.

More on inheritanceMore on inheritance• Note: Subclasses can override methods they

have inherited if they want to change the corresponding behavior. For example, the method to calculate the Expected Lifespan for each Class of Dog, Cat, and Horse may be different from their superclass Four-Legged Animal. 

18

Page 19: Basic Object-Oriented Concepts – towards implementation CS3340.

Inheritance: Classes form a Inheritance: Classes form a

hierarchyhierarchy

• Classes are arranged in a treelike structure called a hierarchy

• Every class may have one or more subclasses

19

Page 20: Basic Object-Oriented Concepts – towards implementation CS3340.

Another Example of a hierarchy—Another Example of a hierarchy—

this one dealing with windowsthis one dealing with windows

20

A FileDialog is a Dialog is a Window is a Container

Container

Panel ScrollPane

Window

Dialog

Frame

FileDialog

Page 21: Basic Object-Oriented Concepts – towards implementation CS3340.

Example of Example of inheritanceinheritance

21

class Person { String name; int age; void birthday () { age = age + 1; }}

class Employee extends Person { double salary; void pay () { ...}}

Every Employee has name and age fields and birthday method as well as a salary field and a pay method.

Don’t worry I will teach you java

Page 22: Basic Object-Oriented Concepts – towards implementation CS3340.

How to declare and How to declare and create objectscreate objects

Employee secretary; // declares secretary secretary = new Employee (); // allocates space Employee secretary = new Employee(); // does both• But the secretary is still "blank" (null) secretary.name = "Adele"; // dot notation secretary.birthday (); // sends a message

22

Don’t worry I will teach you java

Page 23: Basic Object-Oriented Concepts – towards implementation CS3340.

How to reference a How to reference a field or methodfield or method

• Inside a class, no dots are necessary class Person { ... age = age + 1; ...}

• Outside a class, you need to say which object you are talking to if (john.age < 75) john.birthday ();

• If you don't have an object, you cannot use its fields or methods!

23

Don’t worry I will teach you java

Page 24: Basic Object-Oriented Concepts – towards implementation CS3340.

Concept: Concept: thisthis object – object – identifying one’s selfidentifying one’s self

• Inside a class, no dots are necessary, becauseo you are working on this object

• If you wish, you can make it explicit: class Person { ... this.age = this.age + 1; ...}

• this is like an extra parameter to the method• You usually don't need to use this

24

Don’t worry I will teach you java

Page 25: Basic Object-Oriented Concepts – towards implementation CS3340.

Concept: Methods can be Concept: Methods can be

overridden – overridden – a form of a form of

polymorphismpolymorphism

• So birds can fly. Except penguins.

25

class Bird extends Animal { void fly (String destination) { location = destination; }}

class Penguin extends Bird { void fly (String whatever) { }}

Don’t worry I will teach you java

Page 26: Basic Object-Oriented Concepts – towards implementation CS3340.

Interacting with Ojbects Interacting with Ojbects

send messagessend messages Bird someBird = pingu; someBird.fly ("South America");

• Did pingu actually go anywhere?o You sent the message fly(...) to pinguo If pingu is a penguin, he ignored ito Otherwise he used the method defined in Bird

• You did not directly call any methodo You cannot tell, without studying the program, which method actually

gets usedo The same statement may result in different methods being used at

different times

26Don’t worry I will teach you java

Page 27: Basic Object-Oriented Concepts – towards implementation CS3340.

Some things to think Some things to think about …we will revisitabout …we will revisit

27

Page 28: Basic Object-Oriented Concepts – towards implementation CS3340.

Advice: Restrict accessAdvice: Restrict access• Always, always strive for a narrow interface• Follow the principle of information hiding:

o the caller should know as little as possible about how the method does its job

o the method should know little or nothing about where or why it is being called

• Make as much as possible private• Your class is responsible for it’s own data; don’t

allow other classes to screw it up!

28

Page 29: Basic Object-Oriented Concepts – towards implementation CS3340.

Advice: Use setters Advice: Use setters and gettersand getters

• This way the object maintains control• Setters and getters have conventional names:

setDataName, getDataName, isDataName (booleans only)

29

class Employee extends Person { private double salary; private boolean male; public void setSalary (double newSalary) { salary = newSalary; } public double getSalary () { return salary; } public boolean isMale() { return male; }}

Page 30: Basic Object-Oriented Concepts – towards implementation CS3340.

Kinds of accessKinds of access• Java provides four levels of access:

o public: available everywhereo protected: available within the package (in the same subdirectory) and

to all subclasseso [default]: available within the packageo private: only available within the class itself

• The default is called package visibility• In small programs this isn't important...right?

30

Page 31: Basic Object-Oriented Concepts – towards implementation CS3340.

Now…Now…

31

Page 32: Basic Object-Oriented Concepts – towards implementation CS3340.

From Budd Book on From Budd Book on OOPOOP

1. Everything is an Object (in pure OOP)

2. Computation is done via Objects Communicating with each other by requesting objects to perform actions.

At some point at least a few object need to perform some work besides passing on requests to other objects/agents. 3. Each Object has its own memory (and in pure OOP consists of other objects) 

4. Every object is an instance of a class.

5. A Class is a repository for behavior associated with an object and the kind of information stored in an instances memory.6. Classes are organized into a single rooted tree structure. Memory and behavior of an instance of a class is automatically available (inheritance) to any class associated with a descendant. 32


Recommended