Model G20 2027 at FLAME University, registrations now open

Encapsulation | ICSE Class 10 Computer Applications Notes

25 min read

On this page

This note covers encapsulation, classes and objects, access specifiers, private, protected and public visibility, the scope of variables, class variables, instance variables, argument variables, local variables, and the identification of variables from their declarations.

What is encapsulation in Java?

A class is a definition from which objects are created. An object is an instance of a class. Java is the programming language used here. A variable is a named storage location for a value, and a method defines an operation.

Definition: Encapsulation is the grouping of related data and the methods that operate on that data within a class. Access controls help determine which parts of that class other code may use directly.

The values held by an object's variables describe its state. Its methods define its behaviour. A class therefore brings together information and the operations associated with that information. These are connected parts of the same definition, rather than unrelated pieces of a program.

How are members connected?

A member is a component declared as part of a class. A variable declared directly in a class is a field, also called a data member. A method declared in the class is a member method. Fields are distinct from variables declared inside methods.

Data hiding means restricting direct access to data from other code. Encapsulation and data hiding are closely related, but the terms emphasise different ideas. Encapsulation groups data and operations; data hiding controls how the data can be reached.

An access specifier, also called an access modifier, controls permission to access a declaration. The Java keywords private, protected and public express different permissions. A keyword is a word reserved by the language for a particular purpose.

Why can methods be useful boundaries?

A method can offer an authorised operation without exposing the field directly to the calling code. Calling code means the code that asks a method to execute. A method call requests that operation; it does not itself grant access to every field used inside the method.

This arrangement lets the class organise how its state is read or changed. A method can check a proposed change before storing it. Such checking depends on the method's instructions: placing data inside a class does not automatically validate values.

When explaining encapsulation, identify both the data and the operations grouped with it. When explaining data hiding, identify the access restriction. These complementary explanations are more precise than describing encapsulation simply as making everything private.

How do private and public access specifiers work?

A declaration introduces a program element, such as a field or method, and specifies relevant properties. In a member declaration, an access specifier controls access to that member. It does not by itself describe whether the member stores data or performs an operation.

Rule: A private member is accessible within its declaring class. A public member allows access from other classes where the declaring class itself is accessible and the member is used correctly.

What does private restrict?

For separate classes, private prevents another class from directly accessing the member. A method in the declaring class can still use that field. Thus a private field remains available for the class's own operations; private does not mean unusable.

The declaring class is the class containing the member's declaration. A separate class does not gain direct access to a private member merely by having an object of the declaring class. Possessing an object and having permission to access a particular member are different conditions.

A public method can read or change a private field within its own class. The caller uses the method, while the field access takes place inside the declaring class. Whether a value is returned or changed depends on the method's definition.

What does public permit?

Public provides broader access than private. A public method can form part of the operations offered to other code. A public field permits direct access where the class and member are otherwise accessible; public does not automatically make that field read-only.

Read-only describes something that can be read but cannot be changed through the relevant operation. Access permission and permission to reassign a variable are separate matters. Do not explain public as meaning constant, or private as meaning a value cannot change.

QuestionPrivate memberPublic member
Can its declaring class use it?Yes, subject to ordinary usage rules.Yes, subject to ordinary usage rules.
Can a separate, unrelated class use it directly?No.Yes, if the declaring class is accessible.
Does this access word make a value unchangeable?No.No.

These comparisons concern separate top-level classes, meaning classes not declared inside another class. Java also permits nested classes, meaning classes declared inside other classes; private access within the same enclosing top-level class is a special case beyond this simple comparison.

Choose the explanation that matches the declaration being discussed. An answer about a private field should state the field restriction and the permitted access within its class. An answer about a public method should describe the method's accessibility, without assuming that its fields are public too.

What does protected access mean, and how do the access levels compare?

A package groups related Java classes under a package name. A subclass is a class derived from another class through inheritance. Inheritance is the relationship through which a derived class acquires eligible members of a base class.

The base class, also called the superclass, is the class from which the subclass is derived. These terms are needed to describe protected access accurately. Package membership and the subclass relationship are separate facts; one does not automatically imply the other.

Rule: A protected member is accessible within its package. It is also accessible from subclass code outside that package, subject to Java's restrictions on how inherited protected members are accessed.

What is the same-package rule?

Code in another class in the same package may access a protected member, even if that other class is not a subclass. Therefore, defining protected as “accessible only in subclasses” leaves out an important part of its visibility.

