Class as the Basis of all Computation | ICSE Class 10 Computer Applications Notes
On this page
This note covers objects, state and behaviour, member variables and methods, classes as abstractions and object factories, encapsulation, primitive and composite data types, Java variable declarations, object creation and method calls.
What is an object, and how does it represent something?
Definition: An object is an instance of a class: a particular entity represented through its state and behaviour. A class is the common specification from which objects of that type are created.
State means the values describing an object at a particular time. Behaviour means the operations associated with it. An operation is an action that a program can perform. A program is a set of instructions for a computer.
In object-oriented programming, a program is organised around objects that combine related data and operations. Data means the values being stored or processed. Looking at both data and operations gives a fuller description than naming the entity alone.
How do real-life examples help?
A student can be described by name, date of birth, address, class and marks. Giving an examination describes behaviour. Here, the student's school class is an educational grouping; the programming term class means a specification for objects.
A mobile phone can be described by model, colour and price. Storing a number is an operation associated with the phone. The model and price are descriptions of the phone, while storing a number describes something it does.
These examples separate the entity, its descriptive information and its actions. They do not require every detail of a real student or phone to appear in a program. The representation contains the features relevant to the problem being solved.
An object is therefore more than a loose collection of values. Its data is associated with behaviour through a class. When explaining an object, identify what it represents, which information describes it and which operations belong to it.
How do member variables and member methods describe an object?
A variable is a named storage location for a value of a particular type. A data type determines the kind of value and the operations permitted on it. A member variable is a variable declared as part of a class.
The terms data member and field are also used for a member variable. An attribute, property or feature is a descriptive characteristic. In an object model, such characteristics can be represented by member variables.
A method is a named block of instructions defined in a class. A member method expresses behaviour associated with that class. Calling, or invoking, a method means requesting execution of its instructions.
What distinguishes a feature from an action?
| Aspect | State | Behaviour |
|---|---|---|
| Meaning | Descriptive values associated with an object | Operations associated with the object |
| Program representation | Member variables that hold data | Member methods that contain instructions |
| Mobile-phone illustration | Model, colour and price | Storing a number |
| Student illustration | Name, date of birth, address, class and marks | Giving an examination |
An instance variable is a non-static field: each object has its own associated value for that field. Here, non-static means belonging to an instance rather than being a class-level member shared by instances. This distinction prevents confusion between an object's state and shared class data.
The variable's name identifies a feature; its value supplies the actual information for an object. Likewise, a method's name identifies an operation, while its instructions specify what happens when the operation executes.
A method may inspect data, change data or produce a result without changing the object's state. It is therefore inaccurate to define behaviour as a compulsory change of every member variable. The relationship is that methods provide operations involving the represented information.
How does a class act as an abstraction for objects?
Abstraction means identifying essential features while leaving out unnecessary implementation details. Implementation means the instructions and internal arrangements used to carry out an operation. A class provides an abstraction by describing the common features and behaviour of a set of objects.
The class defines a common structure rather than a separate structure for every individual object. The Student class can represent students sharing such attributes as name, date of birth, address, class and marks, together with associated behaviour.
The names Rani and Ravish identify individual students in this illustration. They belong to the common Student category. Their inclusion in the same class does not mean that every descriptive value must be identical.
Property: Objects of a class share common attributes and behaviour
A class specifies which information is represented. Individual objects supply their own values for instance variables. Thus, a shared attribute means a shared kind of information, not a requirement that every instance have the same information.
The same reasoning applies to the mobile-phone category. Model, colour and price identify common features. The category can describe individual phones without making the class definition itself a particular phone.
Note: Distinguish the common description from the individual values. Similar attributes and behaviour place objects in a common category; they do not make those objects interchangeable in every respect.
Abstraction also helps separate what an operation does from how it is carried out. A user of a mobile phone can use its operations without studying the internal circuitry. In a program, the useful description of a method similarly centres on its purpose and required information.
When explaining a class as an abstraction, connect the general description to the set of objects it represents. Merely calling it a blueprint leaves out the reason: the class gathers the essential common features of those objects.
Why may a class be viewed as an object factory?
A class may be regarded as a blueprint for creating objects. It may be viewed as a factory that produces similar objects. These comparisons describe the relationship between a common specification and the individual instances made from it.
Instantiation is the creation of an object from a class. The resulting object is an instance of that class. The class definition can be used when creating further objects of the same type, without rewriting the common member declarations each time.
Property: A class is a blueprint for its instances
| Basis | Class | Object |
|---|---|---|
| Role | Provides a common specification | Is a particular instance of that specification |
| Information | Declares the members describing the type | Has state associated with its instance variables |
| Creation | Provides the type used to create instances | Results from instantiation |
| Student illustration | Student is the common category | Rani and Ravish illustrate individual students |
The factory comparison does not mean that an object is a physical manufactured item. A Java object exists as part of a running program. Java is the programming language used here; running a program means executing its instructions.
Nor does similar mean identical. Objects of a class share the member definitions provided by that class, but their instance-variable values can differ. Creating an instance does not automatically create a new class definition.
A reference is a value through which a program can refer to an object. It should not be confused with the class definition or with the object's complete collection of fields.
A class describes the type, while an object provides a particular instance on which instance operations can act. Remembering both parts helps when reading declarations: a class name identifies a type, whereas a reference variable can identify which object an operation is to use.
How do objects encapsulate state and behaviour?
Definition: Encapsulation means combining related data and the methods operating on that data into a single unit. Classes provide this organisation for objects.
The state and behaviour of an object are connected. Member variables describe its stored information, and member methods supply operations associated with that information. Encapsulation keeps this relationship visible in the organisation of the program.
In the mobile-phone model, model, colour and price are descriptive data. Operations such as storing a number belong with the phone's behaviour. A class groups the description of these related features and operations.
Property: Encapsulation combines related data and methods in a class
| Concept | Main concern | Question it answers |
|---|---|---|
| Abstraction | Selecting and presenting the essential features | What should the representation show? |
| Encapsulation | Combining related data and operations within a unit | How are those related parts organised together? |
| Data hiding | Restricting access to internal data | Which parts may other code access directly? |
Data hiding concerns restrictions on access. An access modifier is a keyword controlling where a declaration can be accessed. A keyword is a word reserved by the programming language for a defined purpose.
In Java, the modifier private restricts access to a member. Placing a field in a class does not by itself make the field private. For the introductory class model, distinguish the act of grouping members from the choice of access restrictions.
Encapsulation and abstraction can work together. A class can group its data and operations while presenting the operations needed by other code. The user can then focus on the offered behaviour without needing every internal detail.
Do not interpret encapsulation as a guarantee that every field changes only through one particular method. The precise access available depends on the declarations. The central idea here is that related state and behaviour belong together in the object model.
How is the basic structure of a Java class written?
A class definition introduces a class name and contains its member declarations. A declaration introduces a name and its type or other required details. Syntax means the grammatical rules governing how these parts must be written.
In the following syntax pattern, ClassName is a placeholder for the chosen class name. The words “member declarations” indicate where fields and methods belong; they are explanatory words rather than Java instructions to copy literally.
The keyword class introduces the definition. The opening brace { starts the class body, and the closing brace } ends it. The class body is the region containing the class's members.
Syntax:
class ClassName {
member declarations
}
Where do fields and methods belong?
A field declaration is placed in the class body, outside a method body. A method definition contains the method's identifying information and its executable instructions. A method body is the block of instructions belonging to that method.
The containing braces matter. A variable declared inside a method is a local variable, meaning a variable belonging to that local block of execution. It is not an instance variable merely because the method itself occurs inside a class.
A Java identifier is a name used for a program element such as a class, variable or method. Java distinguishes uppercase and lowercase letters. Consequently, each use of an identifier must match the declared name accurately.
The arrangement gives a useful reading order: locate the class name, identify the member variables, then identify the member methods. After that, examine the instructions within each method. This separates the definition of the object's structure from the actions performed during execution.
A class definition need not contain a fixed number of fields or methods. Choose members according to the represented data and operations. The class and object relationship depends on the role of those members, not on a prescribed member count.
What are primitive data types in Java?
A primitive data type is a basic value type provided by Java. Its value is not a class instance. Primitive types support the fundamental kinds of data on which program operations work.
Java has eight primitive types. An integer is a number without a fractional part. A floating-point type represents numerical values using a finite-precision floating-point format; many fractional values are represented approximately.
Unicode is a standard for representing text characters. UTF-16 is an encoding form, or representation scheme, using 16-bit code units. A bit is a binary digit; a code unit is one storage unit of the encoding. Some characters require more than one code unit.
| Group | Java types | Kind of value |
|---|---|---|
| Integer types | byte, short, int, long | Integer values within the range of the selected type |
| Floating-point types | float, double | Numerical values represented with finite precision |
| Character type | char | A single UTF-16 code unit |
| Logical type | boolean | Either true or false |
How should primitive values be understood?
The selected type affects which values a variable can hold and which operations are suitable. A numerical type, a character type and a logical type serve different purposes. A type name is therefore more than a label attached to a variable.
true and false are the literal boolean values. A literal is a value written directly in program text. A boolean represents a logical result, not a general numerical quantity.
A primitive variable holds a primitive value. It does not become an object simply because the declaration appears in a class. The class can contain primitive fields, but the field's declared type remains primitive.
Not every Java value is an object. Classes organise the object model, while primitive types supply basic values. Both have roles within a Java program.
How do composite types differ from primitive types?
A composite type brings together a structured collection of related information. A class is treated as a composite type because its objects can combine member data with associated operations. A class may also be considered as a new data type created by the user, with its own functionality.
User-defined means defined by the programmer rather than supplied as a primitive type by the language. Defining a class therefore introduces a type whose structure and operations are described by its members.
What does a class-type variable hold?
A class type is a reference type in Java. A variable of that type holds a reference to an object, or the special value null, which means that the reference does not refer to an object. The variable is not the complete object itself.
| Basis | Primitive type | Class type |
|---|---|---|
| Definition | A basic value type supplied by Java | A type described by a class definition |
| Variable contents | A primitive value | An object reference or null |
| Structure | No programmer-defined instance fields within the primitive value | Can describe related fields and methods |
| Creation of a user-defined type | A variable declaration uses an existing primitive type | A class definition introduces the class type |
String is a Java class representing character strings, meaning sequences of characters. It is not a primitive type. Its familiarity does not alter its classification as a class type.
Composite and user-defined do not mean exactly the same thing. Java also supplies classes, such as String, that programmers can use without writing those class definitions themselves. The distinction to remember is between basic primitive values and values referring to structured objects.
A class's fields need not all have different types. The important point is that the class describes related members as a unit. Counting different type names inside a class is not a valid test for deciding whether it is a composite type.
How are variables declared and objects created?
Declaration introduces a variable and its type. Initialisation supplies its initial value. Object creation produces an instance. These actions are related, but naming a variable of a class type does not itself create an instance of that class.
In these syntax patterns, variableName stands for the chosen variable identifier, and referenceName stands for the chosen reference-variable identifier. ClassName again stands for the class's declared name. The semicolon ; terminates each declaration statement.
Syntax:
int variableName;
ClassName referenceName;
The first pattern declares a primitive variable of type int. The second declares a variable of a class type. Neither pattern contains an explicit initial value. These are declaration patterns, not complete programs with a displayed output.
How does creation differ from declaration alone?
The keyword new is used in a class-instance creation expression. An expression is a combination of program elements evaluated to produce a value. The assignment symbol = places the value of the expression on its right into the variable on its left.
A constructor is the class mechanism invoked during instance creation to initialise a new object. Empty parentheses () in the following pattern indicate that no arguments are supplied. Arguments are values or expressions supplied in a call.
Syntax:
ClassName referenceName = new ClassName();
This pattern declares a reference variable, creates an object and stores the resulting reference in that variable. It assumes ClassName names an instantiable class with an accessible no-argument constructor. Instantiable means capable of having an object created from it.
The declaration alone and the combined pattern therefore have different effects. A local reference variable must be assigned before use; declaring its type does not supply an object for a subsequent instance-method call. Keep this distinction when explaining what a statement accomplishes.
How do method calls connect classes with computation?
Computation means carrying out operations on data to obtain a result or perform a task. In the object-oriented model, objects provide state, and methods define the operations that work with that state and other supplied information.
A message in this context is a request for an object to perform an operation. In Java, a method call expresses such a request. The word does not mean a text message sent through a phone or network.
In the syntax below, methodName stands for the name of an accessible instance method requiring no arguments. The dot . selects a member through the reference before it. The parentheses mark the method call.
Syntax:
referenceName.methodName();
This pattern assumes that referenceName has already been assigned a reference to a suitable object. The class supplies the method definition, while the reference identifies the object receiving the request. The semicolon ends the method-call statement.
How can a class-based computation be followed?
- Identify the class and the members it declares. Separate stored data from the methods that operate on data.
- Identify the object being used. Check where its reference was assigned before an instance method is invoked.
- Locate the requested method. Use its actual instructions to determine the operation, rather than guessing from its name.
- Follow the instructions and the values they use. Distinguish any changed state from any value returned or text displayed.
A return value is a value sent back to the caller, meaning the code that requested the method. Displayed output is information written for the user to see. Returning a value and displaying it are different actions.
Calling a method therefore does not necessarily produce visible output. Its effect depends on the instructions it contains. Likewise, a method definition describes an operation; an actual call requests that operation during execution.
The class supplies the organised description of data and behaviour. Objects supply particular instances, and method calls connect those instances to actions. Together, these relationships explain the role of a class as the basis for organising computation.
Glossary
- Object — A particular instance of a class, represented through its associated state and behaviour.
- Class — A specification describing the common data members and methods for objects of a type.
- State — The values of the data describing an object at a particular point during execution.
- Behaviour — The operations associated with an object, expressed through methods in the program.
- Member variable — A variable declared as part of a class, also called a data member or field.
- Member method — A method defined in a class that describes an operation associated with that class.
- Abstraction — Identifying and presenting essential features while leaving unnecessary implementation details out of the description.
- Encapsulation — Combining related data and the methods operating on it within a single unit.
- Instantiation — The process of creating an object as an instance of a particular class.
- Primitive type — A basic Java value type whose values are not instances of a class.
- Composite type — A type representing structured information; a class can combine related fields and associated operations.
- Reference — A value through which a program can refer to an object during execution.
- Declaration — A statement or construct that introduces a program name with its required type or other details.
- Initialisation — The act of supplying the initial value for a variable or an object's data.
- Method call — A request to execute a method, supplying any arguments required for the operation.
Common errors and misconceptions
- Misconception: A class and an object are the same thing. Correct: A class supplies a common specification; an object is a particular instance created from that specification. State belongs to the particular object through its instance variables.
- Misconception: Objects of the same class must have identical field values. Correct: They share the class's member definitions, but their instance-variable values can differ. Common attributes mean common kinds of information, not necessarily equal values.
- Misconception: A member variable describes an action, while a method stores an attribute. Correct: Member variables represent data; member methods express operations. Distinguish a feature of an object from something the object can do.
- Misconception: Every value in Java is an object. Correct: Java also has primitive values. A primitive variable does not become an object merely because it is declared within a class.
- Misconception: Declaring a class-type variable automatically creates an object. Correct: Declaring the reference and creating the object are different actions. The combined creation pattern includes new and a constructor invocation.
- Misconception: Encapsulation automatically makes all fields private. Correct: Encapsulation groups data and operations; access restrictions depend on the declarations. Grouping members and restricting their direct access are related but distinct ideas.
- Misconception: Every method call prints a result. Correct: A method does what its instructions specify. Changing state, returning a value and displaying output are different possible effects of execution.
Exam-style questions with model answers
Q1. Define a class and an object, giving one distinct definition for each. [2 marks]
- A class is a common specification that describes the data members and methods for objects of a particular type.
- An object is an instance of a class, with its own associated state and the behaviour described by that class.
Q2. Explain state, behaviour and encapsulation, giving one distinct point for each term. [3 marks]
- State is the information describing an object at a particular time, represented by the values associated with its instance variables.
- Behaviour is the set of operations associated with the object. Member methods contain the instructions that express those operations in the program.
- Encapsulation combines related data and the methods operating on that data within a single unit, so state and behaviour are organised together.
Q3. A mobile-phone model represents model, colour and price as data, and storing a number as an operation. Identify its data attributes, identify its behaviour, state their program representations, and explain the class/object relationship for this model. [4 marks]
- The data attributes are model, colour and price. They describe information associated with an individual mobile-phone object.
- Storing a number is the behaviour supplied in the question. It describes an operation rather than a descriptive data value.
- Member variables represent the data attributes, while a member method represents the operation of storing a number.
- The mobile-phone class provides the common specification; a particular mobile-phone object is an instance of that class with its associated state.
Q4. Distinguish a primitive type from a class type using these three bases: nature of the type, contents of a variable, and programmer-defined members. [3 marks]
- A primitive type is a basic value type supplied by Java. A class type is described by a class definition that provides a common specification for objects.
- A primitive variable holds a primitive value. A variable of a class type holds an object reference or null, rather than containing the complete object.
- A primitive value has no programmer-defined instance fields or methods within it. A class can declare related fields and methods as its members.
Q5. Explain why a class may be regarded as a blueprint and viewed as an object factory. Include the common specification, instantiation, reuse of the definition, individual state and abstraction. [5 marks]
- A class provides the common specification for its objects. It identifies the member data and methods that describe the type, which explains the blueprint comparison.
- Instantiation creates a particular object from the class. The resulting object is an instance, rather than another name for the common class definition.
- The same class definition can be used to create further objects of that type. This repeatable relationship explains why the class may be viewed as an object factory.
- Each object has its own associated instance-variable values. Objects can therefore share a class description without being required to have identical state.
- The class is also an abstraction because it brings together essential common features and behaviour. It need not represent every detail of the corresponding real-life entities.
Q6. In the syntax pattern “ClassName referenceName = new ClassName();”, ClassName denotes an instantiable class with an accessible no-argument constructor, and referenceName denotes a new local variable. Explain the type, variable, new expression, constructor call, assignment and final relationship between variable and object. [6 marks]
- ClassName specifies the declared class type of the variable. It identifies the class whose instances the declaration is intended to reference in this pattern.
- referenceName introduces the local reference variable. Declaring that name establishes a variable of the stated type; the name itself is not the object.
- The expression beginning with new creates an instance of ClassName. Object creation is an additional action beyond declaring a variable of that class type.
- The creation expression invokes the accessible no-argument constructor to initialise the new object. The empty parentheses indicate that no arguments are supplied to it.
- The assignment symbol places the reference produced by the creation expression into referenceName. Thus the declaration also explicitly initialises the variable.
- After this statement completes normally, referenceName refers to the newly created object. The variable holds the reference, while the object has its associated instance data.
Q7. For this question, referenceName already refers to an object, and methodName is an accessible instance method taking no arguments. Explain the receiver, dot, parentheses and execution request in “referenceName.methodName();”. [4 marks]
- referenceName supplies the reference to the receiving object. The question establishes that it already refers to an object before the call is made.
- The dot selects the member named methodName through that reference. Here the selected member is the instance method specified in the question.
- The parentheses mark the call and contain no arguments. This matches the stated condition that the method requires no supplied arguments.
- The statement requests execution of the method. Its output or effect cannot be inferred without its instructions, so the call alone does not establish a printed result.
Key takeaways
- A class describes a common type, while an object is a particular instance with associated state and behaviour.
- Member variables represent stored information, and member methods describe the operations associated with that information.
- A class abstracts essential common features and may be viewed as a factory for creating similar objects.
- Encapsulation combines related data and operations; it should be distinguished from the access restrictions used for data hiding.
- Primitive variables hold primitive values, while class-type variables hold object references or the special value null.
- Declaring a reference variable and creating an object are distinct actions, although a statement can combine them.
- An instance-method call selects an operation through an object reference and supplies any arguments required by that operation.
- A method's actual instructions determine its effect; changing state, returning a value and displaying output are different actions.
Test yourself
How is an attribute different from its value?
An attribute identifies a descriptive feature represented in the model. Its value gives the actual information associated with that feature for a particular object.
Why can objects of the same class have different state?
They share member definitions, but each object has its own associated instance-variable values. Sharing a type does not require every value to be identical.
What is the difference between abstraction and encapsulation?
Abstraction identifies essential features and leaves out unnecessary implementation details. Encapsulation combines related data and the operations on that data within a single unit.
Is String a primitive type in Java?
No. String is a Java class representing character strings, so it is a class type rather than a primitive type.
What does null mean for an object reference?
It is the special reference value indicating that the reference does not refer to an object.
Does declaring a variable of a class type create an instance?
No. The declaration introduces a reference variable. Creating an object is a separate action, commonly expressed using new with a constructor invocation.
What does the dot do in an instance-method call?
It selects a member through the reference preceding it. The method name and parentheses then specify the requested method call.
Why does a method call not necessarily display output?
The instructions in the method determine its effect. It may work with state or return a value without writing any visible output.
