- Home
- Chapter 1 - Object Oriented Programming Concepts
- Object Oriented Programming Concepts
- Multiple Choice Questions
- State whether the given statements are True or False
- Assignment Questions
- Chapter 2 - Introduction to Java
- Introduction to Java
- Multiple Choice Questions
- Assignment Questions
- Chapter 3 - Values and Data Types
- Values and Data Types
- Multiple Choice Questions
- State whether the given statements are True or False
- Assignment Questions
- Chapter 4 - Operators in Java
- Operators in Java
- Multiple Choice Questions
- State whether the given statements are True or False
- Assignment Questions
- Chapter 5 - User-Defined Methods
- User-Defined Methods
- Multiple Choice Questions
- State whether the given statements are True or False
- Assignment Questions
- Chapter 6 - Input in Java
- Input in Java
- Multiple Choice Questions
- Assignment Questions and Programs
- Chapter 7 - Mathematical Library Methods
- Mathematical Library Methods
- Multiple Choice Questions
- Assignment Questions
- Chapter 8 - Conditional Constructs in Java
- Conditional Constructs in Java
- Multiple Choice Questions
- Assignment Questions and Programs
- Chapter 9 - Iterative Constructs in Java
- Iterative Constructs in Java
- Multiple Choice Questions
- State whether the given statements are True or False
- Assignment Questions and Programs
- Chapter 10 - Nested for loops
- Nested for loops
- Assignment Questions and Programs
- Chapter 11 - Constructors
- Constructors
- Multiple Choice Questions
- Assignment Questions and Programs
- Chapter 12 - Library Classes
- Library Classes
- Multiple Choice Questions
- Assignment Questions
- Chapter 13 - Encapsulation and Inheritance
- Library Classes
- Multiple Choice Questions
- Assignment Questions
- Chapter 14 - Arrays
- Library Classes
- Multiple Choice Questions
- Assignment Questions
- Chapter 15 - String Handling
- Library Classes
- Multiple Choice Questions
- Assignment Questions
Encapsulation and Inheritance in Java
Chapter 13
Encapsulation and Inheritance in Java
Class 10 - Logix Kips ICSE Computer Applications with BlueJ
![]() Share with a Friend |
Assignment Questions and Answers
- public
- private
- protected
- public
- private
- protected
- no modifier specified
- public — The class members declared with the public access specifier can be accessed from outside the class.
- private — The class members declared with the private access specifier can be accessed only within the class in which they are declared.
- protected — The class members declared with the protected access specifier can be accessed within the same package only. They are not accessible from outside the package, except if the sub class is in the other package.
- no modifier specified — When no access specifier is mentioned, then by default that members of a class are public within the package. They can be accessed within the same package but not outside the package.
- It hides the implementation details of the class from other objects, which makes it easier to change the implementation without affecting the rest of the system.
- It provides encapsulation as the class defines a clear and well-defined interface for interacting with its state.
- It protects the state of the object by preventing external objects from modifying the state in an unintended way.
- Local variables
- Parameter variables
- Instance variables
- Class variables
- Local variables — The scope of local variables is limited to the method or the block they are declared in. So, local variables are accessible only to the method or block in which they are declared. They are not accessible to the rest of the class.
- Parameter variables — The scope of parameter variables is limited to the method where they are being used. Therefore, parameter variables are visible only to the method in which they are used. They are not accessible to the rest of the class.
- Instance variables — The scope of an instance variable is within the object that it belongs to, and it can be accessed from any method of the class that it belongs to, within the visibility restrictions defined by its access modifiers.
- Class variables — A class variable can be accessed from any instance of the class using the class name followed by the variable name. It is shared among all instances of a class. It is available as a single copy to all instances of the class.
Question 1
How does a class encapsulate state and behaviour?
Answer
A class encapsulates state and behavior by combining data and functions into a single unit. The state of an object is represented by its member variables and behaviour is represented by member methods. By combining state and behavior within a single unit, the class encapsulates the implementation details, allowing the outside world to interact with the object through a well-defined interface.
Question 2
Name the access specifiers available in Java.
Answer
The access specifiers available in Java are:
Question 3
Explain visibility in terms of the following access modifiers:
Answer
Question 4
Why is it a good idea to make all instance variables private?
Answer
It is a good idea to make all instance variables private because:
Question 5
What do you mean by the scope of variables in Java?
Answer
The scope of a variable refers to that part of the program in which the variable is accessible.
Question 6
Explain the scope of the following variables in Java:
Answer
Question 7
Explain the scope of variables in blocks and sub-blocks.
Answer
A variable declared in a block is visible (accessible) in the block and in all the sub-blocks. However, it is not visible outside the block.
Consider the given example:
void Add() {
int a = 10;
int b = 20;
if(a < b) { //Block 1
int sum = a + b;
System.out.println(sum);
}
else { //Block 2
int diff = a - b;
System.out.println(diff);
}
}
Here, the variables a and b will be accessible throughout the block and its sub-blocks (Block 1 and Block 2) but the variable sum will be accessible only in Block 1 and the variable diff will be accessible only in Block 2.
Question 8
Is it legal to define local variables with the same identifier in nested blocks?
Answer
No, it is illegal to define local variables with the same identifier in nested blocks
The local variable declaration space of a block includes any nested blocks. Thus, within a nested block it is not possible to declare a local variable with the same name as a local variable in an enclosing block.
Question 9
Why do you need to use static variables in Java?
Answer
Static variables in Java are used when we want to create a variable that is common to all objects of a class, rather than having a separate instance of the variable for each object. This is useful in situations where we need to keep track of information that applies to a class as a whole, rather than to individual objects.
Some common use cases for static variables include:
- Counter to keep track of the number of instances of a class that have been created.
- To store a constant value that is common to all objects of a class.
- To store configuration information that is common to all objects of a class.
Question 10
What is the mechanism that allows one class to extend another class?
Answer
Inheritance is the mechanism that allows one class to extend another class.
Question 11
What do you call a class that is an extension of another class?
Answer
A class that is an extension of another class is known as a derived class or child class or sub-class.
Question 12
What does the inheritance mechanism allow one class to acquire from another?
Answer
The inheritance mechanism allows the derived class to inherit the state and behaviour from the base class. The private data members and member methods are not inherited by the sub class.
Question 13
How is inheritance transitive? Explain.
Answer
The transitive nature of inheritance means if class B is derived from class A, class B will inherit all the properties of class A. Now all the sub classes of class B will also be able to inherit properties of class A because of the transitive nature of inheritance.
Question 14
Explain various types of inheritance.
Answer
The various types of inheritance are as follows:
- Single Inheritance— When a class is derived from only one base class, it is known as single inheritance.
- Multiple Inheritance— When a sub class is inherited from multiple base classes, it is known as multiple inheritance.
- Hierarchical Inheritance— When many sub classes are inherited from a single base class, it is known as hierarchical inheritance.
- Hybrid Inheritance— When more than one type of inheritance forms are used together, it is known as hybrid inheritance.
- Multilevel Inheritance— When a sub class is inherited from a class that itself is being inherited from another class, it is known as multilevel inheritance.
Question 15
Why do you need to use inheritance? Give two reasons.
Answer
We need to use inheritance for the following reasons:
- Inheritance divides a program into useful and reusable set of classes.
- Changes made in the original class are reflected in all the inherited classes.
Question 16
Declare a public class CoolClass.
- Write the header for a public member method CoolMethodA.
- Write the header for an integer member method CoolMethodB that should be accessible to the classes in the package but not to derived classes.
- Write the header for an integer member method CoolMethodC that should be accessible only to other methods of the class.
- Write the header for a character member method CoolMethodD that should be accessible to the classes in the package and any derived classes.
Answer
public class CoolClass
{
public void CoolMethodA() {
}
int CoolMethodB() {
}
private int CoolMethodC() {
}
protected char CoolMethodD() {
}
}
