introduction to objects classes

Upload: rose-marie-arias

Post on 03-Jun-2018

225 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/13/2019 Introduction to Objects Classes

    1/22

    Programming with Objects and Classes

    OO Programming ConceptsDeclaring and Creating Objects

    ConstructorsModifiers ( public , private )

  • 8/13/2019 Introduction to Objects Classes

    2/22

    Object

    represents an entity in the real world thatcan be distinctly identified.

    it has a unique identity, state and behaviors. the state of an object is represented by datafields (also known as properties)

    The behavior of an object is defined by as set ofmethods.

    is an instance of a class

  • 8/13/2019 Introduction to Objects Classes

    3/22

    Class

    is a template or blueprint that defines whatan objects data and methods will be.

    one can create many instances of a class.Creating an instance is referred to asinstantiation.

    is a construct that defines objects of thesame type.

  • 8/13/2019 Introduction to Objects Classes

    4/22

    OO Programming Concepts

    data field 1

    method n

    data field n

    method 1

    An object

    ...

    ...

    State

    Behavior

    Data Fieldradius = 5

    MethodfindArea

    A Circle object

  • 8/13/2019 Introduction to Objects Classes

    5/22

    Class and Objects

    Circle

    radius

    findArea

    circle1: Circle

    radius = 2

    new Circle()

    circlen: Circle

    radius = 5

    new Circle()

    ...

    Graphicalnotation for classes

    Graphicalnotation for ob ects

  • 8/13/2019 Introduction to Objects Classes

    6/22

    Class Declaration

    class Circle{

    double radius = 1.0;

    double findArea(){

    return radius*radius*3.14159;}

    }

  • 8/13/2019 Introduction to Objects Classes

    7/22

    Declaring Objects

    ClassName objectName;

    Example:Circle myCircle;

  • 8/13/2019 Introduction to Objects Classes

    8/22

    Creating Objects

    objectName = new ClassName();

    Example:myCircle = new Circle();

  • 8/13/2019 Introduction to Objects Classes

    9/22

    Declaring/Creating Objects

    in a Single StepClassName objectName = new

    ClassName();

    Example:Circle myCircle = new Circle();

  • 8/13/2019 Introduction to Objects Classes

    10/22

    Differences between variables of primitive Data types and object types

    1

    c: Circle

    radius = 5

    Primitive type int i = 1 i

    Object type Circle c c reference

    Created usingnew Circle(5)

  • 8/13/2019 Introduction to Objects Classes

    11/22

    Copying Variables of PrimitiveData Types and Object Types

    1

    c1: Circle

    radius = 5

    Primitive type assignmenti = j

    Before:

    i

    2 j

    2

    After:

    i

    2 j

    Object type assignmentc1 = c2

    Before:

    c1

    c2

    After:

    c1

    c2

    c2: Circle

    radius = 9

  • 8/13/2019 Introduction to Objects Classes

    12/22

    Accessing Objects

    Referencing the objects data: objectName.data

    myCircle.radius

    Referencing the objects method: objectName.method

    myCircle.findArea()

  • 8/13/2019 Introduction to Objects Classes

    13/22

    Example 1

    Using Objects Objective: Demonstrate creating objects,accessing data, and using methods. Sample: TestCircle.java

  • 8/13/2019 Introduction to Objects Classes

    14/22

    Constructors

    is a special kind of methodare invoked when a new object is created.are designed to perform initializing actions

    such as initializing the data fields ofobjects.are used to construct objects, has exactly the

    same name as the defining class.can be overloaded (multiple constructorswith the same name but different

    signatures)

  • 8/13/2019 Introduction to Objects Classes

    15/22

    Constructors

    Circle(double r){

    radius = r;}

    Circle(){

    radius = 1.0;}

    myCircle = new Circle(5.0);

  • 8/13/2019 Introduction to Objects Classes

    16/22

    Example 2

    Using Constructors Objective: Discuss the role of constructorsand use them to create objects.Sample: TestCircleWithConstructors.java

  • 8/13/2019 Introduction to Objects Classes

    17/22

    TestCircle1.javaCircle1.java

  • 8/13/2019 Introduction to Objects Classes

    18/22

    Passing Objects to Methods

    Passing by reference

    Passing by value

    Example : Passing Objects as Arguments

    TestPassingObject.java

  • 8/13/2019 Introduction to Objects Classes

    19/22

  • 8/13/2019 Introduction to Objects Classes

    20/22

    Example 4Using the private Modifier

    and Accessor Methods In this example, private data are used for the

    radius and the accessor methods getRadius andsetRadius are provided for the clients to retrieve

    and modify the radius.

    TestCircleWithPrivateModifier.java

  • 8/13/2019 Introduction to Objects Classes

    21/22

    Exercise:Write a class named Rectangle to represent rectangles. The

    data fields are height and width declared as private. Youneed to provide the assessor methods for the propertiesand a method findArea() for computing the area of therectangle.

    Write a client program to test the class Rectangle. In theclient program, create two Rectangle objects.

    Assign any widths and heights to the two objects. Display both object's properties and find their areas . Call yoursetter method that would update the width and height ofthe rectangle by 20%.

  • 8/13/2019 Introduction to Objects Classes

    22/22

    Sample Output: