Elementary Concept of Object and Classes
A Class is also considered as an object factory.
The Object of a class differs on various characteristics.
The object of a class is represented through the attributes.
The term instantiation is used for creating various objects.
Class is a blueprint of the objects.
new keyword indicates an operator for dynamic allocation of an object.
Objects are also termed as class tags or entities.
Different objects of a class have common behaviour.
Define Object with an example.
An object is an entity having a specific identity, specific characteristics and specific behavior. Taking a car as an example of an object, it has characteristics like colour, model, version, registration number, etc. It has behaviours like start the engine, stop the engine, accelerate the car, apply the brakes, etc.
Mention five states (characteristics) and two methods for the following Classes:
(a) Class Employee
Characteristics | Methods |
---|---|
Name | computeSalary() |
Employee Number | computeTax() |
Pan Number | |
Salary | |
Income Tax |
(b) Class Bank
Characteristics | Methods |
---|---|
Bank Name | openAccount() |
Bank Address | depositMoney() |
IFSC Code | |
MICR Code | |
Accounts |
(c) Class School
Characteristics | Methods |
---|---|
School Name | admitStudent() |
School Address | conductExams() |
Classes | |
Students | |
Teachers |
(d) Class Book
Characteristics | Methods |
---|---|
Book Name | buyBook() |
ISBN Number | readBook() |
Price | |
Author | |
Publisher |
(e) Class Park
Characteristics | Methods |
---|---|
Park Name | openPark() |
Park Address | closePark() |
Ticket Price | |
Opening Time | |
Closing Time |
(f) Class Medicine
Characteristics | Methods |
---|---|
Name | buyMedicine() |
Quantity | sellMedicine() |
Manufacturing Date | |
Expiry Date | |
Price |
(g) Class Computer
Characteristics | Methods |
---|---|
Model | startComputer() |
Serial Number | shutdownComputer() |
Processor | |
RAM | |
Hard Disk |
(h) Class Camera
Characteristics | Methods |
---|---|
Model | takePicture() |
Serial Number | adjustZoom() |
Colour | |
Price | |
Resolution |
What is an Object? Give five examples of real world objects.
An object is an entity having a specific identity, specific characteristics and specific behavior. Examples — car, bottle, mobile phone, computer, student.
How will you define a software object?
A software object replaces the characteristics and behaviours of a real world object with data members and member methods, respectively.
Class and Objects are inter-related. Explain.
A Class is used to create various Objects that have different characteristics and common behaviours. Each object follows all the features defined within a class. That is why class is also referred to as a blue print or prototype of an object. This way we can say that they are inter-related.
A class is also referred as 'Object Factory'. Comment.
A class has the complete description of the data elements the object will contain, the methods the object can do, the way these data elements and methods can be accessed. A class can create objects of itself with different characteristics and common behaviour just like a factory can produce similar items based on a particular design. Hence, class is also referred to as 'Object Factory'.
What does the following statement mean?Employee staff = new Employee ( );
This statement creates a new object of class Employee. The newly created object is assigned to a variable named staff which is of Employee type. The object can be accessed using staff variable.
Why is an Object called an 'Instance' of a class? Explain.
A class can create objects of itself with different characteristics and common behaviour. So, we can say that an Object represents a specific state of the class. For these reasons, an Object is called an Instance of a Class.
Why is a class known as composite data type?
A class can contain data members of various primitive and reference data types. Hence, class is known as composite data type.
Write a statement to create an object 'Keyboard' of the class 'Computer'.
Computer Keyboard = new Computer();
Consider a real world object as 'Cricket Ball'. Now, mention two behaviours and methods each by taking the 'Cricket Ball' as a software Object.
Characteristics — Colour, Condition
Behaviours/Methods — throw(), stop()
Refer a class structure as shown below:
class MySchool {
Name
Address
Principal's name
AcceptData();
PrintData();
}
With reference to the above class declaration, indicate whether the following statements are True/False:
Acceptdata( ) is the characteristic of the class.
FalseAddress is behaviour of the class.
FalseAcceptdata( ) and PrintData( ) are the common behaviour of the objects of class 'MySchool'.
TrueBehaviours are the medium of inter-object communication.
TrueCreating multiple objects of class 'MySchool' is not possible.
False
You want to create a class 'Football'. Choose the elements to be used as characteristics and behavior from the list given below:
Ball, Goalkeeper, Making a goal, Defender, Forward player, passing ball, Referee, hitting corner, making fault
Characteristics | Behaviours |
---|---|
Ball | Making a goal |
Goalkeeper | passing ball |
Defender | hitting corner |
Forward player | making fault |
Referee |
Write a program by using a class 'Picnic' without any data members but having only functions as per the specifications given below:
class name : Picnic
void display1( ): To print venue, place and reporting time.
void display2( ): To print number of students, name of the teacher accompanying and bus number.
Write a main class to create an object of the class 'Picnic' and call the functions display1() and display2( ).
class Picnic
{
public void display1() {
System.out.println("Venue: Botanical Garden");
System.out.println("Place: MG Road");
System.out.println("Reporting Time: 9:00 AM");
}
public void display2() {
System.out.println("Number of Students: 50");
System.out.println("Name of teacher: Mr. Nagabhushan");
System.out.println("Bus Number: KA 01 1234");
}
public static void main(String args[]) {
Picnic obj = new Picnic();
obj.display1();
obj.display2();
}
}