I) Classes:
● A class is the user- defined data type and the main building block of
object – oriented programming.
● It holds data members and member functions in a single unit .
● The data members (variables / attributes) and member functions(methods)
can be accessed and used by creating an instance of that class(object)
// It is like a blueprint/template of an object .
● The data members are the attributes and member functions are the
behaviour .
Syntax :
<Access specifier> class classname
//Data member
//Member Function
Explanation: A class declaration includes the following component :
a) Access specifier – public / private / protected/ default
b) Class – keyword
c) Class name – valid name
● Every class name should start with an upper case character.
● You should not use special characters when naming classes
Example :
Example
public class Student
String name; // data member
int Reg_no ; // data member
void study() // Member function
● Thus in a class , the data members(attributes) and member
function(behaviour) are put together.
Q: Why is class called a composite data type ?
● A composite data type is one which is composed of various primitive data
types.
● A class is defined with a number of data members of different types . It binds
both data member and member function as a single entity
● When an object is created , it contains all the members described within the
class..
Thus class becomes such a data type that includes various primitive types.
Hence , the class is said to be a composite data type/ user defined
datatype.
Example :
public class Student
int roll;
String name;
Double marks.
Naming conventions
● Start with a capital letter .
A class name is an identifier ie) a series of characters consisting of
● Letters(A-Z , a-z)
● Digits(0-9)
● underscores ( _ )
● dollar signs ( $ )
Not allowed :
Digit at beginning
Space
Example:
class Raster;
class ImageSprite;
Names used for classes, variables, and methods are called identifiers.
In Java, there are several points to remember about identifiers.
They are as follows -
● All identifiers should begin with a letter (A to Z or a to z), currency character
($) or an underscore (_).
● Should not begin with a digit
● After the first character, identifiers can have any combination of characters.
● A keyword cannot be used as an identifier.
● Most importantly, identifiers are case sensitive
● Examples of legal identifiers: age, $salary, _value, __1_value.
● Examples of illegal identifiers: 123abc, -salary.
Q2: Creating a class means creating a data type of the user's choice. How ??
Ans : Class binds one or more primitive data types together to be used as a
single data type. The user creates a data type(class) and declares
variables(characteristics) and functions ( behaviour) with it.
Thus creating a class means creating a data type of the user's choice.
Q3) Why is class called as an object factory
Ans : A class is called an object factory because objects are created from the class
containing common attributes and behaviour. The class behaves like a specification
for creating such similar objects.
Q4) Define instance of a class
Ans : An instance of a class is an object. It is also known as a class object or class
instance
Q5) Explain the following statement “Class is a specification for Objects”.
A class is just a specification of the object. The attributes and methods in a class are
thus declarations that do not contain any values. However, an object is a concrete
instance of a class
II) Objects:
2.1 Two Categories of objects
A) Real world objects:
Real world objects are those that we experience in our day to day life.
Features :
● It is visible to us.
● Has definite shape and size.
● It can be brought into thought and figures.
Each real world object has characteristics and behaviour.
Characteristics / States ( attributes) –> Parts of body / Specifications
Behaviour Use / Functions
Example :
Consider “Table “ as an object and explain the characteristics of and behaviour .
Characteristics :
a) It has four legs
b) It is made up of wood
Behaviour :
a) Used to keep books
b) Used for study purpose
Q) Consider “Mobile “ as an object and explain the characteristics of and
behaviour .
Q) Consider “Student “ as an object and explain the characteristics of and
behaviour .
B) Software objects:
Objects are created while writing Java programs.
Relation between Real world objects and software objects
● Characteristics / States in a real world object is considered to be the
data member in the software objects.
● The behaviour in the real world object is referred to as functions or
methods in software objects
● Hence a software object is a combination of data members and
functions (methods) .
Example :
2.2 Features of objects :
● An object is an instance of a class .
● It represents a real world entity.
● It is used to allocate memory to a class’s data member and member function
● It is used to access the data member and member function of a class .
● An object consists of
a) State - They are the attributes of the object
b) Behaviour-They represent the methods of an object
c) Identity- Unique name of the object that enables it to interact with other
objects.
2.2 a) Accessing data members of the class using objects
Syntax:
Object_name.data_membername ;
2.2 b) Accessing the Member function of the class
Syntax:
Object_name.member_function name ;
Note : We use the dot operator(.) to access them
Example :
Q) What is meant by the term attribute ?
Attributes are defined as the characteristics or the data member possessed by
the objects of a class .
It is also referred as “states of an object”
Q What do you understand about the state of an object? Explain with an
example.
The state of an object is the particular condition it is in. For example, a lamp
can be on or off.
The lamp’s switch (methods) turn lamp on and turn lamp off are used to
access the state of the
lamp.
2.3 Message passing between objects
● Objects can interact(communicate) with each other by passing
information and receiving information among themselves.
● In Java, we implement message passing using method calls.
For example, when Object A invokes a public method of Object B, it is
sending a message to Object B.
Q1) How are classes and objects interrelated to each other ?
A class is used to create various objects which possess different characteristics and
common behaviour within it . So we can say that a class is a blueprint or a prototype
of an object.
Thus objects follow all the features which are defined within the class and hence
they are interrelated.
Example :
Q2) Define instantiation
Ans : Instantiation refers to the creation of new instances of objects in a program.
When we create an object, we are creating an "instance" of a class, therefore
"instantiating" a class.
Q3 ) What is the difference between instantiate and initialize in Java? .
Initialization Instantiation
Assigning a value to a variable when it Process of creating objects using ‘new’
is declared; keyword .
int a=10; Student s1 = new Student();
Initializing a variable is done exactly once This is when memory is allocated for an
when the computer loads your program object.
into memory for execution.
Q4) Why is an object called as an instance of a class
The object possesses variable and member methods defined within the class . Hence it
is called as an instance of a class .
Q5) What happens when we declaration of a class variable, instantiation of a
class and initialization of an object of class
Example :
Consider the class ‘Box’
class Box
double width;
double height;
double depth;
When we create an object ,
// declare reference to an object of class Box
Box mybox ;
// instantiation via new operator and
// initialization via default constructor of class Box
mybox = new Box();
Hence declaration of a class variable, instantiation of a class and
initialization of an object of class can be together illustrated as follows :
Q6) Role of new operator in creating an object.
The keyword ‘new’ is used for dynamic allocation of memory for the object .
Q7) Java program to create an object mp4 of class digital
digital mp4 = new digital();
Q 8) Class vs objects
Q9: Explain how objects communicate with each other.
Ans: Objects communicate with each other by sending messages. The
sender object requests the receiver object to perform an action.