0% found this document useful (0 votes)
26 views34 pages

5.2 Obj Behave

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)
26 views34 pages

5.2 Obj Behave

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/ 34

Behavior of

Objects

By Upekha Vandebona
A Class
Class describes what an
object knows and what an
object does.
Song c2 = new Song();
c2.setArtist(“John Denver”);
c2.setTitle(“Country Roads”);

Song p3 = new Song();


p3.setArtitst(“Coldplay”);
p3.setTitle(“Paradise”);

c2.play();
p3.play();
Size > Yes
“Woof!
60 Woof!”
No

Size > Yes


14 “Ruff! Ruff!”

No

“Yip! Yip!”
Exercise

Create an method to
depth
calculate and output
height the volume in the Box
Class

width
Void and Return
Methods can return values. Every
method should be declared with a
return type.
‘void’ return type means they don’t
give anything back.
If you declare a method to
void go(){ int go(){ return a value you must
return a value of the
return 34; declared type! (Or a value
that is compatible with the
} } declared type.)
Exercise

Create an method to
depth
calculate and return
height the volume in the Box
Class

width
Method Parameters and
Arguments
d.bark(3) ;

Parameter is nothing more than a local variable. A


variable with a type and a name that can be used inside the
body of the method.
But here’s the important part: If a method takes a
parameter, you must pass it something when you call
it. And that something must be a value of the appropriate
type.
Multiple Parameters
 Methods can have multiple parameters.
 Separate them with commas when you
declare them, and separate the
arguments with commas when you pass
them.
 If a method has parameters, you must
pass arguments of the right type and
order.
Dog Class bark method
Bark(int numOfBarks, String sound) – Softly, Loudly
Exercise

Create an method to
depth
set dimensions of the
height box, all at once.

width
Pass by Value 3
(pass~by~copy)
1

2
Method Signature
Method Name:
int add(int a, int b) {
Return Type: return a + b;
Parameter List: }

In this example:

int is the return type.


add is the method name.
(int a, int b) is the parameter
list.
Constructors
A constructor does look and feel a lot like a method,
but it’s not a method.
It’s got the code that runs when you say new.
In other words, the code that runs when you
instantiate an object.
The key feature of a constructor is that it runs before
the object can be assigned to a reference.
That means you get a chance to step in and do things
to
get the object ready for use
Using the constructor to
initialize
The best place to put
initialization code is
in the constructor.
And all you need to
do is make a
constructor with
arguments.
Overloading
Constructors
Exercise

Create an constructor
depth
to set dimensions of
height the box, all at once.

width
“this” Keyword

It is illegal in Java to declare two local variables with the same name
inside the same or enclosing scopes.
Interestingly, you can have local variables, including formal parameters
to methods, which overlap with the names of the class’ instance
variables. However, when a local variable has the same name as an
instance variable, the local variable hides the instance variable.
Garbage Collection
Garbage-Collectible Heap.
 Each time an object is created in Java, it goes into
an area of memory known as The Heap.
 All objects—no matter when, where, or how they’re
created—live on the heap.
 But it’s not just any old memory heap; the Java
heap is actually called the Garbage-Collectible
Heap.
 When you create an object, Java allocates memory
space on the heap according to how much that
particular object needs.
Java manages that memory
for you!
 When the JVM can “see” that an object
can never be used again, that object
becomes eligible for garbage collection.
 And if you’re running low on memory,
the Garbage Collector will run, throw out
the unreachable objects, and free up the
space so that the space can be reused.
Thank You!

You might also like