More On Module2
More On Module2
More On Module2
Operation
Operation
Example: StudentObject
Enroll()
st_name
st_id
Performance() Displayinfo()
branch
semester
Result()
Class
• Syntax:
class class_name
{
Data members
}
Method Members
Class
class Student
{
String st_id; Data Members or Properties of
String st_name; Student Class
void read_data() { }
void print_data() { }
Method Members or
} Behaviors of Student Class
DECLARING OBJECTS
Creating of an Object
• Syntax:
classname identifier = new classname( );
• Example:
Student Obj = new Student();
DECLARING OBJECTS
Student Obj;
Obj
st_id, st_name
Obj read_data()
print_data()
DECLARING OBJECTS
02, Ravi
Obj2 read_data()
print_data()
Assigning Object Reference Variables
ex1 int a, b;
void getInfo();
void dispInfo();
ex2
Template for Example class
Note:
Reference variable will not create duplicate template
Assigning object reference variables
Example ex1 = new Example();
Example ex2 = ex1;
Introducing Methods
General form of a method:
returntype name(parameter-list)
{
// body of method
}
Example:
void read_data()
{
// body of method
}
Accessing Class Members
• Classinstance.classDatamember;
• Example
sobj.st_id;
• Classinstance.classMethodmember;
• Example
sobj.read_data();
Introducing Methods
• Constructors are the special methods of the class that shares the same
name of the class.
• Constructors are little strange because they have no return type, not even
void.
• The implicit return type of a class’ constructor is the class type itself.
• It is the constructor’s job to initialize the internal state of an object so that
the code
CONSTRUCTORS
• Default constructors
• Parameterized constructors
CREATING & DESTROYING OBJECTS
Creating of an Object
• Syntax:
classname identifier = new classname( );
Example Obj = new Example();
Destroying of an Object
• Obj.finalize()
• Automatic garbage collector
protected void finalize( )
{
//stmts
}
Garbage Collection
• The garbage collector runs periodically, checking for objects that are
no longer referenced by any running state or indirectly through
other referenced objects.
The finalize( ) Method
General Syntax:
protected void finalize( )
{
// finalization code here
}
Note:
• The need for destructor functions is minimal because of Java’s
garbage collection subsystem.
Polymorphism
• Overloading methods
• Overloading constructors
• No operators overloading
INTRODUCING ACCESS CONTROL
• Default: public
• private
• public
• protected
CLASS
class Student
{
String st_id;
String st_name;
void read_data() public / default
{ } visibility
void print_data()
{ }
}
CLASS
class Student
{
public String st_id;
public String st_name;
public double result; Explicit
private String mob; visibilities
public void read_data() { }
private void print_data() { }
}
Understanding static
1. Creation of Static data members
• These are global variables, visible to all of its instances
• To create a class member that is independent of any of its class instance
• It can be accessed before any objects of its class are created
• It can be accessed without reference to any object
• Both class methods and variables can be static
2. Creation of static member methods
• Methods declared as static have restrictions:
- Call only access other static methods and data
- Cannot refer to this or super in any way
Note: Outside the class, static methods and variables are used independently with out any
object of the class
Syntax:
classname.method( ) or classname.variable
3. Creation of Static Block
• it is a block that gets executed when the class is first loaded/or before any other objects
are created for that class
• Initialization of static variables in static block
• It gets executed exactly once
• Syntax: static { ______ }
Contd…
4. use of static import statement
• When using static import, it is possible to refer to static members directly by their
names, without having to qualify them with the name of their class.
• Use static import to bring sqrt() into view.
• import static java.lang.Math.sqrt;
• for eg: a=sqrt(4) instead of a=Math.sqrt(4)
General form is
1. import static pkg.type-name.static-member-name ;
- Here, type-name is the name of a class or interface that contains the desired
static member.
- Its full package name is specified by pkg.
- The name of the member is specified by static member-name.
2. The second form of static import imports all static members of a given class or
interface. Its general form is shown here:
- import static pkg.type-name.*;
Introducing final
• I-D Array
• 2-D Array
• Irregular Array
• Assigning Array references
• Ar1 = Ar2;
• Using the length member
• Ar.length
2-D Array