Concepts of objects and classes
We have heard the terms class and object
in class VIII. Now let us learn in detail about
them. In the given picture what we can see
is a design of a car and few real cars (like
alto, S-cross etc). The class car is giving the
generaldesign for all the other cars (like the
cars will have four wheels, four doors,
accelerator etc) based on this design the
cars are manufactured. But each car is
different from the other; it can be in respect
of color, speed, sitting capacity etc. Hence
what we can say is a class gives us the
basic structure based on which the objects
are created.
A class as a specification for objects:
1) Defines the common data and methods as single unit that will be shared by set of objects.
2) A class is a blue print that defines the variables and the methods common to all objects.
3) A class is a collection of objects of similar type.
Objects:
An object is a unique entity that has some characteristics and behavior. It is also known as an instance of a
class.
What is the difference between a real world object and a software object?
Real world objects have physical characteristics Software objects also have state and behaviour
(state and behaviour) State is maintained through variables or data
Ex: Motor bike items
characteristics two wheels, number of gears Behaviour maintained through
behaviour applying break, changing the gears functions/methods
etc...
A class as an object factory
A class is a blue print from which each individual objects are created. All the objects inside class have same
characteristics and behavior that is data and function or we can say a class produces similar types of object.
Thus a class is called an object factory.
What do you mean by message passing?
Exchange of information and data between two objects is known as message passing. Objects communicate
with each other through message passing.
Example:
1) People communicate with each other.
2) Government work through many different Ministries.
Why is a class known as composite data type?
A class binds up and hence encapsulates one or more primitive datatypes (such as int, double etc.) together to
be used as a single unit ( data type) .Hence ,it is called composite data type.
Why is a class known as user-defined data type?
The design of the class is decided by the programmer according to his programs need. Hence it is called user
defined data type.
Sample Questions:
1. Categorize each of the following as a class or an object
a) A design of a chair on a paper
b) A table
c) Blue print of a House
2. Define an object.
3. What is class?
4. Difference between a class and an object.
5. What do you mean by message?
6. What do you mean by message passing?
7. How are classes and objects are related to one another?
8. Why are objects said to be instances of class?
9. Why are classes considered as object factories?
10. Why class is known as a user defined data type?
11. How objects communicate with each other?
Assignment -1(Simple programs)
1. Write a program in java to store / input the radius of a Circle, and calculate the Area and Circumference
using the formula Area =3.141*R2 and Circumference = 2 *3.141*R
2. Write a program in java to swap the values stored in two variables (Without using a third variable) Display
the values of the two variables before and after swapping.
3. Write a program in java to input the Basic Salary of an employee and calculate his Gross and Net Salary
based on the information given below
D.A. (Dearness Allowance) = 75% of Basic Salary
H.R.A. (House Rent Allowance)= 25% of Basic Salary
P.F. (Provident Fund) = 12% of (Basic + D.A.)
Gross Salary = Basic + D.A + H.R.A
Net Salary = Gross Salary – P.F.
4. Write a program in java to input the Principal, Rate and Time and calculate the Simple Interest and Amount.
5.Write a program in java to input three integers and find the difference between their sum and average.