B Y :
H U M A S A M I N
Object Oriented Programming
Concepts
Class and Object
 Class is a user defined datatype.
 The class definition provides a template or blueprint,
which describes
 the data (instance variables) contained within, and
 the behavior (methods) common to all objects of a class.
 Object is a variable of a class.
Syntax of class
class ClassName
{
//data or instance variables
//behavior or methods
}
Data or Instance Variables
 The data is contained in variables defined within the
class
 Often called instance variables, data members,
properties or attributes.
 The instance variables are variables of the primitive
types or they can be reference variables of objects of
other classes.
Example
class Student
{
//data members or instance variables
int rollno;
String name;
int semester;
int[] marks;
//behavior or methods
}
Behavior or Methods
 The behavior is controlled by methods defined
within the class.
 Often called member methods or member functions.
 Syntax:
returntype methodName(parameterlist)
{
//valid java statements
}
Example
class Student
{
//data members or instance variables
int rollno;
String name;
int semester;
//behavior or methods
void displayValues( )
{
System.out.println(rollno);
System.out.println(name);
System.out.println(semester);
}
}
Object
 Object is a variable of a class.
 Object is the implementation of class.
 It is a software bundle of variables and methods.
 It is also known as instance of a class.
 The members of the class both data members and
methods are accessed with the help of the object.
Declaration and Definition of Object
 Syntax:
 Declaration of object:
ClassName objectName;
 Definition:
ObjectName=new ClassName( );
 Shortcut:
 ClassName objectName=new ClassName( );
Example
 Objects are created in the main method or any other
class.
public class StudentDriver
{
public static void main(String args[])
{
Student s;
s=new Student( );
}
}
Accessing Members of the class
 The members of the class(both data members and
methods) are accessed with the help of the object of
the class.
 Syntax:
objectName.instanceVariable=value;
OR
objectName.methodName();
Access Modifiers or Access Specifiers
Access
Specifier
Class Package SubClass World
private Y N N N
package Y Y N N
protected Y Y Y N
public Y Y Y Y
Example
public class Student
{
//data members or instance variables
private int rollNo;
public int semester;
//behavior or methods are kept public
public void displayValues( )
{
System.out.println(rollno);
System.out.println(name);
System.out.println(semester);
}
}
Note: Data Members are kept private and methods are kept public
Example Continued..
public class StudentDriver
{
public static void main(String args[])
{
Student s;
s=new Student( );
s.semester=2;
s.rollNo=123; //Not Allowed as its private
s.displayValues( );
}
}
Accessing private members
 To access the private members of the class, we have
to provide getter and setter methods in the class.
 The getters and setters have public access specifier.
 If x & y are the instance variables then for setters,
word “set” is used before the instance variable name
like setX, setY.
 For getters, word “get” is used before the instance
variable name getX, getY.
Example
public class Student
{
//data members or instance variables
private int rollNo;
public int semester;
//behavior or methods are kept public
public void setrRollNo(int r)
{
rollNo=r;
}
public int getRollNo( )
{
return rollNo;
}
public void displayValues( )
{
System.out.println(rollNo);
System.out.println(semester);
}
}
Example cont..
public class StudentDriver
{
public static void main(String args[])
{
Student s;
s=new Student( );
s.semester=2;
System.out.println(s.semester);
s.setrollNo(123);
System.out.println(s.getrollNo( ));
int r;
r=s.getrollNo();
System.out.println(r);
s.displayValues( );
}
}
Constructors
 Constructor is a special kind of method of the class
having the same name as that of the class and has a
no return type.
 It is called when an object of the class is going to be
created.
 The main purpose of writing constructor is
initialization of instance variables.
Constructor Example
public class Student
{
//data members or instance variables
private int rollNo;
public int semester;
//behavior or methods are kept public
//Constructor
public Student( )
{
rollNo=10;
semester=2;
}
public setrollNo(int r)
{
rollNo=r;
}
public int getrollNo( )
{
return rollNo;
}
public void displayValues( )
{
System.out.println(rollNo);
System.out.println(semester);
}
}
Constructor
 If you don’t provide constructor for class, the JVM
will provide a default (zero argument) constructor
and initialize the instance variables to default values.
Lab Work
 Write a program to create a class named Circle
having radius as a data member. The class should
contain two methods to calculate the area and
circumference of the circle.
 You have to create two objects of the Circle class and
