03_Objects and Classes in java.pdf

79 views 29 slides Jan 25, 2023
Slide 1
Slide 1 of 29
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29

About This Presentation

objects


Slide Content

Nihar Ranjan Roy

WHATISACLASS?
It is defined as a blueprint or a template from
which an object is declared
[access specifier] [modifiers] class <class name>
{
………………
Members of the class
……………....
}
2

RULESFORNAMINGACLASS
A class name should not be a keyword in java
Name can begin with letter, an _ or $
Name should not contain embedded space or
period(.)
It can be of any length
3

WHATISANOBJECT?
Its an instance of a class.
Its maintains its states in variables and
implements its behavior with its methods
<Class name><object name>;
Example
Student obj;
Obj=new Student();
Objt=new Student(..arguments);
Or
Student obj=new Student();
4

MEMBERSOFACLASS
Members
of a class
variables
Instance variables Class variables
Methods
5

HOWTODECLAREMETHODS?
Syntax
[access specifier][modifier]<return_type>
method_name([Parameters])
{
//body
}
Example
intadd(inta , intb)
{return (a+b);}
6

HOWTOACCESSTHEMEMBERS?
Obj.<member name>;
ClassName.<member>;for static members
7

NEWOPERATOR?
Declaration Student obj;
obj
Creationobj=new Student()
obj
NULL
address
Name
Roll
8

PROBLEM
Create class named student whose members are
Name// to store student name
Roll //to store his roll no
Init_mem(parameters) //to initialize the members
Disp() // display name and roll
9

10
class Student
{
String name;
introll;
void set_Value(String n,intr)
{
name=n;
roll=r;
}
void disp()
{
System.out.println("Name is==>"+name);
System.out.println(“Roll is==>"+roll);
}
}
class StudentMClass
{
public static void main(String args[])
{
Student st=new Student();
st.set_Value("Nihar",1);
st.disp();
}
}

HOWTOINITIALIZEANOBJECT
11

CONSTRUCTORS
1.A constructor is a special function having the
same name that of the class.
2.It has no return type
3.And is invoked through new operator
Syntax
[Modifier]<class_name>([Parameter list])
{
//Body of constructor
}
12

TYPESOFCONSTRUCTORS
Student obj=new Student();
Student obj=new Student( name, roll);
Student obj1=new Student (obj)
13

PROBLEM
Create class named student whose members
are
Name// to store student name
Roll //to store his roll no
Disp() // display name and roll
Implement
Default constructor
Parameter Constructor
Copy Constructors
14

15
class Student
{
String name;
introll;
Student()
{ System.out.println("===Default===");}
Student(String n,intr)
{
name=n;
roll=r;
System.out.println("===parameter===");
}
Student (Student st)
{name=st.name;
roll=st.roll;
System.out.println("===Copy===");
}
void disp()
{
System.out.println("\nNameis==>"+name+"\nRollno is==>"+roll);
}
}
Default
Parameter
Copy

16
class StudentCClass
{
public static void main(String args[])
{
Student st=new Student();
st.disp();
Student st1=new Student("Nihar",1);
st1.disp();
Student st2=new Student(st1);
st2.disp();
}
}
Default
Parameter
Copy

class Student
{
String name;
introll;
/*Student()
{ System.out.println("===Default===");}*/
Student(String n,intr)
{
name=n;
roll=r;
System.out.println("===parameter===");
}
void disp()
{System.out.println("\nNameis==>"+name+"\nRollno is==>"+roll);
}
}
17
Default
Commented
Parameter
C:\myjava\StudentCClass.java:36: cannot find symbol
symbol : constructor Student()
location: class Student
Student st=new Student();
Student st=new Student();

WHATAREDESTRUCTORS?
Finalizers are methods that are called immediately
before an object is garbage collected
Syntax
protected void finalize()
{
}
18

KEYWORD“ THIS”
this is used to indicate the current object.
Class Color
{intr,g,b;
Color(intr, intg, intb)
{
r=r, g=g, b=b;
}
}
this.r=r, this.g=g,this.b=b;
19

ACCESSSPECIFIERINJAVA
20

PUBLIC
public class Student
{
public String name;
public introll;
public void disp()
{………
……
}
}
Accessible from all other
classes
Inner classes cannot have
public access specifiers
Only one public class can
be declared in a source file
21

PRIVATE
Only objects of the same class
can have access to private
members
This provides highest degree of
protection
Subclasses cannot derived
private members
22

PROTECTED
Members declared as protected are accessible
from the class itself and its sub class also.
23

UNSPECIFIED/DEFAULT/FRIENDLY
If non of the access specifiers are specified then it
has package level access i.eall the classes in the
same package can access it.
24

CONCLUSION
25
None
/default
(package)
class
interface
member
Accessible only in its package
Accessible only in its package
Accessible only in its package
private member Accessible only in its class(which
defins it).
protectedmember Accessible only within its package
and its subclasses
public class
interface
member
Accessible anywhere
Accessible anywhere
Accessible anywhere its class is.

MODIFIERSINJAVA
Modifiers
static
final
abstract
native
transient
synchronize
volatile
26

27
ModifierUsed on Meaning
abstractclass
interface
method
Contains unimplemented methods and cannot be instantiated.
All interfaces are abstract. Optional in declarations
No body, only signature. The enclosing class is abstract
final class
method
field
variable
Cannot be subclassed
Cannot be overridden and dynamically looked up
Cannot change its value. static final fields are compile-time
constants.
Cannot change its value.
native method Platform-dependent. No body, only signature
strictfpclass
method
All methods in the class are implicitly strictfp.
All floating-point computation done is strictly conforms to
the IEEE 754 standard. All values including intermediate
results must be expressed as IEEE float or double values.
It is rarely used.
MODIFIERS….

MODIFIERS….
28
static class
method
field
initializer
Make an inner class top-level class
A class method, invoked through the class name.
A class field, invoked through the class name
one instance, regardless of class instances created.
Run when the class is loaded, rather than when an instance
is created.
synchronizedmethod For a static method, a lock for the class is acquired before
executing the method. For a non-static method, a lock for
the specific
object instance is acquired.
transient field Not be serialized with the object, used with object
serializations.
volatile field Accessible by unsynchronized threads, very rarely used.

PROBLEM
Writeaprograminwhichwhenever
anobjectiscreatedofthatclass
itprintsthenoofobjectscreated
tillnow
29
Tags