0% found this document useful (0 votes)
23 views49 pages

Lecture-15,16: by Dr. Bharati Mishra

Uploaded by

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

Lecture-15,16: by Dr. Bharati Mishra

Uploaded by

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

Lecture-15,16

By
Dr. Bharati Mishra
Class
Motivation
• Suppose you want to develop a graphical user
interface as shown below. How do you program it?
Object
• object
• An entity in the real world that can be distinctly
identified.
• A student, a desk, a circle, a button, and even a loan
can all be viewed as objects.
• has a unique identity, state, and behaviors.
• State = a set of data fields (also known as
properties) with their current values.
• Behavior = a set of methods
Class vs. Object
• Classes are constructs that define objects of the
same type
Class Example
UML
UML Notation
• UML class diagram
Constructor
Constructor
• Constructors are a special kind of methods that
are invoked to construct objects.

Circle() Constructor
{ with no
} argument

Circle(double newRadius)
Constructor
{
with argument
radius = newRadius;
}
Constructor
• Constructors must have the same name as the
class itself.
• Constructors do not have a return type—not even
void.
• Constructors are invoked using the new operator
when an object is created.
• Constructors play the role of initializing objects.
Constructor
• Creating objects using constructors

new ClassName();

new Circle();

new Circle(5.0);
Constructor
• A class may be declared without constructors.
• In this case, a no-arg constructor with an empty
body is implicitly declared in the class.
• This constructor, called a default constructor, is
provided automatically only if no constructors are
explicitly declared in the class.
Creating Objects

• Declaring & creating objects in a single step

Circle myCircle = new Circle();

declare create

• Two steps

//step 1: declare
Circle myCircle;
//step 2: create
myCircle = new Circle();
Accessing Data Fields & Methods
Access

• Referencing the object’s data fields:

myCircle.radius

• Invoking the object’s method:

myCircle.getArea()
Data Fields

• The data fields can be of reference types


• Example: name data field is of type String

public class Student {


String name; // name has default value null
int age; // age has default value 0
boolean isScienceMajor; // has default value false
char gender; // c has default value '\u0000‘

//methods …
}
null

• If a data field of a reference type does not


reference any object, the data field holds a
special literal value: null.
Default Values

• The default value of a data field is


• null for a reference type
• 0 for a numeric type
• false for a boolean type
• '\u0000' for a char type
• However, Java assigns no default value to a local
variable inside a method.
Default Values

• 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
String y; // y has no default value
System.out.println("x is " + x);
System.out.println("y is " + y);
}
}

Compilation error: variables


not initialized
Program
• Accessing methods and data fields

TestCircle1 Run

TV

TestTV Run
Copying

• Primitive type copy


Copying

• Reference type copy


Garbage Collector

• As shown in the previous figure, after the


assignment statement c1 = c2, c1 points to the
same object referenced by c2.
• The object previously referenced by c1 is no
longer referenced.
• This object is known as garbage.
• Garbage is automatically collected by JVM.
Garbage Collector

• TIP: 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.
Instance Fields

• Instance fields belong to a specific instance.


• Instance methods are invoked by an instance of
the class.
• i.e. non-static methods
Static

• Static methods are not tied to a specific object.


• Static variables are shared by all the instances of
the class.
• Static constants are final variables shared by all
the instances of the class.
• All declared using static keyword
Static vs. Instance

• Example
Static vs. Instance

• Static fields or methods can be used from instance


or static methods.
• Instance fields or methods can be only used from
instance methods.
• So: a variable or method that does not depend on
a specific instance of the class, should be specified
as static.
Static vs. Instance
public class Foo {
int i = 5;
static int k = 2;

public static void main(String[] args) {


int j= i; // Wrong because i is an instance variable
m1(); // Wrong because m1() is an instance method
}

public void m1() {


i = i + k + m2(i, k);
// Correct: since instance and static variables and
//methods can be used in an instance method
}
public static int m2(int i, int j) {
return (int)(Math.pow(i, j));
}
}
Static vs. Instance
public class Foo {
int i = 5;
static int k = 2;

public static void main(String[] args) {


Foo foo = new Foo();
int j= foo.i; // OK
foo.m1(); // OK
}

public void m1() {


i = i + k + m2(i, k);
}
public static int m2(int i, int j) {
return (int)(Math.pow(i, j));
}
}
Static vs. Instance

• Which one is a better design?


Program

• This example adds a class (i.e. static) variable


numberOfObjects to track the number of Circle
objects created.

Circle2

TestCircle2 Run
Accessibility

1. Package access (default in Java)


• The class, variable, or method can be accessed by
any class in the same package.

2. public
• The class, data, or method is visible to any class in
any package.

3. private
• The data or methods can be accessed only by the
declaring class.
Accessibility Example

• Access C1 methods & fields from C2/C3?

Package 1 Package 2
public class C2 {
public class C1 { public class C3 {
void aMethod() {
public int x; void aMethod() {
C1 o = new C1();
int y; C1 o = new C1();
}
private int z; }
}
}
public void m1() {..}
void m2() {..}
private void m3() {..}
}
Accessibility Example

Package 1 Package 2
public class C2 {
public class C1 { public class C3 {
void aMethod() {
public int x; void aMethod() {
C1 o = new C1();
int y; C1 o = new C1();
//can access o.x;
private int z; //can access o.x;
//can access o.y;
//cannot access
//cannot access
public void m1() {..} //o.y;
//o.z;
void m2() {..} //cannot access
//can invoke
private void m3() {..} //o.z;
//o.m1();
} //can invoke
//can invoke o.m2()
//o.m1();
//cannot invoke
//cannot invoke
//o.m3();
//o.m2();
}
//cannot invoke
}
//o.m3();
}
}
Accessibility Example

• Which classes can access C1?

Package 1 Package 2
public class C2 {
class C1 { public class C3 {
//can access C1?
… //can access C1?
}
} //can access C2?
}
Accessibility Example

• Which classes can access C1?

Package 1 Package 2
public class C2 {
class C1 { public class C3 {
//can access C1
… //cannot access C1
}
} //can access C2
}
Accessibility Summary

• The private modifier restricts access to within a


class
• The default modifier restricts access to within a
package
• The public modifier enables unrestricted access.
Accessibility

• Example:
public class Foo {

private boolean x;

public void test()


{
System.out.println(x);
System.out.println(convert()); OK
}

private int convert(boolean b)


{
return x ? 1 : -1;
}
}
Accessibility

• Example:

public class Test {


public static void main(String[] args)
{
Foo foo = new Foo();
System.out.println(foo.x);
Error!
System.out.println(foo.convert(foo.x));
}
}

• Because x and convert are private members


Why Private?

• To protect data.

• To make class easy to maintain.


get/set

• The get and set methods are used to read and


modify private properties.
• Data encapsulation

The - sign
indicates
private
modifier
Program

• Data encapsulation

Circle3
Run

TestCircle3
Program

• Passing by value for primitive type value (the value


is passed to the parameter)
• Passing by value for reference type value (the
value is the reference to the object)

TestPassObject Run
Array of Objects
Array of Objects

• An array of objects is actually an array of reference


variables.
• So invoking circleArray[1].getArea() involves two
levels of referencing.
• circleArray references to the entire array.
• circleArray[1] references to a Circle object.

Circle[] circleArray = new Circle[10];


Array of Objects

Circle[] circleArray = new Circle[10];


Program

• Summarizing the areas of the circles

TotalArea 49
Run

You might also like