A separate, unrelated class in a different package does not gain access to a protected member. Protected therefore does not provide the same general accessibility as public. The package and inheritance relationship must both be considered.

What qualification applies across packages?

An object reference is a value that refers to an object. Its declared type specifies the kind of value the variable can hold. Being inside subclass code does not permit accessing a protected instance field through every possible superclass reference.

For a protected instance member, meaning a member associated with an object, subclass access across packages is restricted. Within the accessing subclass, access through an object reference must use a reference whose type is that subclass or a subclass of it.

Accessing locationPrivateProtectedPublic
Within the declaring classPermittedPermittedPermitted
Separate class in the same packageNot permittedPermittedPermitted
Subclass in another packageNo direct accessPermitted with protected-access restrictionsPermitted
Unrelated class in another packageNot permittedNot permittedPermitted

The table assumes that the declaring class itself is accessible wherever necessary. It describes member access, rather than the separate rules governing which modifiers may be used on a top-level class.

Java also has package access when a member is declared without an access modifier. Such a member is accessible within its package. There is no access modifier keyword named default to insert into that declaration; omission produces this access level.

Keep this distinction clear when reading declarations. An omitted access modifier does not mean public, and protected is not simply another name for package access. The additional subclass provision is what extends protected access beyond its package.

How do scope and visibility differ?

Definition: The scope of a declaration is the region of program text in which its declared name can be used to refer to it, subject to name hiding by another declaration. Accessibility concerns permission to access it.

Visibility is commonly used when describing whether a variable or member can be used at a particular place. A precise explanation separates the variable's scope from the member's accessibility. A declaration's position and its access specifier answer different questions.

What does declaration position tell you?

A field is declared in the class body, outside method bodies. A local variable is declared within a method or another local block. An argument variable is declared in a method's parameter list. These positions help identify which scope rules apply.

A block is a group of declarations and statements enclosed by braces. The symbol { opens a block and } closes it. A statement is an instruction in a program. A method body is the block containing that method's instructions.

A parameter list is the list in a method declaration that describes values the method receives. Each listed parameter has a type and a name. Its role differs from that of a field even when the two use the same data type.

What can hide a name?

Shadowing occurs when a declaration with the same name causes another declaration not to be selected by an unqualified use of that name. An unqualified name is used without an object or class name in front of it.

A parameter or local variable can shadow a field. This does not remove the field or change its access modifier. The keyword this refers to the current object within an instance context and can distinguish that object's field from a same-named local variable or parameter.

An instance context is code executed in relation to a particular object. It contrasts with a static context, which has no current object supplied by the method call itself. The distinction matters when deciding how an instance field can be accessed.

Note: Scope, access and lifetime are different ideas. Scope concerns program text; access concerns permission; lifetime concerns how long a particular variable exists during execution.

Use a consistent reading order: find the declaration, identify its category, locate the code attempting to use it, and then apply access and usage rules. Looking only for public or private cannot determine the scope of a local variable.

What are class variables, and what does static change?

A class variable is a field declared with the keyword static. It belongs to the class rather than separately to each object. Static describes this association; it is not an access specifier and does not mean public.

Rule: A static field is shared by the objects of its class. Creating another object does not give that object a separate copy of the class variable.

How do access and sharing combine?

A class variable may have private, protected or public access. These access words determine which code may use the field directly. Static answers a different question: whether the field is associated with the class or separately with individual objects.

A private static field is therefore a class variable with restricted direct access. Its sharing does not make it public. A public static field combines class association with broader accessibility. Each word contributes its own meaning to the declaration.

Access to a class variable is commonly written using the class name. The dot operator, written as a full stop, separates the qualifying class or object expression from the member name. Access permission still applies when a class name is used.

How are static methods related?

A static method is associated with a class and can be called without choosing an object as its receiver. The receiver is the object on which an instance method is called. A static method has no current receiver supplied in that way.

A static method can directly use an accessible static field of its class. To use an instance field, it needs a suitable object reference. This requirement is about which object's field is intended, as well as the separate question of whether access is permitted.

Property to identifyWhat to inspectConclusion
Class associationA field declaration containing staticThe field is a class variable.
Access permissionThe access modifier on that fieldPrivate, protected or public controls accessibility.
Declaration categoryIts position directly in the class bodyThe variable is a field rather than a method-local variable.

A static context does not have this available as a reference to a current object. Do not infer that a static method cannot use any object at all: it can use an object reference that it obtains or receives, subject to the access rules.

