Object Oriented Methodology - OOM - Part 6, Study notes of Object Oriented Programming

In this document topics covered which are Object Oriented Methodology, Adding Methods to a Class, Class in Java, Instantiating the Object, Understanding References, Methods Are Associated with an Object.

Typology: Study notes

2010/2011

Uploaded on 09/03/2011

krithika
krithika 🇮🇳

4.4

(58)

96 documents

1 / 53

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
Object Oriented Methodology
Object Oriented Methodology
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35

Partial preview of the text

Download Object Oriented Methodology - OOM - Part 6 and more Study notes Object Oriented Programming in PDF only on Docsity!

1

Object Oriented Methodology Object Oriented Methodology

2

OOM OOM

 (^) AboutAbout OOP :OOP :

 (^) Procedure Oriented Vs. Object Oriented

 (^) OOP ConceptsOOP Concepts

 (^) Objects & Classes  (^) Elements of the Object Model  (^) Problem solving in OOP methodology

 (^) Implementation issuesImplementation issues

 (^) OOP implementations: Java, C++ , C# etc.

 (^) Working of a Java Program

  • (^) Compiler-Interpreter & Java Programming  (^) Java Functionality: Servlets & Applets?
  • (^) Platform independent : JVM
  • (^) Console Operation Vs. IDE : JDK Vs. NetBeans  (^) Tools needed: JDK , IDE  (^) Writing Java Class  (^) Adding Methods to a Class

 (^) Object Oriented ModelingObject Oriented Modeling

C++, Java & C # C++, Java & C

Similarities with C++

 (^) – Userdefined classes can be used like builtin types.

 (^) Basic syntax

Differences from C++

 (^) – Methods (member functions) are the only function type

 (^) – Object is the topmost ancestor for all classes

 (^) – All methods use the runtime, not compiletime, types (i.e. all Java methods are like C++ virtual functions)

 (^) – The types of all objects are known at runtime

 (^) – All objects are allocated on the heap (always safe to return objects from methods) Single inheritance only

Comparisons to C#

 (^) – C# very similar to Java in OOP. For details, see

 (^) http://www.harding.edu/fmccown/java1_5_csharp_comparison.html

4

Class Class

 (^) We have to Keep in mind that a class describes

what an object looks like.

 (^) The Employee class is being used to describe

employees of a company in the context of paying them weekly.

 (^) The fields that appear in the Employee class

represent information about an employee that is needed to compute his or her pay.

5

Following class demonstrates methods by adding Following class demonstrates methods by adding

02 methods to the Employee class 02 methods to the Employee class..

public class Employee { { public String name; public String address; public int number; public int SSN; public double salary; public void mailCheck() { System.out.println(“Mailing check to “ + name

  • “\n” + address); } public double computePay() { return salary/52; } }}

7

Class in Java Class in Java

Notice about this Employee class theEmployee class

following:

 (^) The name of the class is EmployeeEmployee.

 (^) The class has five public fields.

 (^) The class has two public methods.

8

Running the Java program Running the Java program

 (^) The Employee class needs to appear in a file

named Employee.java, and the compiled bytecode will appear in a file named Employee.class.

10

C:\javac Employee.java

C:\java Employee

( compilation creates Employee.class )

(Execution on the local JVMJVM )

Instantiating an Object Instantiating an Object

 (^) In Java, the new keyword is used to instantiate an object. The new operator creates the object in memory and returns a reference to the newly created object.

 (^) This object will remain in memory as long as your program retains a reference to the object.

 (^) The following statements declare an Employee reference and use the new keyword to assign the reference to a new Employee object. Employee e; e = new Employee();

 (^) The reference e is pointing to the Employee object in memory.

 (^) The new operator allocates memory for the object and then “zeroes” the memory so that none of the object’s fields will contain garbage.

 (^) Instead, all fields will have an initial value of zero.

11

Understanding References Understanding References

 (^) A reference is (typically) a 32bit integer value that contains

the memory address of the object it refers to.

 (^) I use the term “typically” because the size of a reference is

not strictly defined in the Java Language Specification.

 (^) In the future, references will likely be 64bit integers or

larger.

 (^) Similarly, they can be smaller than 32 bits when used with

operating systems for small electronic devices.

 (^) Because references are essentially integers, you may

wonder why they need to be declared as a particular data type.

 (^) This is because data types are strictly enforced in Java. A

reference must be of a particular class data type

13

References References

 (^) For example, in the following statements, two Employee

references and one String reference are allocated in memory.

Employee e1, e2;

String s;

 (^) Each of these three references consumes the same amount

of memory and is essentially an integer data type.

 (^) However, the references e1 and e2 can refer only to

Employee objects.

 (^) The reference s can refer only to a String object.

14

References References

 (^) However, the compiler knows that String objects and Employee objects are not compatible and will generate compiler errors in the statements above.

 (^) (Other languages such as C++ have similar data type concerns, but they are often not as strictly enforced as they are in Java.).

 (^) We could have declared the reference e and instantiated the Employee object in a single statement: Employee e = new Employee();

 (^) This statement creates two separate elements in memory: thethe reference e and the Employee objectreference e the Employee object.

 (^) The reference e is notnot an object.

 (^) The object itself does not have a variable namedoes not have a variable name, and the only way you can access and use the object is to use a reference to the object.

16

Garbage Collection Garbage Collection

 (^) As we can recall that in our last Ex. For each employee in

the company, we would instantiate an Employee object.

If we have 50 employees50 employees, we need 50 Employee objects50 Employee objects in memory, this would create 50 names, addresses, salaries50 names, addresses, salaries, so on. And for more employees more objects will be created

Each employee would be distinguished by a reference, so we would need 50 references as well.

 (^) We have seen that how the new keyword is used to

instantiate objects.

 (^) However, we have not discussed how to delete an object

after you are finished with it so the memory that the object is consuming can be freed.

17

Garbage Collection Garbage Collection

 (^) The following program instantiates three Employee objects.Study the program and try to determine at which point in the program that each Employee will be eligible for garbage collection.

public class GCDemo

{

public static void main(String [] args)

{

Employee e1, e2, e3;

e1 = new Employee(); //Employee #

e2 = new Employee(); //Employee #

e3 = new Employee(); //Employee #

e2 = e1;

e3 = null;

e1 = null;

}

19

 (^) The GCDemo program creates three references and assigns each to a new Employee object. The new keyword is used three times, so there are three objects in the program.

 (^) The result of assigning e2 to e1 is that employee #2 no longer has a reference to it and can be garbage collected at any point after the statement e2 = e1 after the statement e2 = e1.

 (^) Note that now employee #1 has two referencesemployee #1 has two references pointing to it.

 (^) Assigning e3 to null causes employee #3 to be immediately

eligible for garbage collection because the object can no longer be reached.

 (^) In fact, if we decide that the object should be retrieved for some reason, it is too late. There is no way to relocate the object after all references to it have been lost.

 (^) Assigning e1 to null does not cause employee #1 to be garbage collected because e2 still refers to the object. The reference e goes out of scope at the end of main(), so the employee #1 object can be garbage collected immediately after main() is done executing

20