Documente Academic
Documente Profesional
Documente Cultură
: 2
Classes and
Objects
School topics
of Computer
Science
& Engineering
PS : These
are
primitive
steps in
Class Fundamentals
Class Fundamentals
FIGURE
class
Ob1
CIRCLE
Ob3
Ob2
RECTANGLE
SQUARE
Class Syntax
class <ClassName>
{
attributes/variables;
Constructors();
methods();
}
Class Example
class Student
{
int iID;
String sName;
void insertRecord(int iID, String sName){
//method body
}
void displayRecord(){
//method body
}
Student(){
//constructor body
}
School of Computer Science & Engineering
Objects
Example
Student Ravi = new Student();
Student Rajesh = Ravi;
Methods
Methods
Methods
Methods
Syntax
<Object_Name> [DOT] <Method_Name>
(<ParameterList>);
Example
Ravi.insertRecord(1,Ravi);
Constructors
Parameterized
constructor
School of Computer Science & Engineering
Constructors
Constructors
Constructors
Summary
new Operator will create an object.
As soon as Object gets created it will call Constructor.
Thus Constructor Initializes an Object as soon as after
creation.
School of Computer Science & Engineering
Constructors
Example
Method Overloading
Method Overloading
Constructor Overloading
Similar
to
method
overloading,
constructor
overloading has a multiple constructors by same
name but different parameters (signature).
If we have to perform only one operation, having
same name of the methods increases the readability
of the program.
this keyword
this keyword
this keyword
Garbage Collection
Garbage Collection
By nulling a reference :
Employee e = new Employee();
e = null;
By anonymous object:
new Employee();
School of Computer Science & Engineering
Passing Parameters
Passing Parameters
Access
Modifier
Within
Class
Within
Package
Outside
Package by
Subclass only
Outside
Package
Default
Private
Protected
Public
static keyword
static keyword
Static variable
If you declare any variable as static, it is known static
variable.
The static variable can be used to refer the common
property of all objects e.g. company name of employees,
college name of students etc.
The static variable gets memory only once in class area at
the time of class loading.
It makes your program memory efficient (i.e it saves
memory).
static variable
static method
static method
Example
static block
Is used to initialize
the
static
data
member.
It
is
executed
before
main
method
at
time
the
of
classloading.
Example
final keyword
final variable
final method
final class
Advantages
Description
Member Inner
Class
Anonymous
Inner Class
Local Inner
Class
Static Nested
Class
Nested Interface
Model Questions
Model Questions
The
other
customer
debit
and
can
services
credit
are,
amount
the
to
Model Questions
Model Questions
Consider
register
for
courseTitle,
Student(stuId,
4
courses,
stuName,
semester),
Course
(courseNo,
courseDuration(numberOfhours),
Model Questions
Interview Questions