When classifying a class variable, mention both its position as a field and the static keyword. Describing a variable merely as “inside the class” is insufficient, because instance variables are also declared there.

What are instance variables, and how do they differ from class variables?

An instance variable is a field declared without static. Each object has its own instance variable storage. Objects created from the same class follow the same field declarations, but the values held in their instance fields may differ.

Rule: Instance variables describe the state associated with particular objects. Class variables describe storage associated with the class and shared by its objects.

What does an object-specific value mean?

Changing an instance field of one object does not, by that action alone, change the corresponding instance field of a different object. The field name is defined by the class, but the object determines which instance field is being used.

Separate instance variables can contain references to the same object. Separate field storage therefore does not guarantee that all objects reachable through those fields are different. Keep the claim about separate storage distinct from a claim about copying an entire object.

Within an instance method, meaning a method called in relation to an object, a field name can refer to that object's field where the name is not shadowed. The current object supplies the context for the access.

Which properties should you compare?

Basis of comparisonClass variableInstance variable
Field declarationContains staticDoes not contain static
AssociationWith the classWith a particular object
Storage across objectsShared class-variable storageSeparate instance-variable storage
Access controlMay be private, protected or publicMay be private, protected or public
Use from a static methodAccessible static fields can be used directlyRequires a suitable object reference and permitted access

Access modifiers do not distinguish class variables from instance variables. Private can apply to either kind of field. To distinguish the categories, inspect static; to distinguish their direct accessibility, inspect the access modifier.

Instance fields are not local variables simply because a method uses them. Their declarations remain part of the class. A method can work with an instance field and a local variable during the same execution, but the variables retain different roles.

When tracing changes to state, first establish the object identity, meaning which particular object is involved. Then identify the field being accessed. A shared class field and an object-specific instance field require different interpretations, even when both are used in the same method.

A complete comparison therefore covers declaration, association and storage, while keeping access separate. This prevents the common mistake of treating static as a synonym for public or treating every private variable as an instance variable.

What are argument variables, and how do they receive values?

An argument variable, or formal parameter, is a variable declared in a method's parameter list. It receives a value when the method is called. An actual argument is the value or expression supplied at that call.

An expression is a combination of values, names or operations that produces a value. An argument expression is evaluated at the call, and the resulting value is passed to the corresponding parameter. The parameter is the receiving variable, not the call itself.

Rule: A method's parameter is available within that method's body. It is not a field shared by other methods merely because those methods belong to the same class.

How does a parameter differ from a local variable?

A parameter obtains its initial value from the method call. A local variable declared in the method body must receive a value through the method's instructions before its value is read. Declaration position and the way the initial value arrives distinguish these roles.

Different method calls have their own parameter variables. Passing a value to a method does not turn the parameter into an instance field or class field. Its association remains with the particular invocation, meaning that particular execution of the method.

What does passing a value mean in Java?

Java uses pass by value: the parameter receives a copy of the supplied value. Reassigning the parameter makes that parameter hold another value; it does not reassign the caller's variable. Reassignment means storing a new value in an existing variable.

A primitive value is a value of a basic Java type rather than an object reference. When such a value is passed, the receiving parameter holds its copied value. Changes to that parameter are changes to the receiving variable.

If the argument is an object reference, the copied value is the reference. The caller and method may consequently refer to the same object. Permitted changes to that object's state are different from reassigning the parameter to refer elsewhere.

A parameter can have the same name as a field. Within the method body, the parameter then shadows that field for unqualified uses of the name. In an instance method, this can be used to identify the current object's field explicitly.

For identification questions, give the declaration-based reason: the variable appears in the parameter list and receives a value from the call. Calling it “a variable used inside a method” is too broad, because fields and local variables can also be used there.

How do local variables work, and how should variables be identified?

A local variable is declared within a method body or another local block. It serves the work of that region of code. It is not a field, even though the method containing it belongs to a class.

Rule: For an ordinary local variable declared in a block, scope begins at its declaration and extends through the remainder of that block. Its name cannot be used to reach it from outside that scope.

What do block boundaries change?

A block may occur inside another block. A local variable declared in an inner block is not available by that name in the surrounding block after the inner block ends. The enclosing method's continued execution does not extend the inner variable's scope.

A local variable declared in an outer block can be used in an inner block within its scope. However, Java does not permit redeclaring a method parameter or local variable with the same name in an overlapping local scope within that method.

Local variables must be definitely assigned before their values are read. This means Java's checking rules must establish that a value has been assigned along every relevant route to the read. A declaration alone need not establish such a value.

