0% found this document useful (0 votes)
170 views26 pages

W3. Advanced OOP Concepts

This document provides an overview of advanced object-oriented programming concepts in Java, including: 1) Reference variables refer to objects stored in heap memory, while primitive types are stored on the stack. 2) The difference between reference variables and objects - a reference variable contains the memory address of an object. 3) Common Java utility classes like Date, Random, and String that can generate random numbers, format dates, and handle strings. 4) Instance and static variables/methods - instance members are associated with objects while static members can be accessed without creating an object.

Uploaded by

Mariam Asif
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
170 views26 pages

W3. Advanced OOP Concepts

This document provides an overview of advanced object-oriented programming concepts in Java, including: 1) Reference variables refer to objects stored in heap memory, while primitive types are stored on the stack. 2) The difference between reference variables and objects - a reference variable contains the memory address of an object. 3) Common Java utility classes like Date, Random, and String that can generate random numbers, format dates, and handle strings. 4) Instance and static variables/methods - instance members are associated with objects while static members can be accessed without creating an object.

Uploaded by

Mariam Asif
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 26

Object Oriented Programming

Advanced OOP Concepts 


Prepared By
Mehak Usmani & Sophia Ajaz

1
Objectives
• To understand the concept of reference variable and reference type
• To understand the difference between reference variable and object
• To understand the difference of heap and stack
• To use some utility classes provided by Java
• To understand the different type of class methods and variables.

2
Reference Variables and Reference Types
• Simply, reference variable is an object and reference type is a class.
• Objects are accessed via object’s reference variables, which contain references to the objects.
• A class is a reference type, which means that a variable of the class type can reference an instance of
the class.
• The following statement declares the variable myCircle to be of the Circle type:
Circle myCircle;
• The next statement creates an object and assigns its reference to myCircle:
myCircle = new Circle();
• a single statement can combines the declaration of reference variable, the creation of an object, and
the assigning of an object reference as;
Circle myCircle = new Circle();

3
Heap & Stack Memory
• Java Heap space is used by java runtime to allocate memory to Objects and JRE classes.
• Objects are created in heap memory but method frames are stored in Stack memory.
• Garbage Collection runs on the heap memory.
• Stack contains method specific values and references that are short-lived. Stack implements LIFO.
• Heap memory is shared by all threads of Java application but Stack memory is local to each thread.
• Size of heap space is much bigger than the small size of Stack in Java

4
Heap & Stack Memory

5
Reference Variables

6
Object or Reference variable
• An object reference variable that appears to hold an object actually contains a reference to that
object.
• Strictly speaking, an object reference variable and an object are different, but most of the time the
distinction can be ignored.
• Therefore, for simplicity, myCircle can be called a Circle object rather than use the longer-winded
description that myCircle is a variable that contains a reference to a Circle object.
• When you assign one object reference variable to another object reference variable, you are not
creating a copy of the object, you are only making a copy of the reference.

7
Object or Reference variable
• Object reference variables act differently when an assignment takes place

• Is b2 being assigned a reference to a copy of the object referred to by b1?


No!
• If b1 has been set to null, b2 will still points to the original object.

8
Variables of Primitive Types and Reference Types
• Every variable represents a memory location that holds a value.
• Declaring a variable is to tell the compiler what type of value the variable can hold.
• For a variable of a primitive type, the value is of the primitive type.
• For a variable of a reference type, the value is a reference to where an object is located.

9
Variables of Primitive Types and Reference Types
• When you assign one variable to another, the other variable is set to the same value.
• For a variable of a primitive type, the real value of one variable is assigned to the other variable.
• For a variable of a reference type, the reference of one variable is assigned to the other variable.

10
Garbage collection
• The object previously referenced by c1 is no longer useful and therefore is now known as garbage.
• Garbage occupies memory space, so the Java runtime system detects garbage and automatically
reclaims the space it occupies. This process is called garbage collection.
• If you know that an object is no longer needed, you can explicitly assign null to a reference variable
for the object. The JVM will automatically collect the space if the object is not referenced by any
variable.

