













































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
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
1 / 53
This page cannot be seen from the preview
Don't miss anything!














































1
2
(^) 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
(^) Object Oriented ModelingObject Oriented Modeling
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
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
7
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
(^) 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 )
(^) 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
(^) 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
(^) 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
(^) 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
(^) 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
(^) 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