Topic 3 Part 1 Class Object
Topic 3 Part 1 Class Object
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.
For example,
Reference variable :
Also call object references Store address Eg: Shirt myShirt = new Shirt();
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
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.
10
0x34009 0x99f311
colorCode
Stack Memory
Heap Memory
colorCode
Heap Memory
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.
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).
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)
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
To be continued..