Unit2 Java
Unit2 Java
Object Oriented
Programming Fundamentals
Topics to Be Covered
• Class Fundamentals • Abstract Class and Interfaces
• Inner Class
• Anonymous Classes
Introduction
• We all know that java is pure Object Oriented Programming
Language.
• Class and Object are one of them…
Class Fundamentals
• Class: It is a template that Datatype variable2;
specifies the attributes and //…
behaviour of things or objects. Datatype variable;
return_type
• It is a blueprint or prototype methodname1(parameter list)
from which objects are
{
created.
// body of the method
• A class is the implementation }
of an Abstract Datatype (ADT).
It defines attributes and
return_type
methods which implements the methodnameN(parameter list)
data structure and operation
{
of the ADT, respectively,
// body of the
– Syntax: method
class class_name }
{ }
Datatype variable1;
Class Fundamentals
• A class is declare by using • Methods
keyword “class”. – Syntax:
• The data, or variables, defines Return_type method_name(parameter
list)
within a class are called as an
instance variables. {
// body of the method
• Because each instance of the }
class (that is, each object of
class) contains its own copy of • Here, return type specifies the
these variables. type of data returned by the
method.
• Thus, the data of one object is
separate and unique from the • This can be any valid type.
data of another.
• If method does not return any
• The actual code contains with value, then its return type must
in methods. be void.
• The methods and variables
defined within a class are called
members of a class.
Class Fundamentals
• The name of method specified by method_name.
• This can be any legal identifier other than those already used by
other member within the current scope.
• The parameter list is a sequence of type and identifier pairs
separated by commas.
• Parameters are essentially variables that receive the value of the
arguments passed to the method when it is called.
• It method has no parameters, then the parameter list will be
empty.
Class Fundamentals
• Return value: return(width * height * depth);
– Method that have a return type other }
than void return a value to the calling }
method using the following from the return
statement. • Method call
• Syntax: • Syntax:
– Return value; – Var_name =
– Here, value is the value returned. object_name.method_name(parameter
list);
– Example:
– Example: vol = b1.volume();
– class Box
{ • In above example, b1 is an object and
double width = 1; when volume() is called, it is put on the
right side of an assignment statement.
double height = 2;
double depth = 3; • On the left is a variable, in this case vol,
that will receive the value returned by
volume();
double volume()
{
How to create a class:
• test.java
Class test
{
int x = 5;
}
• Note: File name must be same as your class name.
Create an Object
• test.java
Class test
{
int x = 5;
public static void main(String[] args)
{ This is an object for class test
test t1 = new test();
System.out.println(t1.x);
}
}
Multiple Objects .
(t1 x)
System.out.println(f1.x);
f2.x = 20;
System.out.println(f2.x);
}
• second.java
}
• When both files have been compiled:
• All the instances share the attributes and the behavior of the class.
• But the values of those attributes, i.e. the state are unique for each
object.
• This notifies the compiler that we will use name to refer to data whose type is
type.
• With a primitive variable, this declaration also reserves the proper amount of
memory for the variable.
Class
A template or
Sally blueprint from which
Jim Bob individual objects
are created.
Garbage Collection
• In java, garbage means unreferenced objects.
• Garbage Collection is process of reclaiming the runtime unused
memory automatically.
• In other words, it is a way to destroy the unused objects.
• To do so, we were using free() function in C language and
delete() in C++.
• But, in java it is performed automatically. So, java provides better
memory management.
Garbage Collection
• Advantage of Garbage Collection
– It makes java memory efficient because garbage collector removes the
unreferenced objects from heap memory.
– It is automatically done by the garbage collector(a part of JVM) so we
don't need to make extra efforts.
• By Anonymous object:
Garbage Collection
• finalize() method
– The finalize() method is invoked each time before the object is garbage
collected. This method can be used to perform cleanup processing. This
method is defined in Object class as:
• Note: The Garbage collector of JVM collects only those objects that
are created by new keyword. So if you have created any object
without new, you can use finalize method to perform cleanup
processing (destroying remaining objects).
Garbage Collection
• gc() method
– The gc() method is used to invoke the garbage collector to perform
cleanup processing. The gc() is found in System and Runtime classes.