More On Module2

Download as pdf or txt
Download as pdf or txt
You are on page 1of 35

Class

• Class is a collection of similar


objects.
Objects
• OOP uses objects as its fundamental building blocks.
• Objects are the basic run-time entities in an object-oriented system.
• Object is a real world existing entity.
• Object is an Instance of a particular class.
• Every object is associated with data and functions which define
meaningful operations on that object.
Object

Operation

Operation Attributes 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

Obj = new Student();

st_id, st_name
Obj read_data()
print_data()
DECLARING OBJECTS

Student Obj1= new Student();


01, Raju
read_data()
Obj1 print_data()
Student Obj2= new Student();

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

• Method without parameters & without return value


• Method with parameters & without return value
• Method with parameters & return value
• Method with objects as parameters
• Method with returning an object
• ‘this’ keyword
CONSTRUCTORS

• A constructor is a special method of the class, having same name of


the class and no return data type.
• Used to initialize an object immediately upon creation.
• It is syntactically similar to a method.
• It is automatically called when the object is created, before the new
operator completes
CONSTRUCTORS

• 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

• Finalization – a java mechanism to define specific actions to be taken


before an object is just about to be reclaimed by the garbage
collector.

• 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

• Prevents its contents from being modified.


• Initialize a final variable when it is declared.
• final is similar to const in C/C++
• Ex:
final int FILE_NEW = 1;
final int FILE_OPEN = 2;
Arrays Revisited

• I-D Array
• 2-D Array
• Irregular Array
• Assigning Array references
• Ar1 = Ar2;
• Using the length member
• Ar.length
2-D Array

• int a[ ][ ] = new int[4][5];


2-D Array
Irregular Array

• int a[ ][ ] = new int[4][5];


a[0] = new int [1];
a[1] = new int [2];
a[2] = new int [3];
a[3] = new int [4];
Introducing Nested & Inner Classes
• Nested / Inner class is a class with • Accessing inner class members:
in another class Outside the class:
• Example: obj.ibj.d;
class Employee
{ Within class:
String empName; ibj.d;
String empID;
class DOB • Visibility of inner class members
{
int dd, mm, yy; • Private members of outer class
} are accessible to inner class
void readEmp( ); • Inner class object can be a
void salCompute( ); member of outer class.
}
Exploring the String class

• String – a predefined class


• String str ;
str is an object of the class String
• str = “ Welcome” + “ All” ;
• Equivalent stmts:
System.out.println(str);
System.out.println(“Welcome All”);
Exploring the String class

• “String are immutable”


• Once a String object is created, its contents cannot be altered
• To change a string, always create a new one with modifications
• Java defines peer classes of String, called StringBuffer, StringBuilder
and StringTokenizer which allow strings to be altered
String class - methods
• String concat(String s) • int indexOf(String s)
• int length() • int lastIndexOf(String s)
• Char charAt(int i) • String replace(char c1, char c2)
• int compareTo(String S) • String substring(int i)
• int compareToIgnoreCase(String • String substring(int i1, int i2)
s) • String toLowerCase()
• boolean equals(String s) • String toUpperCase()
• boolean startsWith(String s) • String trim()
• boolean endsWith(String s)

String class – methods

void getChars(int i1, int i2, char ar[ ], int i3)


String[ ] split(delimiter)
Using Command-Line Arguments

• A command-line argument is the information that directly follows


the program’s name on the command line when it is executed.
• Example:
javac Add.java
java Add 10 20
“10”→args[0]
“20”→args[1]

You might also like