4. JAVA IntroClass

Embed Size (px)

Citation preview

  • 7/30/2019 4. JAVA IntroClass

    1/28

    Introduction to Class

    Object-Oriented Programming

  • 7/30/2019 4. JAVA IntroClass

    2/28

    Module Introduction

    1. Classes and Objects

    2. Instance variables

    3. Methods

    4. Initializer

    FPT- APTECH 2/28FPT- APTECH 2/28

  • 7/30/2019 4. JAVA IntroClass

    3/28

    #1 Classes and Objects

    Class declaration

    Constructors

    Creating objects

    FPT- APTECH 3/28

  • 7/30/2019 4. JAVA IntroClass

    4/28

    Creating Class

    The template or blueprint from which

    objects are actually made.

    Syntax:

    class ClassName{//definition of class

    }

    Al l cod e that you wr i te in Java is ins ide a

    class.

    Classes can be built by extending other

    classes

    FPT- APTECH 4/28

  • 7/30/2019 4. JAVA IntroClass

    5/28

    Constructor - how to instance an object?

    Same name as the class.

    A class can have more than one constructor.

    A constructor can take zero, one, or more parameters.

    Has no return value.

    Always called with the new operator.

    Use for initializing variables and invoking any methods thatmay be for initialization.

    If a class does not have an explicit constructor, a defaultconstructor (no-argument) is provided.

    FPT- APTECH 5/28

  • 7/30/2019 4. JAVA IntroClass

    6/28

    Name of class

    1. Use Naming rules

    2. Always captain first letter

    3. Use Noun for naming

    Data member

    What data does it need to

    know?Constructor

    Define the way to

    instance object

    1. Same name as the

    class name

    2. Like function in C but

    have no return value

    Methods

    What can it do?

    1. Like function in C

    2. Use Verb for naming

    3. Always lower first letter

    Dissecting the Rectangle class

    FPT- APTECH 6/28

  • 7/30/2019 4. JAVA IntroClass

    7/28

    Creating objects (instance object)

    An object must be created before using in a program.

    1. Declare a variable to store the object reference.

    1. Objects can only be manipulated via references

    2. Creating an object.1. Using the newoperator in conjunction with a call to a constructor

    FPT- APTECH 7/28

  • 7/30/2019 4. JAVA IntroClass

    8/28

    #2 Data Member

    Instance Variables

    Class Variables

    FPT- APTECH 8/28

  • 7/30/2019 4. JAVA IntroClass

    9/28

    Instance variables

    The values of those variables

    define the state of object.

    All instances of a class have

    the same instance variables May have different values

    inside them.

    Has an access modifier

    associated with it.

    FPT- APTECH 9/28

  • 7/30/2019 4. JAVA IntroClass

    10/28

    Example of Instance variables

    recA

    length = 3.0

    width = 4.0

    recB

    length = 6.4

    width = 4.7

    FPT- APTECH 10/28

  • 7/30/2019 4. JAVA IntroClass

    11/28

    Class Variables (Static Variables)

    Variable that is accessed

    without using an object of a

    class.

    Declare using the statickeyword.

    Only one copy of a static

    variable is shared by all the

    objects of the class.

    Change in the value of static

    variable is reflected by all the

    objects of the class.

    FPT- APTECH 11/28

  • 7/30/2019 4. JAVA IntroClass

    12/28

    Example of Static Variables

    FPT- APTECH 12/28

  • 7/30/2019 4. JAVA IntroClass

    13/28

    #3 - Methods

    Definition

    Instance Method

    Calling method and Passing Arguments by Value.

    Calling method and Passing Arguments by Reference.

    Static Methods

    Variable Argument Methods

    FPT- APTECH 13/28

  • 7/30/2019 4. JAVA IntroClass

    14/28

    A method is defined as the actual implementation of an operation

    on an object.

    Syntax:

    access_specifier modifier datatypemethod_name (parameter_list)

    {//body of the method

    }

    The method name should begin with

    a lowercase verb and follow with

    a proper noun.

    Method

    FPT- APTECH 14/28

  • 7/30/2019 4. JAVA IntroClass

    15/28

    Instance Method

    A function defined in a class

    Invoked by an instance object

    and can access instance

    variables

    Provide a mechanism for

    accessing the private data

    stored in an object

    FPT- APTECH 15/28

  • 7/30/2019 4. JAVA IntroClass

    16/28

    Invoking Methods

    Using the '.' operator

    Syntax:

    .

    FPT- APTECH 16/28

  • 7/30/2019 4. JAVA IntroClass

    17/28

    Calling method and Passing Arguments by Value.

    Value from calling method is passed as an argument to the

    called method.

    Any change made to that passed value in the called method

    will not modify the value in the calling method.

    Variables of primitive types (int, float ..) are passed by value.

    FPT- APTECH 17/28

  • 7/30/2019 4. JAVA IntroClass

    18/28

    Example of Calling method and Passing Arguments by

    Value.

    FPT- APTECH 18/28

  • 7/30/2019 4. JAVA IntroClass

    19/28

    Calling method and Passing Arguments by Reference.

    Called method to change the value of the parameter passed

    to if from the calling method.

    When references are passed as parameters, the caller can

    change the values stored but not the reference variables.

    FPT- APTECH 19/28

  • 7/30/2019 4. JAVA IntroClass

    20/28

    Example of Calling method and Passing Arguments by

    Reference

    FPT- APTECH 20/28

  • 7/30/2019 4. JAVA IntroClass

    21/28

    Static Methods

    Methods that do not operate on objects.

    Can be called without creating any objects of the class.

    Call static method, supply the name of the class.

    Declared using the static keyword.

    Can directly refer only to static variables and other static

    methods of the class.

    Cannot refer to non-static methods and variables

    FPT- APTECH 21/28

  • 7/30/2019 4. JAVA IntroClass

    22/28

    Example of Static Methods

    FPT- APTECH 22/28

  • 7/30/2019 4. JAVA IntroClass

    23/28

    static methods usage

    When a method doesn't need to access the object state

    When a method only needs to access static fields of the

    class.

    FPT- APTECH 23/28

  • 7/30/2019 4. JAVA IntroClass

    24/28

    Variable Argument Methods

    Allow calling a method with variable number of arguments.

    FPT- APTECH 24/28

  • 7/30/2019 4. JAVA IntroClass

    25/28

    #4 - Initializer

    Concept

    Object Field Initializer

    FPT- APTECH 25/28

  • 7/30/2019 4. JAVA IntroClass

    26/28

    Concept of Initializer

    Initializers are small pieces of code embedded in curly

    braces that perform initialization:

    class block

    class field

    object block

    object field

    class Test {

    static boolean b;// Automatic

    static double d=1.2; // Explicitstatic {

    System.out.println (Block");

    System.out.println (d);

    }

    }

    FPT- APTECH 26/28

  • 7/30/2019 4. JAVA IntroClass

    27/28

    Object Field Initializer

    Object field initializers

    also known as instance

    initializers

    Are declared inside a

    class, but outside the

    constructor and method

    definitions.

    public class A {//instance field initializerfloat price=9.50F;String str1;String str2;//instance block initializer

    {str1 = AAA;str2 = BBB;

    }public static voidmain(String[] args) { }

    }

    FPT- APTECH 27/28

  • 7/30/2019 4. JAVA IntroClass

    28/28

    Thats about all for today!

    Thank you al l for yo ur attent ion and patient !

    Classes and Objects

    Instance variables

    Methods

    Initializer