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

Java Arrays and Inheritance Explained

Uploaded by

rajak2801v
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)
10 views4 pages

Java Arrays and Inheritance Explained

Uploaded by

rajak2801v
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

Q1. Explain Arrays in Java.

Definition:
An array is a collection of similar data type elements stored in continuous memory locations.
It is an object in Java and its size is fixed once created.

Features:
1. Stores multiple values in a single variable.
2. Elements are accessed by index (0-based).
3. All elements must be of the same type.
4. Fast access due to index-based retrieval.
5. Size must be declared at creation time.

Types:
- One-dimensional (1D)
- Two-dimensional (2D)
- Multi-dimensional

Advantages:
- Reduces code (no need for multiple variables).
- Easy traversal with loops.
- Memory-efficient.
- Useful in searching and sorting.

Diagram (1D Array):


Index → 0 1 2 3
Value → 10 20 30 40

Q2. Explain the different forms of inheritance supported by Java.

Definition:
Inheritance allows one class (child) to acquire properties and behaviors of another class (parent).

Types:
1. Single Inheritance – One child inherits from one parent.
2. Multilevel Inheritance – Chain of inheritance (A → B → C).
3. Hierarchical Inheritance – One parent, multiple children.
4. Multiple Inheritance (via Interfaces).

Advantages:
- Promotes code reusability.
- Supports method overriding.
- Provides hierarchical relationships.

Diagrams:
- Single: A → B
- Multilevel: A → B → C
- Hierarchical: A → B, A → C
- Multiple: Interface A + Interface B → Class C

Q3. Explain Abstract Classes and Methods in Java.

Abstract Class:
- Declared with 'abstract' keyword.
- Cannot be instantiated.
- May contain abstract (without body) and concrete methods.

Abstract Method:
- Declared without body.
- Must be implemented in subclass.
- Syntax: abstract returnType methodName();

Features:
1. Provides partial abstraction.
2. Supports polymorphism.
3. Can have variables, constructors, and methods.
4. Enforces design in subclasses.

Advantages:
- Ensures subclasses follow a blueprint.
- Promotes code reuse.
- Useful in large systems with common design.

Note: If a class has at least one abstract method → the class must be abstract.

Q4. What is a Class in Java and how can classes be defined?

Definition:
A class is a blueprint for creating objects. It contains variables (data) and methods (behavior).

Features:
1. Defines attributes and behavior of objects.
2. Supports encapsulation.
3. Can be inherited.
4. Accessed by creating objects using 'new'.

Diagram:
Class: Student
--------------
id
name
display()

Object: s1
--------------
id = 101
name = "Raja"

Q5. Explain Constructors in Java.

Definition:
A constructor is a special method used to initialize objects. It has the same name as the class
and no return type. Called automatically when an object is created.

Features:
1. Called only once at object creation.
2. Can be overloaded.
3. No return type.
4. If not defined, Java provides a default constructor.
Types:
- Default Constructor – No arguments.
- Parameterized Constructor – Accepts parameters.
- Copy Constructor – Copies values of another object.

Advantages:
- Simplifies object initialization.
- Prevents uninitialized objects.
- Improves readability and reuse.

Difference between Constructor & Method:


Constructor – Initializes object, no return type, called automatically.
Method – Defines behavior, must have return type, called explicitly.

Q6. Explain One-Dimensional, Two-Dimensional, and Multi-Dimensional Arrays in Java.

1D Array:
- Linear collection of elements.
- Example: Roll numbers, marks list.

2D Array:
- Table-like structure with rows and columns.
- Example: Matrix, timetable.

Multi-Dimensional Array:
- Array of arrays (3D or more).
- Example: Cube representation.

Diagram (2D Array 3×3):


[10 20 30]
[40 50 60]
[70 80 90]

Advantages:
- Handles large structured data.
- Useful in matrix operations, games, and simulations.

Q7. Explain String Class and String Methods in Java.

String Class:
- A sequence of characters enclosed in double quotes.
- Strings are immutable (cannot be changed once created).
- Stored in the String pool for memory efficiency.

Ways to Create String:


1. Using string literal → "Hello"
2. Using new keyword → new String("Hello")

Important Methods:
- length()
- charAt(int index)
- substring(int begin, int end)
- equals()
- compareTo()
- toLowerCase(), toUpperCase()
- concat()
- trim()

Advantages:
- Provides many built-in methods.
- Useful for text processing and manipulation.

You might also like