Recap ( - PowerPoint PPT Presentation

1 / 32
About This Presentation
Title:

Recap (

Description:

Create object types liberally. Variable (Fields) Local Variables ... Create object types liberally. The 'has-a' relationship. Separation ... – PowerPoint PPT presentation

Number of Views:17
Avg rating:3.0/5.0
Slides: 33
Provided by: jno9
Category:
Tags: liberally | recap

less

Transcript and Presenter's Notes

Title: Recap (


1
Recap (önemli noktalari yinelemek) from last week
  • Paradigm
  • Kays 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()

2
The JVM
3
Key Aspects of OO Programming
  • Abstraction
  • just the essentials
  • Composition
  • building from parts
  • Separation
  • what vs. how
  • Generalization
  • finding the common elements

4
Abstraction
  • "Simplifying to its essentials the description of
    a real-world entity
  • Encapsulation

5
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

6
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

7
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
  • ...

8
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

9
Anatomy of a Java class
  • Package
  • Imports
  • Comments
  • Declaration
  • Fields
  • Constructors
  • Methods

10
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

11
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

12
Constructors
  • Constructors 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

13
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.

14
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.

15
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.

16
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

17
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).

18
Objects
  • Declaring 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

19
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

20
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

21
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

22
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.

23
Classes, Fields, Methods, Objects
24
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.

25
Accessibility
  • public
  • 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.

26
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

27
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.

28
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.

29
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

30
Composition
  • Create object types liberally
  • The "has-a" relationship

31
Separation
  • Distinguishing what an object can do versus how
    it does it
  • Related to the use of Java interface definitions
  • Related to Encapsulation

32
Generalization
  • Identifying, and possibly organizing, common
    elements among different entities
  • Hierarchy - inheritance
  • Polymorphism
  • Patterns
Write a Comment
User Comments (0)
About PowerShow.com