CS451 Lecture 2: Introduction to Object Orientation - PowerPoint PPT Presentation

1 / 28
About This Presentation
Title:

CS451 Lecture 2: Introduction to Object Orientation

Description:

... child descendent subclass derived class Inheritance A child class will inherit all non-private attributes and ... int:primitive Java datatype with ... – PowerPoint PPT presentation

Number of Views:54
Avg rating:3.0/5.0
Slides: 29
Provided by: frankm8
Learn more at: https://sse.umkc.edu
Category:

less

Transcript and Presenter's Notes

Title: CS451 Lecture 2: Introduction to Object Orientation


1
CS451Lecture 2 Introduction to Object
Orientation
  • Yugi Lee
  • STB 555
  • (816) 235-5932
  • leeyu_at_umkc.edu
  • www.sice.umkc.edu/leeyu
  • Acknowledgement This lecture is based on
    material courtesy of Monash University

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

3
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!

4
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, whilst hiding the
    implementation
  • Message sending
  • the process by which objects communicate
  • Classes and objects
  • ADT implementations and their runtime equivalents

5
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)

6
Abstract Data Types (ADTs)
  • E.g. intprimitive Java datatype with state
    (i.e., the value it holds), but no internal
    behaviour
  • E.g.

7
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

8
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)

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

10
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

11
Classes, Objects and State
  • 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
  • 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

12
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

13
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

14
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

15
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

16
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)

17
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

18
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)

19
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

20
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

21
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

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

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

24
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

25
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

26
Inheritance
  • class A is the...
  • parent
  • ancestor
  • superclass
  • base class
  • class B is the
  • child
  • descendent
  • subclass
  • derived class

27
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

28
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".
Write a Comment
User Comments (0)
About PowerShow.com