Initialisation means providing a variable's initial value. Local variables do not acquire a usable default value merely because they have been declared. Do not transfer assumptions about field initialisation to a local variable in a method.

How can declaration evidence guide classification?

  1. Locate the declaration rather than a later use of the variable. A use inside a method does not prove that the variable is local.
  2. If the declaration is directly in the class body, identify it as a field. Check static to distinguish a class variable from an instance variable.
  3. If the declaration occurs in a method's parameter list, identify it as an argument variable or formal parameter.
  4. If the declaration occurs within the method body or a local block, identify it as a local variable and locate the enclosing scope boundary.
  5. For fields, inspect the access modifier separately. State both the variable category and access level when both are requested.
Variable categoryDeclaration evidenceMain distinction
Class variableField declared with staticStorage associated with the class
Instance variableField declared without staticStorage associated with each object
Argument variableDeclaration in a parameter listReceives a value for an invocation
Local variableDeclaration inside a method body or local blockUse restricted by its local scope

Private, protected and public are not valid access modifiers for ordinary local variables or method parameters. Static is not used to declare an ordinary method-local variable in Java. These words must be interpreted in the declaration contexts where Java permits them.

Finish an explanation by separating category, scope and access. Category describes the variable's role; scope locates where its name can be used; access restricts permitted use of members. Keeping these questions separate makes an identification both precise and complete.

Glossary

  • Encapsulation — Grouping related data and the methods operating on that data within a class.
  • Data hiding — Restricting direct access to data so that other code uses permitted operations.
  • Class — A definition specifying the fields and methods associated with objects created from it.
  • Object — An instance of a class, with its own identity and associated instance state.
  • Field — A variable declared directly in a class, outside method bodies.
  • Access specifier — A modifier controlling which code is permitted to access a declared member.
  • Scope — The region of program text where a declaration's name can refer to that declaration, subject to shadowing.
  • Package — A named grouping of Java classes relevant to access permissions between classes.
  • Subclass — A class derived through inheritance from another class, called its superclass.
  • Class variable — A field declared static, with storage associated with the class rather than each object.
  • Instance variable — A non-static field with separate variable storage associated with each object.
  • Argument variable — A formal parameter declared in a method's parameter list that receives a supplied value.
  • Local variable — A variable declared in a method body or local block, with locally bounded scope.
  • Shadowing — A name-resolution situation where another declaration prevents an unqualified name from selecting a field or other declaration.
  • Initialisation — Providing the initial value that a variable holds before subsequent changes occur.

Common errors and misconceptions

  • Misconception: Encapsulation means making every member private. Correct: Encapsulation groups data and operations. Access control can hide fields while leaving selected methods available to other code.
  • Misconception: A private field cannot be changed. Correct: Private controls accessibility. Code within the declaring class can change a private field where reassignment is otherwise permitted.
  • Misconception: Protected members are available only to subclasses. Correct: They are also accessible to other classes in the same package. Cross-package subclass access has additional restrictions.
  • Misconception: Static means public. Correct: Static specifies class association. A static field can have private, protected or public access; accessibility and sharing answer different questions.
  • Misconception: Every variable used inside a method is local. Correct: A method can use fields, parameters and local variables. Inspect the declaration to identify the category.
  • Misconception: A local variable remains accessible throughout the class. Correct: Its scope is bounded by its declaration and enclosing local region, not by the whole class.
  • Misconception: Passing an object means Java passes the caller's variable by reference. Correct: Java copies the reference value. Changing the referenced object differs from reassigning the caller's variable.
  • Misconception: Declaring a local variable gives it a default value that can immediately be read. Correct: Its value must be definitely assigned before a read is permitted.

Exam-style questions with model answers

Q1. Define encapsulation and distinguish it from data hiding in Java. [2 marks]
  1. Encapsulation groups related data and the methods operating on that data within a class.
  2. Data hiding restricts direct access to data. It supports controlled use of encapsulated data, but is not the whole meaning of encapsulation.
Q2. Explain private, protected and public member access for separate top-level Java classes. Assume the declaring class is accessible wherever necessary. Include the qualification on cross-package protected instance-member access. [3 marks]
  1. Private: A member is accessible within its declaring class, but not directly from another separate top-level class, even if that class has an object of the declaring class.
  2. Protected: Access is permitted within the same package and from subclasses outside it. Across packages, instance-member access through a reference requires the accessing subclass type or one of its subclasses.
  3. Public: The member is accessible from other classes where the declaring class is accessible and ordinary member-usage requirements are satisfied.
