Lecture 4 OOP(OOP in Java)
Lecture 4 OOP(OOP in Java)
• Clear use of
• Variables
• Methods
• Objects
• Nouns, things in the world
• Constructor
• Given a Class, the way to create an Object (that
is, an Instance of the Class) and initialize it
Object
• Attributes Anything we can put a
thumb on
• Properties an object has
• Methods
• Actions that an object can do
Defining Classes
The Structure of Classes
class name {
instance variables
declarations and symbolic constants
how to manipulate
method those objects (may or
definitions may not include its
own “driver”, i.e.,
} These parts of a class can main( ))
actually be in any order
Defining a Class Comparison with C++
• Java gives you the ability to write classes or user-defined data
types similar to the way C++ does, with a few differences
how to manipulate
public void Display
those objects (may or
(……) {
may not include its
………. own “driver”, i.e.,
} main( ))
Defining a Class Comparison with C++ (cont)
• Points to consider when defining a class (cont)
• Object References
• With null
• Remember, the same rule is not applied to local variables. Using a
local variable without initialization is a compile time error.
• Access Modifiers
• public : Accessible anywhere by anyone
• Private : Only accessible within this class
• Protected : Accessible only to the class itself and to it’s subclasses or other
classes in the same “package”
• Package : Default access if no access modifier is provided.
Accessible to all classes in the same package
• Constructor
• Same name as class name
• Does not have a return type
• No initialization list
• JVM provides a zero-argument constructor only if a class
doesn’t define it’s own constructor
• Destructor
• Destructors are not required in a java class
Example
Task - Defining a Class
• Create a class for Student Student
• should be able to store the following
characteristics of student Attributes:
Roll NO
• Roll No Name
• Name Methods:
constructors
• Provide default, parameterized and copy getters/setters
constructors print
// Standard Getters
public Student() {
name = “not set”;
rollNo = 100;
}
} // end of class
Using Classes
Defining a Class Comparison with C++
• Objects of a class are always created on heap using the “new”
operator followed by constructor
s1.print();
s2.print();
s2.setName("usman");
s2.setRollNo(20);
//continue….
Student Client Code
System.out.println("calling copy constructor");
Student s3 = new Student(s2); //call to copy constructor
s2.print();
s3.print();
s3.print();
} //end of main
Compile and Execute
More on Classes
Static
• A class can have static
• Variables
• Methods
Class: Student
countStudents: 2
Method: getCountStudents()
Object: usman
Type: Student
Name: usman shahid
Roll No: 5
Methods: getName, setName
getRollNo, setRollNo
toString
Garbage Collection
• Java performs garbage collection
and eliminates the need to free
objects explicitly.
• System.gc()
• Request the JVM to run the garbage collector
• Not necessary it will run
Memory Management
Stack Heap
public class Test{
0F59
public static void main|(String args[]){ s1
name
ali
0F59
Student s1 = new Student(“ali”);
Student s2 = new Student(“raza”);
03D2
s1= s2; s2
name
raza
03D2
}
…….
Modify Student Class
// Constructor that uses a default value instead of taking an argument.
public Student() {
name = “not set”;
rollNo = 100;
countStudents += 1;
}
int numObjs;
numObjs = Student.getCountStudents();
System.out.println("Students Objects:"+numObjs);
numObjs = Student.getCountStudents();
Student Client Code
Student s2 = new Student("usman", 49);
System.out.println("Student:" +s2); //implicit call to toString()
numObjs = Student.getCountStudents();
System.out.println("Students Objects:"+numObjs);
s1 = null;
numObjs = Student.getCountStudents();
System.out.println("Students Objects:"+numObjs);
} //end of main
Compile and Execute
More on Java OOP
• http://www.codeguru.com/java/tij/tij_c.shtml