Classes
&
Objects
In JAVA
-M Vishnuvardhan,
Dept. of Computer Science,
SSBN Degree College, ATP
SSBN Degree College, ATP M Vishnuvardhan
Introduction
In real world we can find many objects like car, student, … All these
objects have state and behavior. To represent these real world entitles
Objects are used in software. Object is software representation of real
life thing. In order to create an object first wee need to describe how
the object should be.
SSBN Degree College, ATP M Vishnuvardhan
Class
A class is a template which describes an Entity or Object.
i.e., A class is a blueprint of the object. Using a class it is possible to
create multiple objects
A class describes an entity in two ways
1.Properties of class (gives characteristics)
2.Behavior(functionality)
Eg: Student --- RollNo, Name, gender ….
--- getAdmission, writeExam, getResult
SSBN Degree College, ATP M Vishnuvardhan
Representation of Class
Syntax:
class ClassName
{
declaration of properties
definations of methods
}
class Student
{
int rollNo;
String name;
void getAdmission()
{ ================ }
}
SSBN Degree College, ATP M Vishnuvardhan
Class - Properties
These are special variables used to describe the characteristics of an
Object. These are also called as attributes/ data members / data.
Syntax :
<<access Specifier>> dataType PropertyName;
Eg:
int rollNo;
String name;
Access Specifier controls the availability and accessibility of member
of a class in different parts of the program.
Eg:- public, private, protected, private protected & default.
For Every member access Specifier need to specified separately
SSBN Degree College, ATP M Vishnuvardhan
Class - Methods
These are functions used to describe the behavior of an Object.
These are also called as member functions/ code.
Syntax :
<<access Specifier>> returnType methodName(<<arguments>>)
{
======= // body of the method (method definition)
}
Eg: void getAdmisssion()
{
=====
}
void printData(int x)
{
=====
}
SSBN Degree College, ATP M Vishnuvardhan
Class - Methods
class Box
{
int length, width;
double area, perimeter;
void calculate()
{
area=length*width;
perimeter=2*(length+width);
}
void printData()
{
System.out.println(“Length:”+length);
System.out.println(“Width:”+width);
System.out.println(“Area:”+area);
System.out.println(“Perimeter:”+perimeter);
}
}
SSBN Degree College, ATP M Vishnuvardhan
Properties of Constructors
» A constructor cannot be called like a ordinary method.
» A constructor is called only once in the life time of an object.
» A constructor is always invoked at the time of creation of object
with the help of new keyword.
» Every class must have a constructor. Without constructor object
creation is not possible.
» If a constructor is not defined for a class the compiler provides a
constructor for the class.
SSBN Degree College, ATP M Vishnuvardhan
Constructor vs. method
Java Constructor Java Method
Constructor is used to initialize the
state of an object.
Method is used to expose
behaviour of an object.
Constructor must not have return
type.
Method must have return type.
Constructor is invoked implicitly. Method is invoked explicitly.
The java compiler provides a default
constructor if you don't have any
constructor.
Method is not provided by
compiler in any case.
Constructor name must be same as
the class name.
Method name may or may not
be same as class name.
SSBN Degree College, ATP M Vishnuvardhan
Types of Constructors
Constructors in java can be of two types
»Default Constructor:
Constructor provided by the compiler is called as default
constructor or a constructor with out parameters is also called as
default constructor
»Parameterized Constructor:
Constructor with parameters is called as parameterized
constructors.
SSBN Degree College, ATP M Vishnuvardhan
Types of Constructors
Constructors in java can be of two types
»Default Constructor:
Constructor provided by the compiler is called as default
constructor or a constructor with out parameters is also called as
default constructor
»Parameterized Constructor:
Constructor with parameters is called as parameterized
constructors.
SSBN Degree College, ATP M Vishnuvardhan
Questions

Classes&amp;objects

  • 1.
    Classes & Objects In JAVA -M Vishnuvardhan, Dept.of Computer Science, SSBN Degree College, ATP
  • 2.
    SSBN Degree College,ATP M Vishnuvardhan Introduction In real world we can find many objects like car, student, … All these objects have state and behavior. To represent these real world entitles Objects are used in software. Object is software representation of real life thing. In order to create an object first wee need to describe how the object should be.
  • 3.
    SSBN Degree College,ATP M Vishnuvardhan Class A class is a template which describes an Entity or Object. i.e., A class is a blueprint of the object. Using a class it is possible to create multiple objects A class describes an entity in two ways 1.Properties of class (gives characteristics) 2.Behavior(functionality) Eg: Student --- RollNo, Name, gender …. --- getAdmission, writeExam, getResult
  • 4.
    SSBN Degree College,ATP M Vishnuvardhan Representation of Class Syntax: class ClassName { declaration of properties definations of methods } class Student { int rollNo; String name; void getAdmission() { ================ } }
  • 5.
    SSBN Degree College,ATP M Vishnuvardhan Class - Properties These are special variables used to describe the characteristics of an Object. These are also called as attributes/ data members / data. Syntax : <<access Specifier>> dataType PropertyName; Eg: int rollNo; String name; Access Specifier controls the availability and accessibility of member of a class in different parts of the program. Eg:- public, private, protected, private protected & default. For Every member access Specifier need to specified separately
  • 6.
    SSBN Degree College,ATP M Vishnuvardhan Class - Methods These are functions used to describe the behavior of an Object. These are also called as member functions/ code. Syntax : <<access Specifier>> returnType methodName(<<arguments>>) { ======= // body of the method (method definition) } Eg: void getAdmisssion() { ===== } void printData(int x) { ===== }
  • 7.
    SSBN Degree College,ATP M Vishnuvardhan Class - Methods class Box { int length, width; double area, perimeter; void calculate() { area=length*width; perimeter=2*(length+width); } void printData() { System.out.println(“Length:”+length); System.out.println(“Width:”+width); System.out.println(“Area:”+area); System.out.println(“Perimeter:”+perimeter); } }
  • 8.
    SSBN Degree College,ATP M Vishnuvardhan Properties of Constructors » A constructor cannot be called like a ordinary method. » A constructor is called only once in the life time of an object. » A constructor is always invoked at the time of creation of object with the help of new keyword. » Every class must have a constructor. Without constructor object creation is not possible. » If a constructor is not defined for a class the compiler provides a constructor for the class.
  • 9.
    SSBN Degree College,ATP M Vishnuvardhan Constructor vs. method Java Constructor Java Method Constructor is used to initialize the state of an object. Method is used to expose behaviour of an object. Constructor must not have return type. Method must have return type. Constructor is invoked implicitly. Method is invoked explicitly. The java compiler provides a default constructor if you don't have any constructor. Method is not provided by compiler in any case. Constructor name must be same as the class name. Method name may or may not be same as class name.
  • 10.
    SSBN Degree College,ATP M Vishnuvardhan Types of Constructors Constructors in java can be of two types »Default Constructor: Constructor provided by the compiler is called as default constructor or a constructor with out parameters is also called as default constructor »Parameterized Constructor: Constructor with parameters is called as parameterized constructors.
  • 11.
    SSBN Degree College,ATP M Vishnuvardhan Types of Constructors Constructors in java can be of two types »Default Constructor: Constructor provided by the compiler is called as default constructor or a constructor with out parameters is also called as default constructor »Parameterized Constructor: Constructor with parameters is called as parameterized constructors.
  • 12.
    SSBN Degree College,ATP M Vishnuvardhan Questions