0% found this document useful (0 votes)
5 views4 pages

Java Chapter 4

Unit 4 covers Object Oriented Programming concepts including classes, objects, constructors, access modifiers, and methods in Java. It explains the definitions, syntax, and examples of classes and objects, the creation of objects using the new keyword, and the differences between constructors and methods. Additionally, it discusses method overloading, garbage collection, and inner classes, highlighting their advantages and functionalities.

Uploaded by

Krish Patel
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views4 pages

Java Chapter 4

Unit 4 covers Object Oriented Programming concepts including classes, objects, constructors, access modifiers, and methods in Java. It explains the definitions, syntax, and examples of classes and objects, the creation of objects using the new keyword, and the differences between constructors and methods. Additionally, it discusses method overloading, garbage collection, and inner classes, highlighting their advantages and functionalities.

Uploaded by

Krish Patel
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Unit 4: Classes and Objects Object Oriented Programming

Object Oriented Programming


Unit 4: Theory Questions & Answers (5 Marks)

Topic 1: Classes and Objects

Q1. Define Class and Object in Java. Explain with syntax and an example.
Answer:

1. Class (The Blueprint): A class is a template that defines the properties (variables) and
behaviors (methods) common to all objects of that kind. A class does not occupy memory
until an object is created.

2. Object (The Real Entity): An object is an instance of a class. It is a real-world entity


that has a state (data) and behavior (functionality). It occupies memory in the heap area.

Example Program:
class Car {
String color = "Red"; // Property
void drive() { // Method
[Link]("Car␣is␣driving");
}
}
public class Main {
public static void main(String args[]) {
Car myCar = new Car(); // Creating an Object
[Link]([Link]);
[Link]();
}
}

Q10. Explain how to create an object in Java. Discuss the new keyword and dot operator.
Answer:

1. Creating an Object: Syntax: ClassName obj = new ClassName();

2. The new Keyword:

• It allocates memory for the object in the Heap area at runtime.


• It returns a reference to that memory and invokes the constructor.

3. The Dot (.) Operator: It is used to access the variables and methods of the object (e.g.,
[Link]).

1
Unit 4: Classes and Objects Object Oriented Programming

Topic 2: Constructors

Q2. What is a Constructor? Explain its rules and different types.


Answer: Definition: A constructor is a special method used to initialize an object. It is called
automatically when an object is created.
Rules:
• Must have the same name as the class.

• Must not have a return type (not even void).

• It cannot be private (usually public).

Types:
1. Default Constructor: A constructor that takes no parameters.
Example: Bike() { [Link]("Bike created"); }

2. Parameterized Constructor: Accepts parameters to initialize variables.


Example: Bike(int speed) { [Link] = speed; }

Q7. Explain Constructor Overloading with an example.


Answer: Definition: Constructor Overloading is the technique of having multiple constructors
in the same class with different parameter lists.
class Box {
int width, height;
// Constructor 1: No arguments
Box() { width = 0; height = 0; }
// Constructor 2: Two arguments
Box(int w, int h) { width = w; height = h; }
}

Q9. Differentiate between Constructor and Method.


Answer:

Feature Constructor Method


Name Same as Class Name. Any valid name.
Return Type No return type. Must have return type.
Invocation Automatic (with new). Manual (with dot operator).
Purpose Initialize object. Perform specific task.

2
Unit 4: Classes and Objects Object Oriented Programming

Topic 3: Keywords and Access

Q3. Explain Access Modifiers in Java.


Answer: Definition: Keywords that set the visibility of classes, variables, and methods.

Modifier Class Pkg Subclass World


Private Yes No No No
Default Yes Yes No No
Protected Yes Yes Yes No
Public Yes Yes Yes Yes

Q5. Explain the this keyword in Java. Why is it used?


Answer: Definition: The this keyword is a reference variable that refers to the current ob-
ject.
Main Use: To resolve name conflicts when a class instance variable and a constructor parame-
ter have the same name.
class Student {
int id;
Student(int id) {
[Link] = id; // Assigns parameter to instance
var
}
}

Topic 4: Methods and Advanced Concepts

Q4. Explain Method Overloading with an example.


Answer: Definition: Allowing a class to have more than one method with the same name, but
different parameter lists. It increases readability.
class MathHelper {
void add(int a, int b) { ... }
void add(int a, int b, int c) { ... }
}

Q6. What is Garbage Collection? Explain finalize().

3
Unit 4: Classes and Objects Object Oriented Programming

Answer: Garbage Collection: The process of automatically deleting unreferenced objects


from Heap memory to manage memory efficiently.
finalize(): A method of the Object class called by the Garbage Collector just before destroying
an object to perform cleanup.

Q8. What is an Inner Class? Mention its advantages.


Answer: Definition: A class declared inside another class.

• Access: Can access all members (even private) of the outer class.

• Organization: Groups classes logically.

• Code: Reduces code length.

You might also like