Top Banner
Recap (önemli noktaları yinelemek) from last week Paradigm Kay’s Description Intro to Objects Messages / Interconnections Information Hiding Classes Inheritance public class MammalTester { // will work Mammal mam = new Florist(); // will not work Florist flo = new Mammal(); }
32

Recap (önemli noktaları yinelemek) from last week Paradigm Kay’s Description Intro to Objects Messages / Interconnections Information Hiding Classes Inheritance.

Jan 12, 2016

Download

Documents

Ginger Maxwell
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Recap (önemli noktaları yinelemek) from last week Paradigm Kay’s Description Intro to Objects Messages / Interconnections Information Hiding Classes Inheritance.

Recap (önemli noktaları yinelemek) from last week

ParadigmKay’s DescriptionIntro to ObjectsMessages / InterconnectionsInformation Hiding Classes

Inheritance

public class MammalTester {

// will work

Mammal mam = new Florist();

// will not work

Florist flo = new Mammal();

}

Page 2: Recap (önemli noktaları yinelemek) from last week Paradigm Kay’s Description Intro to Objects Messages / Interconnections Information Hiding Classes Inheritance.

The JVM

Page 3: Recap (önemli noktaları yinelemek) from last week Paradigm Kay’s Description Intro to Objects Messages / Interconnections Information Hiding Classes Inheritance.

Key Aspects of OO Programming

Abstraction just the essentials

Composition building from parts

Separation what vs. how

Generalization finding the common elements

Page 4: Recap (önemli noktaları yinelemek) from last week Paradigm Kay’s Description Intro to Objects Messages / Interconnections Information Hiding Classes Inheritance.

Abstraction

"Simplifying to its essentials the description of a real-world entity”

Encapsulation

Page 5: Recap (önemli noktaları yinelemek) from last week Paradigm Kay’s Description Intro to Objects Messages / Interconnections Information Hiding Classes Inheritance.

Properties of Good Abstraction

Well named

Coherent - attributes and behavior that are related and make sense

Accurate - attributes and behavior match the entity being modeled

Minimal - nothing extraneous

Complete - all the attributes and behavior that are necessary

Consistency - Operations should be consistent with each other and with respect to names, arguments, return values, and behavior.

Decoupled - Different abstractions should not be inter-dependent

Page 6: Recap (önemli noktaları yinelemek) from last week Paradigm Kay’s Description Intro to Objects Messages / Interconnections Information Hiding Classes Inheritance.

Classes

“A class is a blueprint, or prototype, that defines the variables and the methods common to all objects of a certain kind." - Java Tutorial

In Java, everything is defined in some class

Page 7: Recap (önemli noktaları yinelemek) from last week Paradigm Kay’s Description Intro to Objects Messages / Interconnections Information Hiding Classes Inheritance.

Classes

Classes are either defined by the Application Program Interface (API) or are programmer defined data types.

Standard Java API classes are organized in packages:

java.lang java.util ...

Page 8: Recap (önemli noktaları yinelemek) from last week Paradigm Kay’s Description Intro to Objects Messages / Interconnections Information Hiding Classes Inheritance.

Classes

There will always be one file for each .class file

At least to start, you will find it easiest if you maintain one .java source file for each class

Page 9: Recap (önemli noktaları yinelemek) from last week Paradigm Kay’s Description Intro to Objects Messages / Interconnections Information Hiding Classes Inheritance.

Anatomy of a Java class

Package ImportsCommentsDeclarationFieldsConstructorsMethods

Page 10: Recap (önemli noktaları yinelemek) from last week Paradigm Kay’s Description Intro to Objects Messages / Interconnections Information Hiding Classes Inheritance.

Methods

A method is a named sequence of instructions - an action you can request an object to take

Methods have a signature: a name and zero or more arguments

Methods declare a data type (primitive or object (or void) they return

Page 11: Recap (önemli noktaları yinelemek) from last week Paradigm Kay’s Description Intro to Objects Messages / Interconnections Information Hiding Classes Inheritance.

Methods

Should generally be fairly short Constructors - a "method" to

initialize objects Constructors and methods can be

over-loaded Constructors and methods can

have accessibility modifiers

Page 12: Recap (önemli noktaları yinelemek) from last week Paradigm Kay’s Description Intro to Objects Messages / Interconnections Information Hiding Classes Inheritance.

ConstructorsConstructors are the mechanism by which new instances of a class are created from the blueprint of the class definition. The purpose of a constructor is to

initialize a new object. Constructors look something like method

definitions except They always have the same name as the class They never return any type of value

Page 13: Recap (önemli noktaları yinelemek) from last week Paradigm Kay’s Description Intro to Objects Messages / Interconnections Information Hiding Classes Inheritance.

Constructors

You "call" a constructor using the new operator and supplying any needed constructor arguments.

Every class has, by default, a constructor:

public ClassName() {}

that takes no arguments and does no special initialization.

Page 14: Recap (önemli noktaları yinelemek) from last week Paradigm Kay’s Description Intro to Objects Messages / Interconnections Information Hiding Classes Inheritance.

Constructors

If you don't define one, Java will create a default, no-arg constructor.

If you define any constructor, with or without arguments, Java assumes you know what you are doing and defines no default constructor.

Page 15: Recap (önemli noktaları yinelemek) from last week Paradigm Kay’s Description Intro to Objects Messages / Interconnections Information Hiding Classes Inheritance.

Constructors: Rules of Thumb

Remember: the purpose of a constructor is to put a newly created object into a known, initial state.

Constructors should not do a lot of processing.

Try to separate object initialization from object behavior.

Page 16: Recap (önemli noktaları yinelemek) from last week Paradigm Kay’s Description Intro to Objects Messages / Interconnections Information Hiding Classes Inheritance.

Objects

An object is a software bundle of variables and related methods." - Java Tutorial

Everything is either a primitive data type or an object

Objects are things: Models of real world, physical things, like Students

Abstract things, like courses, elections, financial transactions

Page 17: Recap (önemli noktaları yinelemek) from last week Paradigm Kay’s Description Intro to Objects Messages / Interconnections Information Hiding Classes Inheritance.

Objects

In Java, declaring a variable to hold a primitive data type reserves space for that primitive data type.

Declaring a variable to hold an object reserves space to hold a reference to an object of that type (or any derived type).

Page 18: Recap (önemli noktaları yinelemek) from last week Paradigm Kay’s Description Intro to Objects Messages / Interconnections Information Hiding Classes Inheritance.

ObjectsDeclaring a variable to hold an object does not create the object

The memory for an object instance is dynamically allocated using the new operator

Create object types liberally

Page 19: Recap (önemli noktaları yinelemek) from last week Paradigm Kay’s Description Intro to Objects Messages / Interconnections Information Hiding Classes Inheritance.

Variable (Fields)

Local Variables Variables you declare and use only within

a method or a smaller block of code.

Instance variables In a good object oriented Java class, just

about all variables ought to be instance (or local) variables.

Each object (instance) will have its own copy of these fields

Page 20: Recap (önemli noktaları yinelemek) from last week Paradigm Kay’s Description Intro to Objects Messages / Interconnections Information Hiding Classes Inheritance.

Variable (Fields)

Class variables Qualifying a variable (field) with the

keyword static makes that variable a class variable:

There is only one of those variables no matter how many instances of the class are instantiated.

All instances share that class variable

Page 21: Recap (önemli noktaları yinelemek) from last week Paradigm Kay’s Description Intro to Objects Messages / Interconnections Information Hiding Classes Inheritance.

Using Class Variables

There are only two good reasons to declare a class variable in Java: As a static final constant As a private static internal item of

information that is purposely shared among object instances

Page 22: Recap (önemli noktaları yinelemek) from last week Paradigm Kay’s Description Intro to Objects Messages / Interconnections Information Hiding Classes Inheritance.

Problem Specification – CS Student

Model enrollment in a CS class: Permit student names to be given on

the command line Assign lab partners as student pairs.

An odd student will have no partner. List the students in the class (and

their lab partner, if any) in reverse order of enrollment.

Page 23: Recap (önemli noktaları yinelemek) from last week Paradigm Kay’s Description Intro to Objects Messages / Interconnections Information Hiding Classes Inheritance.

Classes, Fields, Methods, Objects

Page 24: Recap (önemli noktaları yinelemek) from last week Paradigm Kay’s Description Intro to Objects Messages / Interconnections Information Hiding Classes Inheritance.

Accessibility

The creator of a Java class controls what access objects outside the class have to the implementation (the inner details) of objects of her class by giving variables and methods accessibility qualifiers.

Unlike in C++, each method or field is given an accessibility modifier.

Page 25: Recap (önemli noktaları yinelemek) from last week Paradigm Kay’s Description Intro to Objects Messages / Interconnections Information Hiding Classes Inheritance.

Accessibilitypublic

All outside objects can call public methods. Any outside object can potentially change public

variables.

private methods are only callable within the instance

methods of the class - not by subclasses. variables are only accessible within the methods of

the class - not from subclasses.

Page 26: Recap (önemli noktaları yinelemek) from last week Paradigm Kay’s Description Intro to Objects Messages / Interconnections Information Hiding Classes Inheritance.

Accessibility

protected methods are only callable from the methods of the class and

any sub classes. variables are accessible within the instance methods of the

class and any sub classes.

"Package access” the default if no other modifier is used: Instances of any class in the same package may call

methods with package access. Instances of any class in the same package can acess

package variables

Page 27: Recap (önemli noktaları yinelemek) from last week Paradigm Kay’s Description Intro to Objects Messages / Interconnections Information Hiding Classes Inheritance.

Encapsulation

Information Hiding objects have public methods that

they expose for other objects to use. These methods permit other objects to send "messages" to it. Private variables and methods of the object are implementation details that can be changed at any time without affecting other objects.

Page 28: Recap (önemli noktaları yinelemek) from last week Paradigm Kay’s Description Intro to Objects Messages / Interconnections Information Hiding Classes Inheritance.

Encapsulation

Expose Appropriate Methods Classes should expose to the outside (make

public) just the methods needed to make the object do the things it is designed to do;

None of the internal details related to the implementation of the class should be visible to the outside.

supports modularity - an object can be written and maintained independently of other objects.

Page 29: Recap (önemli noktaları yinelemek) from last week Paradigm Kay’s Description Intro to Objects Messages / Interconnections Information Hiding Classes Inheritance.

Composition

"An organization of components interacting to achieve a coherent, common behavior"

Composition extends the responsibilities of an object by delegating work to additional objects.

Composition is the major mechanism for extending the responsibilities of an object. Nearly every object in an object model is composed of, knows of, or works with other objects.

”Java Design" Peter Coad and Mark Mayfield, Prentice Hall, 1996

Page 30: Recap (önemli noktaları yinelemek) from last week Paradigm Kay’s Description Intro to Objects Messages / Interconnections Information Hiding Classes Inheritance.

Composition

Create object types liberally

The "has-a" relationship

Page 31: Recap (önemli noktaları yinelemek) from last week Paradigm Kay’s Description Intro to Objects Messages / Interconnections Information Hiding Classes Inheritance.

Separation

Distinguishing what an object can do versus how it does it

Related to the use of Java interface definitions

Related to Encapsulation

Page 32: Recap (önemli noktaları yinelemek) from last week Paradigm Kay’s Description Intro to Objects Messages / Interconnections Information Hiding Classes Inheritance.

Generalization

Identifying, and possibly organizing, common elements among different entities

Hierarchy - inheritance Polymorphism Patterns