11
null
• If a data field of a reference type does not reference any object, the data field holds a special Java value,
null.

• null is a literal just like true and false. While true and false are Boolean literals, null is a literal for a
reference type.
• The default value of a data field is null for a reference type.
• Java assigns no default value to a local variable inside a method.

public class Test {


public static void main(String[] args) {
int x; // x has no default value
System.out.println("x is " + x); // error
}
}

12
NullPointerException
• NullPointerException is a common runtime error.
• It occurs when you invoke a method on a reference variable with a null value.
• Make sure you assign an object reference to the variable before invoking the method through the
reference variable.

13
What is wrong with each of the following programs?

14
Using Classes from the Java Library
• The Java API contains a rich set of classes for developing Java programs.
• The Object class is the parent class of all the classes in java by default. It is the topmost class of java.
• Different classes are frequently used by Java developers, some of which are;
– The Date Class
– The Random Class
– The String Class
– Boolean
– Integer

15
The Date Class
• Java provides a system-independent encapsulation of date and time in the java.util.Date class.
• The no-arg constructor of the Date class can be used to create an instance for the current date
and time.

Date date = new Date();

• The getTime() method of Date class returns the elapsed time since January 1, 1970, GMT, and
the toString() method return the date and time as a string.
• The Date class has another constructor, Date(long elapseTime), which can be used to construct
a Date object.

16
The Date Class

Output:

17
The Random Class
• Just like random function of Math class, there is another class called Random Class that can be used
to generate random numbers.
• The class can generate a random int, long, double, float, and boolean value.
• The random object can be created with no-arg as well as parameterized constructor.
• The no-arg constructor creates a Random object using the current elapsed time as its seed.
• Random object can be created by specifying a seed .
• A seed is a number used to initialize a random number generator.
• If two Random objects have the same seed, they will generate identical sequences of numbers.

18
The Random Class

Output:

19
Instance Variables and Methods
• The data field in the class is known as an instance variable.
• The method defines in the class are instance method.
• Instance variable or Instance method are dependent on a specific instance.
• Instance variable or instance method are non-static that means it cannot be access with class name
only.
• It needs an object or instance to get accessed or invoked.
• An instance variable is tied to a specific instance of the class; it is not shared among objects of the
same class.

20
static Variables and Methods
• A static variable is shared by all objects of the class.
static int radius;
• Static variables (also called class variable) store values for the variables in a common memory
location. Because of this common location, if one object changes the value of a static variable, all
objects of the same class are affected.
• Static methods can be called without creating an instance of the class.
static double getArea();
• A static method cannot access instance members of the class.
• Static members can be directly invoked by the class.
Example: Math.pow(3, 2.5).
• To declare a static variable or define a static method, put the modifier static in declaration.

21
UML Notation
• Instance variables belong to the instances and have memory storage independent of one another.
Static variables are shared by all the instances of the same class.
• static variables or methods are underlined in the UML diagram.

22
static Constants
• Constants in a class are shared by all objects of the class.
• Constants should be declared as final static.
• For example:
The constant PI in the Math class is defined as:
static final double PI = 3.14159265358979323846;

• If any variable is declared as final, the value of final variable cannot be changed. It will be a constant.
• Changing the value of such variable will give compile time error.

23
Instance vs. static
• An instance method can invoke an instance or static method and access an instance or
static data field.
• A static method can invoke a static method and access a static data field. However, a
static method cannot invoke an instance method or access an instance data field, since
static methods and static data fields don’t belong to a particular object.
• The relationship between static and instance members is summarized in the following
diagram:

24
Representing in Class diagram
Circle
Attributes are usually listed in the form:
attributeName : Type

• Static attributes.
- noOfObjects : int attributeName : Type

• Static methods are underlined


+ getTotalObjects(): int visibility name (parameters) : return_type

Object Oriented Programming


Let’s write a class with static members
• Use Class Name to invoke a static method and to access a static variable. This improves readability,
because other programmers can easily recognize the static method and data in the class.

Static Members
Program

26

You might also like