display their area and circumference.

OOP concepts

  • 1.
    B Y : HU M A S A M I N Object Oriented Programming Concepts
  • 2.
    Class and Object Class is a user defined datatype.  The class definition provides a template or blueprint, which describes  the data (instance variables) contained within, and  the behavior (methods) common to all objects of a class.  Object is a variable of a class.
  • 3.
    Syntax of class classClassName { //data or instance variables //behavior or methods }
  • 4.
    Data or InstanceVariables  The data is contained in variables defined within the class  Often called instance variables, data members, properties or attributes.  The instance variables are variables of the primitive types or they can be reference variables of objects of other classes.
  • 5.
    Example class Student { //data membersor instance variables int rollno; String name; int semester; int[] marks; //behavior or methods }
  • 6.
    Behavior or Methods The behavior is controlled by methods defined within the class.  Often called member methods or member functions.  Syntax: returntype methodName(parameterlist) { //valid java statements }
  • 7.
    Example class Student { //data membersor instance variables int rollno; String name; int semester; //behavior or methods void displayValues( ) { System.out.println(rollno); System.out.println(name); System.out.println(semester); } }
  • 8.
    Object  Object isa variable of a class.  Object is the implementation of class.  It is a software bundle of variables and methods.  It is also known as instance of a class.  The members of the class both data members and methods are accessed with the help of the object.
  • 9.
    Declaration and Definitionof Object  Syntax:  Declaration of object: ClassName objectName;  Definition: ObjectName=new ClassName( );  Shortcut:  ClassName objectName=new ClassName( );
  • 10.
    Example  Objects arecreated in the main method or any other class. public class StudentDriver { public static void main(String args[]) { Student s; s=new Student( ); } }
  • 11.
    Accessing Members ofthe class  The members of the class(both data members and methods) are accessed with the help of the object of the class.  Syntax: objectName.instanceVariable=value; OR objectName.methodName();
  • 12.
    Access Modifiers orAccess Specifiers Access Specifier Class Package SubClass World private Y N N N package Y Y N N protected Y Y Y N public Y Y Y Y
  • 13.
    Example public class Student { //datamembers or instance variables private int rollNo; public int semester; //behavior or methods are kept public public void displayValues( ) { System.out.println(rollno); System.out.println(name); System.out.println(semester); } } Note: Data Members are kept private and methods are kept public
  • 14.
    Example Continued.. public classStudentDriver { public static void main(String args[]) { Student s; s=new Student( ); s.semester=2; s.rollNo=123; //Not Allowed as its private s.displayValues( ); } }
  • 15.
    Accessing private members To access the private members of the class, we have to provide getter and setter methods in the class.  The getters and setters have public access specifier.  If x & y are the instance variables then for setters, word “set” is used before the instance variable name like setX, setY.  For getters, word “get” is used before the instance variable name getX, getY.
  • 16.
    Example public class Student { //datamembers or instance variables private int rollNo; public int semester; //behavior or methods are kept public public void setrRollNo(int r) { rollNo=r; } public int getRollNo( ) { return rollNo; } public void displayValues( ) { System.out.println(rollNo); System.out.println(semester); } }
  • 17.
    Example cont.. public classStudentDriver { public static void main(String args[]) { Student s; s=new Student( ); s.semester=2; System.out.println(s.semester); s.setrollNo(123); System.out.println(s.getrollNo( )); int r; r=s.getrollNo(); System.out.println(r); s.displayValues( ); } }
  • 18.
    Constructors  Constructor isa special kind of method of the class having the same name as that of the class and has a no return type.  It is called when an object of the class is going to be created.  The main purpose of writing constructor is initialization of instance variables.
  • 19.
    Constructor Example public classStudent { //data members or instance variables private int rollNo; public int semester; //behavior or methods are kept public //Constructor public Student( ) { rollNo=10; semester=2; } public setrollNo(int r) { rollNo=r; } public int getrollNo( ) { return rollNo; } public void displayValues( ) { System.out.println(rollNo); System.out.println(semester); } }
  • 20.
    Constructor  If youdon’t provide constructor for class, the JVM will provide a default (zero argument) constructor and initialize the instance variables to default values.
  • 21.
    Lab Work  Writea program to create a class named Circle having radius as a data member. The class should contain two methods to calculate the area and circumference of the circle.  You have to create two objects of the Circle class and display their area and circumference.