Q3. Compare class variables and instance variables under these four headings: declaration, association, storage across objects, and access control. [4 marks]
  1. Declaration: A class variable is a field declared with static. An instance variable is a field declared without static; both are declared directly in the class body.
  2. Association: A class variable belongs to the class, whereas an instance variable belongs to a particular object created from that class.
  3. Storage: Class-variable storage is shared across the objects of the class. Each object has separate storage for its instance variables.
  4. Access control: Both categories can be private, protected or public. The access modifier does not determine whether a field is shared or object-specific.
Q4. Explain five distinctions needed when identifying class, instance, argument and local variables: declaration location, static, parameter values, local scope, and access modifiers. [5 marks]
  1. Declaration location: Fields are declared directly in the class body. Argument variables are declared in a method's parameter list, while local variables are declared within a method body or local block.
  2. Static: A field containing static is a class variable. A field without static is an instance variable, so this keyword distinguishes the two field categories.
  3. Parameter values: An argument variable receives a value supplied by a method call. It is associated with that invocation rather than being a field of the class.
  4. Local scope: An ordinary block-local variable is available from its declaration through the rest of that block, subject to Java's name-use rules.
  5. Access modifiers: Private, protected and public describe member accessibility. They do not replace variable categories and cannot be applied to ordinary local variables or method parameters.
Q5. Distinguish scope from accessibility, and explain why a public method can use a private field in its own class without making that field public. [3 marks]
  1. Scope concerns the region of program text in which a declaration's name can refer to it. The declaration's position helps determine that region.
  2. Accessibility concerns whether access to a member is permitted. A private field remains directly restricted even when a public method exists in its class.
  3. The public method's field access occurs inside the declaring class, where private access is permitted. Calling that method does not grant the caller direct access to the field.
Q6. Explain six Java rules about method parameters and ordinary local variables: declaration position, source of initial values, parameter scope, block-local scope, access modifiers, and reassignment of a parameter. [6 marks]
  1. Declaration position: A method parameter is declared in the parameter list. An ordinary local variable is declared in the method body or a local block.
  2. Initial values: Parameters receive values from the call. Local variables must be assigned before their values are read; a declaration alone does not guarantee a readable value.
  3. Parameter scope: A method's parameter can be used within that method's body. It does not become a field available to other methods.
  4. Block-local scope: A variable declared in an ordinary block has scope from its declaration through the remainder of that block, not throughout the class.
  5. Access modifiers: Private, protected and public are member access modifiers. They are not valid access modifiers for ordinary local variables or method parameters.
  6. Parameter reassignment: Java passes values by copying them into parameters. Reassigning the parameter does not reassign the caller's variable, even when the copied value is an object reference.

Key takeaways

  • Encapsulation groups related data and methods within a class; data hiding restricts direct access to the grouped data.
  • Private, protected and public control member access, while the declaration's position helps determine the scope of a variable.
  • Protected access includes the same package and qualified access from subclasses outside that package; it is broader than package access.
  • Static fields are class variables shared across objects; fields without static are instance variables associated with particular objects.
  • Argument variables receive values from method calls, while ordinary local variables are declared within method bodies or local blocks.
  • Read the declaration before classifying a variable, because using a field inside a method does not make it local.
  • Java passes argument values by copying them; copying an object reference can leave caller and method referring to the same object.
  • Local variables require definite assignment before reading, and their accessibility does not extend beyond the region of their local scope.

Test yourself

What two things does encapsulation bring together?

It brings related data and the methods operating on that data together within a class.

Does private prevent a field from being changed within its class?

No. Private restricts access; it does not itself prohibit changes made by permitted code within the declaring class.

Must another class in the same package be a subclass to access a protected member?

No. Protected access permits other classes in the same package to access the member without an inheritance relationship.

Which declaration feature distinguishes a class variable from an instance variable?

The static keyword distinguishes them: a static field is a class variable, while a non-static field is an instance variable.

Where is an argument variable declared, and where does its initial value come from?

It is declared in the method's parameter list and receives its initial value from the corresponding supplied argument.

What happens to the scope of a local variable declared in an inner block when that block ends?

Its scope ends with that block. The variable cannot be used by that name from the surrounding code after the block.

Does a local variable automatically have a value that may be read after declaration?

No. Java requires the variable to be definitely assigned before its value can be read.

What is copied when an object reference is passed to a Java method?

The reference value is copied into the parameter, so both references may refer to the same object.