<<

Unit 3: Elementary Concept of Object and Classes Answer the following questions Question 1-Define Object with an example. An object is an entity having a specific identity, specific characteristics and specific behaviour. 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. Question 2-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.

Question 3-Class and Objects are inter-related. Explain. Or A class is also referred as 'Object Factory'. Comment.

A Class is used to create various Objects that have different characteristics and common behaviours just like a factory can produce similar items based on a particular design. 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 or class is also referred to as 'Object Factory'. Question 4-Why an Object is 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.

Question 5-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.

Question 6-Why is a class known as composite ? A class can contain data members of various primitive and reference data types. Hence, class is known as composite data type. Question 7-State two differences between fundamental and user-defined data type.

Fundamental data type User defined data type These are inbuilt data type provided by These are data types created by the user using the Java Language. fundamental or user defined data type or both. The size of it is fixed. The size of different user-defined data type depends upon the size of the individual components of it. These data types are available in all These data types are available only as specified by parts of a program within a class. the access specifiers. Question 8-State two differences between a class and an object.

A class An object Class is a blueprint or from which Object is an instance of a class objects are created Class is a group of similar objects Object is a real world entity such as pen, laptop, mobile, bed, keyboard, mouse, chair etc Class is a logical entity Object is a physical entity Question 9-Write a statement to create an object 'Keyboard' of the class 'Computer'. Computer Keyboard = new Computer(); Question 10-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()

Question 11-What is an access specifier? Which two access specifier is used in a class declaration?

Access modifiers (or access specifiers) are keywords in object-oriented languages that the accessibility of classes, methods, and other members. Access modifiers are a specific part of syntax used to facilitate the encapsulation of components. The public and default access specifier is used in a class declaration.

Question 12-State imortant rules you should follow for naming a class.

While using names for a class the following set of rules are to be kept in mind.

1. It can have any alphabet (capital or small), digits, underscore and dollar sign characters. For example, a, b, cat, mat123, cost_price, Topaz$ are all example of valid identifier.

2. It should not begin with digits or should not contain any special . For example 2ab, ab#, top@, etc., are invalid identifiers as it either begins with a digit or contain special characters (like #, @).

3. It cannot have a space between it. For example, Simple Interest or Selling Price are invalid class- names as it contains a space.

4. It must not be a keyword. For example, for, while, do are invalid class-names as they are keywords and are assigned some special function for the language.

5. It can be of any length. Even though Java gives you the flexibility to provide a huge length for an identifier a very long name is impractical and difficult to manage.

Question 13-What two conventions you should follow naming a class?

1. Class name should begin with capital letter. Example, Bank, School, Student

2. If a class name consists of more than one word, the first letter of each new word should be in uppercase. For example MyClass, AccountDetails, SimpleInterest, etc.

Question 14-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:

1. Acceptdata( ) is the characteristic of the class. False , it is behaviour of the class. 2. Address is behaviour of the class. False, it is characteristics of the class. 3. Acceptdata( ) and PrintData( ) are the common behaviour of the objects of class 'Our School'. False, different behavior 4. Behaviours are the medium of inter-object communication. True 5. Creating multiple objects of class 'School' is not possible. False Question 15-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(); } } Question 16-Rewrite the following program after removing the errors, underlining each corrections: class My Class { int a, b; void initialize( ) { a=5; b=6; } void show( ) { System.out.println(a+ “ ”+ b); } static void main( ) { My Class ob = new My Class( ); ob.initialize( ); show( ).ob; } } Ans. class MyClass { int a, b; void initialize( ) { a=5; b=6; } void show( ) { System.out.println(a+ “ ” + b); } static void main( ) { MyClass ob = new MyClass( ); ob.initialize( ); ob.show( ); } } Question 17-Which among the following are invalid class name in Java? State with reasons. 1. Compound Interest Ans. Invalid as it contains a space. 2. 1MyClass Ans. Invalid as it should not begin with a digit. 3. MyClass$ Ans. Valid 4. Myclass# Ans. Invalid as it contains a special character #. 5.My@Class Ans. Invalid as it contains a special character @. Tick () the correct option. 1. Which among the following creates a blueprint to represent characteristic and behaviour of an object? a. Object b. Class c. Instance d. None of these 2. Which among the following do not belong to an object? a. State b. Behaviour c. Identity d. Class 3. Which among the following is not a component of a class? a. Modifiers b. Class name c. Object d. Body 4. Which among the following keyword is used to allocate memory space for an object? a. new b. for c. while d. int 5. Which among the following operator is used to access individual members of an object? a. . (dot) b. + (plus) c. – (minus) d. / (divide) 6. Which among the following modifier is used in a ‘class’? a. public b. default c. Both a and b d. None of these 7. Which among the following is a valid class name? a. Simple Interest b. SimpleInterest c. 1SimpleInterest d. Simple@Interest 8. Which among the following is a valid object name? a. obj1 b. 1obj c. Obj 1 d. Obj#1 9. Which among the following is used to represent behaviour in a class? a. Method b. Data members c. Both a and b d. None of these 10. If a method named show( ) is to be invoked using an object ‘ob’ , which among the following is correct? a. ob.show( ) b. ob.show c. show( ).ob d. None of these B. State whether the following statements are True (T) or False (F). 1. An object is called a class factory. F 2. A class is an instance of an object. F 3. A class is a mechanism to implement encapsulation. T 4. Data members in a class is used to represent the characteristic of an object. T 5. The new operator is used to create an object. T 6. It’s a rule to have a class-name beginning in capital letter. F 7. Java is case sensitive. T 8. A class-name cannot be a keyword. T 9. The dot operator is used to access members in an object using an object. T 10. In programming every object will have a name. T C. Fill in the blanks. 1. A class is a template that binds together data and methods together. 2. The values in the attributes of an object is called the state of an object. 3. The . dot operator is used to access the individual members of a class 4. The keyword new is used to allocate memory space for an object. 5. The default and public access modifier is used with a class. 6. It is a common convention to begin a class-name in capital letter. 7. A class is called an object factory. 8. Object-name is used to give a unique name to an object for identification. 9. Behaviour of an object is represented by methods. 10. The size of fundamental data type is fixed. 11. A Class is also considered as an object factory.

12. The Object of a class differs on various characteristics.

13. The object of a class is represented through the attributes.

14. The term instantiation is used for creating various objects.

15. Class is a blueprint of the objects.

16. new keyword indicates an operator for dynamic allocation of an object.

17. Objects are also termed as class tags or entities.

18. Different objects of a class have common behaviour.