Sie sind auf Seite 1von 36

OBJECT ORIENTED PROGRAMMING IN JAVA

Understand Objects and Classes

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.

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 method 6. Explain the scope of variables in classes

Introduction
Java is an object-oriented programming language. The concept behind object-oriented programming is implemented in Java using classes 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.

Create Classes in Java Program


class Book { //body of the class Book }

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. Method declarations 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.

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

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( );

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();

To declare, instantiate and initialize an object variable :


1. Declare a reference to the object by specifying its identifier and the type of object 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.

Declare Object Reference Variables


To declare an object reference variable, state the class 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

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 from a class The classname is the class or type of object being created

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 object instance, you can use it to manipulate an objects data or call an objects methods.

Creating Object Reference Variables Using One or Two Lines of Code


One line of code : Shirt myShirt; myShirt = new Shirt(); Two lines of code : Shirt myShirt = new Shirt();

Using an Object Reference Variable to Manipulate Data


To manipulate the value or to invoke the methods of a specific object used (.) dot operator. Eg : myShirt.colorcode=B; myShirt.displayData();

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.

How reference variables are stored in memory??


public static void main(String args[]) { int counter; counter = 10; Shirt myShirt = new Shirt(); }

0x34009 0 0.0 counter myShirt yourShirt ShirtID price

10
0x34009 0x99f311

colorCode

0x34009 ShirtID 0 0.0 U price colorCode

Stack Memory

Heap Memory

Assigning a Reference Fron One Variable To Another


Example : The myShirt 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 is assigned to the item on the left. The line of code in the figure would appear in a method, such as the main method.

Assigning a Reference Fron One Variable To Another


0x34009 0 0.0 10 counter myShirt yourShirt 0x34009 0x99f311 ShirtID price Unreference memory >> garbage collected

colorCode

0x34009 ShirtID 0 0.0 price colorCode

0x99f311 U Stack Memory

Heap Memory

Assigning a Reference Fron One Variable To Another


The address in the reference variable yourShirt (0x99f311), is assigned to the variable myShirt. Both variable now point to the same object, even though the other object. The one that the variable myShirt once pointed to, still exists. Unless another reference variable was pointing to the second Shirt object, the object is garbage collected.

METHOD IN JAVA PROGRAM

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 methods 4. 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.

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 }

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 a method that can be used elsewhere in the program. Methods can return only ONE item (literal value, variable, object reference and so on). If nothing is be returned, the keyword void must be specified as the return type. Method_identifier is the name of the methods

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. The method_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.

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.

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(); } }

In this e.g, an object reference variable call myShirt is declared and initialized to a Shirt object (on line 3 and 4). The myShirt object reference variable then invokes the display method within the Shirt object (line 6).

Calling and Worker Methods


In the previous example, the ShirtTest class call the display method from within another method (main method). Therefore, the main method 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 it does 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.

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 the ShirtTest.java class)

Invoking a Method in the Same Class


Calling a method in the same class is easy, just include the name of the worker method and its arguments, if any.

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)

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.

Object 1 Caller method V1 Value 1 being passed from object 1 to object 2 Walker method

Object 2

1 2 3 9 10..

4 5 6 7 8

V2

Object2 returns value 2 to object 1

To be continued..

Das könnte Ihnen auch gefallen