0% found this document useful (0 votes)
0 views5 pages

Unit 3 JAVA Shorts

Java unit 3 shorts
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)
0 views5 pages

Unit 3 JAVA Shorts

Java unit 3 shorts
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 - 3 JAVA NAME : shaik janu CSE -E 248T1A05V7

[Link] can you dynamically change the size of an array in Java? Discuss the
limitations of this approach.
[Link] Change of Array Size:
Java arrays have a fixed size, which cannot be changed directly once created.
However, we can simulate dynamic resizing by creating a new array with a different size and
copying the elements of the old array into it.

Example:
int[] a = new int[5]; // old array
// assign values
int[] b = new int[10]; // new array with larger size
for (int i = 0; i < [Link]; i++) {
b[i] = a[i]; // copy old elements
}
a = b; // now 'a' refers to new array

Explanation:In this process, the old array is destroyed and replaced by a new one with a
larger size.
The existing values are manually copied to the new array.

Limitations:
•Time-consuming because elements must be copied manually.
•Memory wastage if the new array is larger than required.
•Old array elements are lost if not copied properly.
•Not truly “dynamic” like in ArrayList or Vector.

[Link] between the storage of one-dimensional and two-dimensional arrays in


memory.
Ans.
Feature One-Dimensional Array Two-Dimensional Array

Definition Collection of elements stored in a Collection of elements arranged in


single row rows and columns.

Storage Stored in contiguous memory Stored in row-major order (row by


locations sequentially. row) in contiguous memory.

Access Accessed using a single subscript Accessed using two subscripts


(e.g., a[i]). (e.g., a[i][j]).

Example int a[5]; int a[3][3];

[Link] is the significance of the 'super' keyword in inheritance? Explain with an


example.
[Link] : The super keyword in Java is used to refer to the immediate parent class
members like variables, methods, and constructors.

Uses of super:
UNIT - 3 JAVA NAME : shaik janu CSE -E 248T1A05V7

1. At variable level: To access parent class variables when both parent and child have the
same variable names.
2. At method level: To call parent class methods that are overridden in the child class.
3. At constructor level: To call the parent class constructor from the child class constructor.

Example:
class Parent {
int a = 10;
void show() { [Link]("Parent method"); }
}
class Child extends Parent {
int a = 20;
void display() {
[Link](super.a); // Access parent variable
[Link](); // Call parent method
}
}

Summary:super helps avoid ambiguity and ensures access to parent class features in
inheritance.

[Link] does Java's array assignment (arrl = arr2) affect object references?
[Link] Assignment in Java : When one array is assigned to another (e.g., arr1 =
arr2), both variables refer to the same array object in memory.
•No new array is created — only a new reference to the existing array is made.
Any change made through one reference will reflect in the other.

[Link] default methods and static methods in interfaces.


Ans.
Feature Default Method Static Method

Keyword Used Declared using default keyword​ Declared using static


keyword

Access Accessed through object of implementing Accessed using interface


class name

Overriding Can be overridden in implementing class Cannot be overridden


Purpose To provide default implementation to To define utility or helper


methods methods

[Link] the concept of "abstract classes" and give one scenario where they offer
an advantage over interfaces.
[Link] class :An abstract class in Java is a class declared with the keyword abstract
that can contain both abstract methods (without body) and concrete methods (with body).
•Can extend only one class (abstract or concrete).
•Can have variables, constructors, and different access specifiers.
UNIT - 3 JAVA NAME : shaik janu CSE -E 248T1A05V7

•Used to provide partial implementation to subclasses.

Advantage over Interface:Abstract classes are preferred when common variables or


implemented methods need to be shared among subclasses, which is not possible with
interfaces.

Example:
abstract class Shape {
abstract void draw();
void show() { [Link]("Drawing shape"); }
}

[Link] arrays in java?


[Link]:An array in Java is an object that stores multiple elements of the same data type
in contiguous memory locations.
•It is a data structure used to store a fixed number of similar elements.
•Arrays in Java are index-based, where the first element is stored at index 0, the second at
index 1, and so on.
Example: int[] num = {10, 20, 30, 40};

[Link] is method overriding? Why is it important in polymorphism?


[Link] Overriding :Method overriding occurs when a subclass provides a specific
implementation of a method that is already defined in its parent class with the same name,
return type, and parameters.
Example:
class Parent {
void show() { [Link]("Parent class"); }
}
class Child extends Parent {
void show() { [Link]("Child class"); }
}

Importance in Polymorphism:
•It enables runtime polymorphism (dynamic method dispatch).
•The method to be executed is decided at runtime based on the object type, not the
reference type.
•It allows the program to achieve flexibility and code reusability.

9. State how multiple inheritance is achieved in Java, and why it's not allowed directly
through classes.
[Link] does not support multiple inheritance through classes to avoid ambiguity and
complexity caused by the diamond problem (when two parent classes have the same
method).

Reason:If a class inherits from multiple classes, it becomes unclear which parent’s method
should be called — leading to confusion.
Solution in Java:Java achieves multiple inheritance through interfaces.
UNIT - 3 JAVA NAME : shaik janu CSE -E 248T1A05V7

•A class can implement multiple interfaces, and an interface can extend multiple other
interfaces, providing multiple inheritance-like behavior safely.

Example:
interface A { void show(); }
interface B { void display(); }
class C implements A, B {
public void show() {}
public void display() {}
}

10. What is a functional interface? State its primary use in Java 8 and above.
[Link] Interface :A functional interface is an interface that contains exactly one
abstract method.
It may also have default and static methods, but only one abstract method.

Primary Use (in Java 8 and above):


Functional interfaces are mainly used to implement Lambda Expressions, which make code
more concise and readable.

Example:
@FunctionalInterface
interface Greeting {
void sayHello();
}

[Link] the purpose and usage of annotations in Java interfaces.


[Link]:Annotations in Java interfaces are used to provide metadata (additional
information) about the code to the compiler or runtime environment.
They help in giving instructions, warnings, or behavioral hints to the compiler without
affecting program logic.

Usage:
•To document interface behavior or specify its purpose.
•To mark interfaces like @FunctionalInterface, ensuring the interface has only one abstract
method.
•Custom annotations can be created to enforce rules or provide configuration details.

Example:
@FunctionalInterface
interface MyInterface {
void display();
}

Explanation:Here, @FunctionalInterface ensures that the interface contains only one


abstract method.

[Link] are nested interfaces useful in Java applications?


UNIT - 3 JAVA NAME : shaik janu CSE -E 248T1A05V7

[Link] Interfaces in Java : A nested interface is an interface that is declared inside


another class or interface.
•It is also known as an inner interface.

Purpose / Usefulness:
•Used to group related interfaces inside a class or interface.
•Helps in encapsulation and logical organization of code.
•Useful when an interface is meant to be used only by the outer class.
•Increases readability and maintainability of large applications.

Example:
class Outer {
interface Inner {
void display();
}
}

Explanation:Here, Inner is a nested interface inside Outer and can be accessed using
[Link].

You might also like