0% found this document useful (0 votes)
9 views3 pages

Java Memory Management & Garbage Collection Quiz Flashcard

Uploaded by

pibexik122
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)
9 views3 pages

Java Memory Management & Garbage Collection Quiz Flashcard

Uploaded by

pibexik122
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
You are on page 1/ 3

Java Nested Classes, Generic Classes & Inheritance

Quiz Flashcard

🌟 Summary with Examples

Nested Class Examples

class Outer {
static class StaticNested {
void display() { System.out.println("Static Nested"); }
}
class Inner {
void display() { System.out.println("Inner Class"); }
}
void methodWithLocalInner() {
class LocalInner {
void display() { System.out.println("Local Inner"); }
}
new LocalInner().display();
}
}

Usage:

Outer.StaticNested sn = new Outer.StaticNested();


sn.display();
Outer outer = new Outer();
Outer.Inner in = outer.new Inner();
in.display();
outer.methodWithLocalInner();

Anonymous Inner Class Example

Runnable r = new Runnable() {


public void run() { System.out.println("Anonymous Inner Class"); }
};
r.run();

1
Generic Class Example

class Box<T> {
T value;
void set(T value) { this.value = value; }
T get() { return value; }
}
Box<Integer> intBox = new Box<>();
intBox.set(10);
System.out.println(intBox.get());

Inheritance with Generics Example

class Parent<T> { }
class Child<T> extends Parent<T> { }

🌟 Quiz Questions (Covers Full Theory)


1️⃣ What are the types of nested classes in Java?

2️⃣ How does a static nested class differ from an inner class?

3️⃣ Can a static nested class access the members of the enclosing class?

4️⃣ What is a local inner class and where is it defined?

5️⃣ What is an anonymous inner class and when would you use one?

6️⃣ How can nested classes participate in inheritance?

7️⃣ What is a generic class in Java?

8️⃣ How do you define a generic class with multiple type parameters?

9️⃣ Can a generic class extend another generic class? Give an example.

🔟 What is type erasure in the context of generics?

1️⃣1️⃣ Can nested classes themselves be generic?

1️⃣2️⃣ How does inheritance work with generic classes?

2
1️⃣3️⃣ What is a bounded type parameter and how is it declared?

1️⃣4️⃣ Can you create an array of a generic type? Why or why not?

🌟 Answers
1️⃣ Static nested class, non-static inner class (member inner class), local inner class, anonymous inner class.

2️⃣ A static nested class does not have access to instance members of the outer class, while an inner class
does.

3️⃣ No, a static nested class cannot access non-static members of the enclosing class directly.

4️⃣ A local inner class is defined within a method or a block of code.

5️⃣ An anonymous inner class is a class without a name, defined and instantiated in a single expression,
often used for implementing interfaces or abstract classes on the fly.

6️⃣ Nested classes can extend or implement other classes/interfaces like regular classes. Inner classes can
extend a class or implement an interface.

7️⃣ A generic class is a class that can operate on objects of various types specified as parameters when
instantiated.

8️⃣ By declaring like class Pair<T, U> { ... } where T and U are type parameters.

9️⃣ Yes. Example: class Child<T> extends Parent<T> {}

🔟 Type erasure is the process where generic type information is removed at runtime, so generic types
behave as Object at runtime.

1️⃣1️⃣ Yes, nested classes can also declare their own type parameters and be generic.

1️⃣2️⃣ A generic class can extend another generic class by specifying compatible type parameters.

1️⃣3️⃣ A bounded type parameter restricts the types that can be used as arguments; e.g., <T extends
Number> .

1️⃣4️⃣ No, because of type erasure, the JVM doesn’t know the actual type at runtime, which can lead to heap
pollution.

You might also like