Topic 3 Part 1 Class Object

download Topic 3 Part 1 Class Object

of 36

Transcript of Topic 3 Part 1 Class Object

  • 7/30/2019 Topic 3 Part 1 Class Object

    1/36

    OBJECT ORIENTED

    PROGRAMMING IN JAVAUnderstand Objects and Classes

  • 7/30/2019 Topic 3 Part 1 Class Object

    2/36

    COURSE LEARNING OUTCOMES :

    Upon completion of this course, the students

    should be able to:

    1. Describe the features and basic concepts of Java

    language.

    2. Write, compile Java source code and interpret Java byte

    code using Java Development Kit (JDK).

    3. Implement the key of OOP concepts: classes and objects,

    inheritance and polymorphism.

    4. Incorporate exception handling in Java Programming.

  • 7/30/2019 Topic 3 Part 1 Class Object

    3/36

    OBJECTIVES :

    1. Create classes in Java program

    2. Identify built-in classes in Java program

    3. Create an object in Java program

    4. Explain the concepts of accessing objects via reference

    variables

    5. Explain the concepts of accessing objects data and

    method6. Explain the scope of variables in classes

  • 7/30/2019 Topic 3 Part 1 Class Object

    4/36

    Introduction

    Java is an object-oriented programming

    language.

    The concept behind object-oriented

    programming is implemented in Java usingclasses and objects.

    Classes A class is a user-defined data

    type. Class is a container which holds

    variables and methods.

    Objects The Objects are created to

    access the variables and methods from

    classes.

  • 7/30/2019 Topic 3 Part 1 Class Object

    5/36

    Create Classes in Java Program

    class Book

    {

    //body of the class Book

    }

  • 7/30/2019 Topic 3 Part 1 Class Object

    6/36

    Create Classes in Java Program

    Variables and methods are declared in the

    body of class.

    Methods are necessary for operating with

    the data contained in the class. Methoddeclarations have four basic parts:

    The name of the method (methodname).

    The type of the value the method returns(type).

    A list of parameters (parameter-list).

    The body of the method.

  • 7/30/2019 Topic 3 Part 1 Class Object

    7/36

    Build-in classes in Java library

    A Java class library specification is a very

    detailed document outlining the classes in

    the API (Application Programming

    Interface).

    Every API includes documentation

    describing the use of classes, variables and

    methods in the programs.

    To view the Java SE (Standard Edition) API

    specification online, go to the following web

    site :

    http://docs.oracle.com/javase/7/docs/api/in

    dex.html

    http://docs.oracle.com/javase/7/docs/api/index.htmlhttp://docs.oracle.com/javase/7/docs/api/index.htmlhttp://docs.oracle.com/javase/7/docs/api/index.htmlhttp://docs.oracle.com/javase/7/docs/api/index.html
  • 7/30/2019 Topic 3 Part 1 Class Object

    8/36

    Create an Object in Java Program

    Object is an entity that consists of variables

    and methods to manipulate those variables.

    The class members can be accessed through

    objects..

    For example,

    Book b1;

    b1 = new Book( );

  • 7/30/2019 Topic 3 Part 1 Class Object

    9/36

    Create an Object in Java Program

    Primitive Variable :

    Store value

    Eg : int a =7;

    Reference variable : Also call object references

    Store address

    Eg:

    Shirt myShirt = new Shirt();

  • 7/30/2019 Topic 3 Part 1 Class Object

    10/36

    To declare, instantiate and initialize

    an object variable :

    1. Declare a reference to the object by

    specifying its identifier and the type ofobject that the reference points to class

    of the object

    2. Create the object instance using new

    keyword.3. Initialize the object reference variable

    by assigning the object to the object

    reference variable.

  • 7/30/2019 Topic 3 Part 1 Class Object

    11/36

    Declare Object Reference Variables

    To declare an object reference variable, state theclass that you want to create an object from.

    Select the name you want to use to refer to the

    object.

    The syntax is :

    classname identifier;

    Eg :

    Shirt myShirt();

    Where :

    The classname is the class or type of object

    referred to with the object reference

    The identifier is the name you assigned to

    the variable of type classname

  • 7/30/2019 Topic 3 Part 1 Class Object

    12/36

    Instantiating an Object

    After declare the object reference, you can

    create the object that you refer to.

    The syntax for instantiating an object is :

    new classname(); Eg :

    new Shirt();

    Where :

    The new keyword creates an object instance froma class

    The classname is the class or type of object being

    created

  • 7/30/2019 Topic 3 Part 1 Class Object

    13/36

    Initializing Object Reference Variable

    Use sign (=) to initialize the object

    reference.

    The syntax for initializing an object to an

    object reference variable is :identifier = new classname();

    Eg :

    myShirt = new Shirt();

    When you have created a valid objectinstance, you can use it to manipulate an

    objects data or call an objects methods.

  • 7/30/2019 Topic 3 Part 1 Class Object

    14/36

  • 7/30/2019 Topic 3 Part 1 Class Object

    15/36

    Using an Object Reference Variable to

    Manipulate Data To manipulate the value or to invoke themethods of a specific object used (.) dot

    operator.

    Eg :myShirt.colorcode=B;

    myShirt.displayData();

  • 7/30/2019 Topic 3 Part 1 Class Object

    16/36

    Storing Object Reference Variables in

    Memory Primitive Variable : Store value

    Eg : int a =7;

    Reference variable : Also call object references

    Store address/location(memory address)

    Eg:

    Shirt myShirt = new Shirt();

    Memory address is written in hexadecimal

    notatiom (eg : 0x334009)

    The memory address is assigned while a program

    runs.

  • 7/30/2019 Topic 3 Part 1 Class Object

    17/36

    How reference variables are stored in

    memory??public static void main(String args[])

    {

    int counter;

    counter = 10;Shirt myShirt = new Shirt();

    }

  • 7/30/2019 Topic 3 Part 1 Class Object

    18/36

    counter

    myShirt

    yourShirt

    0x34009

    0x99f311

    Stack Memory Heap Memory

    10

    0

    0.0

    U

    0

    0.0

    U

    0x34009

    0x34009

    ShirtID

    price

    colorCode

    ShirtID

    price

    colorCode

  • 7/30/2019 Topic 3 Part 1 Class Object

    19/36

    Assigning a Reference Fron One

    Variable To Another Example : ThemyShirt and yourShirt reference

    variables contain the same address to the

    same object.Shirt myShirt = new Shirt();

    Shirt yourShirt = new Shirt();

    myShirt = yourShirt;

    The value of the item on the right isassigned to the item on the left.

    The line of code in the figure would appear

    in a method, such as the main method.

  • 7/30/2019 Topic 3 Part 1 Class Object

    20/36

    Assigning a Reference Fron One

    Variable To Another

    counter

    myShirt

    yourShirt

    0x34009

    0x99f311

    0x99f311

    Stack Memory Heap Memory

    10

    0

    0.0

    U

    0

    0.0

    U

    0x34009

    0x34009

    ShirtID

    price

    colorCode

    ShirtID

    price

    colorCode

    Unreference

    memory >>garbage collected

  • 7/30/2019 Topic 3 Part 1 Class Object

    21/36

    Assigning a Reference Fron One

    Variable To Another

    The address in the reference variableyourShirt (0x99f311), is assigned to the

    variable myShirt.

    Both variable now point to the same object,even though the other object.

    The one that the variablemyShirt once

    pointed to, still exists.

    Unless another reference variable waspointing to the second Shirt object, the

    object is garbage collected.

  • 7/30/2019 Topic 3 Part 1 Class Object

    22/36

    METHOD IN JAVA PROGRAM

  • 7/30/2019 Topic 3 Part 1 Class Object

    23/36

    Preview Questions

    State TRUE or FALSE

    1. A class can contain ONLY ONE method

    2. The basic form of method must return a

    string

    3. Worker methods can also caller methods4. Main method accepts an array of strings

    5. A methods can only have one parameter

    6. Methods cannot have the same name as the

    class.7. Methods can be declares outisde a class as

    a standalone method.

  • 7/30/2019 Topic 3 Part 1 Class Object

    24/36

    Define Method

    In a class is contained within one or more

    methods.

    The syntax of all method declarations is :

    [modifiers] return_type method_identifier([arguments])

    {

    //method code block

    }

  • 7/30/2019 Topic 3 Part 1 Class Object

    25/36

    Where :

    [modifiers] represent several Java

    technology keywords that modify the way

    methods are used. Modifiers are OPTIONAL

    return_type is the type of value return from amethod that can be used elsewhere in the

    program. Methods can return only ONE item

    (literal value, variable, object reference and soon). If nothing is be returned, the keyword void

    must be specified as the return type. Method_identifier is the name of the

    methods

  • 7/30/2019 Topic 3 Part 1 Class Object

    26/36

    The ([arguments]) represent a list of

    variables whose values are passed to the method

    for use by the method. Arguments are OPTIONAL

    (indicated by the square brackets); many method

    do not accept arguments.

    Themethod_code_block is a sequence of

    statements that the method performs. A wide

    variety of tasks can take place in the code block

    or body of a method.

  • 7/30/2019 Topic 3 Part 1 Class Object

    27/36

    Basic Form of a Method

    The basic form of a method accepts no arguments and

    returns nothing.

    The following display method from Shirt class is a basic

    method :public void displayInformation() {

    System.out.println(Shirt ID : + shirtID);

    System.out.println(Shirt description : +

    description);

    System.out.println(Color Code: + colorCode);System.out.println(Shirt Price: + price);

    System.out.println(Quantity in stock: +quantityInStock);

    } // end of display method

    This method prints several lines of information.

  • 7/30/2019 Topic 3 Part 1 Class Object

    28/36

    Invoking a Method From a Different

    Class

    To invoke/execute a method in a different class,

    use the dot (.) operator with an object reference

    variable to do to access the public variables of

    an object.

    E.g :public class ShirtTest {

    public static void main(String args[]) {

    Shirt myShirt;

    myShirt = new Shirt();

    myShirt.displayInformation();

    }

    }

  • 7/30/2019 Topic 3 Part 1 Class Object

    29/36

    In this e.g, an object reference variable callmyShirt is declared and initialized to aShirt object (on line 3 and 4).

    ThemyShirt object reference variable

    then invokes the display method within theShirt object (line 6).

  • 7/30/2019 Topic 3 Part 1 Class Object

    30/36

    Calling and Worker Methods

    In the previous example, the ShirtTest classcall the display method from within anothermethod (mainmethod).

    Therefore, themainmethod is referred to as the

    calling method because it is invoking or calling

    another method to do some work.

    Conversely, the display method is referred to as

    the worker method because itdoes some work for

    the main method.

    Many methods are both calling and worker

    methods because they not only do some work, but

    call other methods too.

  • 7/30/2019 Topic 3 Part 1 Class Object

    31/36

    When a calling method calls a worker

    method, the calling method stops execution

    until the worker method is done.

    After the worker method has completed,

    program flow is said to return to the point

    after the method invocation in the calling

    method (return to line 6 in theShirtTest.java class)

  • 7/30/2019 Topic 3 Part 1 Class Object

    32/36

    Invoking a Method in the Same Class

    Calling a method in the same class is easy,

    justinclude the name of the worker method

    and its arguments, if any.

  • 7/30/2019 Topic 3 Part 1 Class Object

    33/36

    Guidelines for Invoking Methods

    1. There is no limit to the number of method calls

    that a calling method can make.

    2. The calling method and the worker method can

    be in the same class or in a different class.

    3. The way to invoke the worker method is

    different, depending on whether it is in the same

    class or a different class from the calling method.

    4. You can invoke methods in any order. Methods

    do not need to be completed in the order in

    which they are listed in the class where they are

    declared (the class containing the worker

    methods)

  • 7/30/2019 Topic 3 Part 1 Class Object

    34/36

    Passing Arguments and Returning

    Values

    Methods can be invoked by a calling method

    with a list of arguments (variables or values

    to be used by the worker method).

    Additionally, methods can return a value to

    the calling method that can be used in the

    calling method.

  • 7/30/2019 Topic 3 Part 1 Class Object

    35/36

    1

    2

    3

    910..

    4

    5

    6

    7

    8

    Object 1 Object 2

    V1

    V2

    Caller method Walker method

    Value 1 being

    passed from

    object 1 toobject 2

    Object2

    returns value

    2 to object 1

  • 7/30/2019 Topic 3 Part 1 Class Object

    36/36