JAVA UNIT-2 ANSWERS
1. Class and Object with Example
Class: A class is a blueprint or template from which objects are
created.
Object: An object is an instance of a class with its own values for
attributes.
Advantages:
1. Code reusability
2. Data hiding using encapsulation
3. Better organization of code
Example:
class Student {
String name;
int age;
void display() {
[Link](name + " " + age);
class TestStudent {
public static void main(String[] args) {
Student s1 = new Student();
[Link] = "John";
[Link] = 20;
[Link]();
2. Static Keyword
Static members belong to the class rather than an instance.
Used for variables, methods, blocks, and nested classes.
JAVA UNIT-2 ANSWERS
Benefits: Memory efficiency and common data sharing.
Example:
class Counter {
static int count = 0;
Counter() {
count++;
[Link](count);
public static void main(String[] args) {
new Counter();
new Counter();
new Counter();
3. Access Specifiers
public: accessible everywhere
private: accessible only within the same class
protected: accessible within package & subclasses
default: accessible within the same package
Example using public:
public class Demo {
public int x = 10;
public void show() {
[Link]("Value: " + x);
}
JAVA UNIT-2 ANSWERS
4. Types of Constructors
Default Constructor: No parameters, assigns default values.
Parameterized Constructor: Accepts parameters to initialize
objects.
Copy Constructor: Creates a new object as a copy of another.
Example:
class Demo {
Demo() { [Link]("Default Constructor"); }
Demo(int x) { [Link]("Parameterized: " + x); }
Demo(Demo d) { [Link]("Copy Constructor"); }
5. Method Overloading
Same method name, different parameter lists.
Advantage: Increases readability and code flexibility.
Example:
class MathOp {
int add(int a, int b) { return a + b; }
double add(double a, double b) { return a + b; }
public static void main(String[] args) {
MathOp m = new MathOp();
[Link]([Link](5, 10));
[Link]([Link](2.5, 3.5));
6. Accessing Private Members
Direct access not possible outside the class.
Solution: Use getter and setter methods.
JAVA UNIT-2 ANSWERS
Example:
class Test {
private int data = 40;
public int getData() { return data; }
7. Recursion
A method calling itself repeatedly until a base condition is met.
Advantages: Compact code, easy to solve problems like factorial,
Fibonacci.
Example:
class RecursionDemo {
int fact(int n) {
if(n==0) return 1;
return n * fact(n-1);
public static void main(String[] args) {
RecursionDemo r = new RecursionDemo();
[Link]([Link](5));
8. Pass-by-Value vs Pass-by-Reference
Pass-by-Value: Copy of variable value is passed.
Pass-by-Reference: Java does not support it directly; reference
copy is passed for objects.
Example:
class Test {
void change(int x) { x = 50; }
JAVA UNIT-2 ANSWERS
public static void main(String[] args) {
int a = 10;
Test t = new Test();
[Link](a);
[Link](a); // prints 10
9. Final Keyword
Used for constants (final int x=10;),
Prevent method overriding (final void method(){}),
Prevent inheritance (final class A{}).
Example:
final class Test {}
final int x = 100;
10. Static vs Non-Static Inner Class
Non-Static Inner Class: Needs outer class object.
Static Inner Class: Does not need outer class object.
Example:
class Outer {
class Inner { void show(){ [Link]("Non-static inner"); } }
static class SInner { void show(){ [Link]("Static inner"); } }
public static void main(String[] args) {
[Link] obj1 = new Outer().new Inner();
[Link]();
[Link] obj2 = new [Link]();
[Link]();
JAVA UNIT-2 ANSWERS
}