Java Inner Classes Quiz
26 Questions
100 Views

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to lesson

Podcast

Play an AI-generated podcast conversation about this lesson

Questions and Answers

Where is an inner class declared?

Inside the curly braces of another class, but outside any method or other code block.

What modifiers can you use on an inner class?

abstract or final, along with an access modifier.

What members of its enclosing class does an inner class have access to?

All of them, including private members.

What do you need to instantiate an inner class?

<p>A reference to an instance of the outer class.</p> Signup and view all the answers

Within a class how would you instantiate an inner class called MyInner?

<p>MyInner mi = new MyInner();</p> Signup and view all the answers

Outside of a class called MyOuter's instance methods how would you instantiate an inner class of MyOuter called MyInner?

<p>MyOuter mo = new MyOuter(); MyOuter.MyInner inner = mo.new MyInner();</p> Signup and view all the answers

What does 'this' refer to when used in code within an inner class?

<p>The inner class itself.</p> Signup and view all the answers

How can you refer to the instance of an outer class called MyOuter to which an inner class instance is tied?

<p>MyOuter.this</p> Signup and view all the answers

Where is a method-local inner class defined?

<p>Within a method of the enclosing class.</p> Signup and view all the answers

How can a method-local inner class be used?

<p>It must be instantiated.</p> Signup and view all the answers

Where can a method-local inner class be instantiated?

<p>Within the same method it is defined, but after the definition.</p> Signup and view all the answers

Which variables declared within the method (including parameters) can a method-local inner class use?

<p>Only those marked final.</p> Signup and view all the answers

Which modifiers apply to a method-local inner class?

<p>Only abstract or final.</p> Signup and view all the answers

What name do anonymous inner classes have?

<p>They do not have a name.</p> Signup and view all the answers

What is the type of an anonymous inner class?

<p>Either a subclass of the named type or an implementor of the named interface.</p> Signup and view all the answers

An anonymous inner class is always created as part of what?

<p>A statement.</p> Signup and view all the answers

What should always follow the closing } of an anonymous inner class?

<p>;</p> Signup and view all the answers

What methods can you call on an anonymous inner class reference?

<p>Only those defined in the reference variable class (or interface).</p> Signup and view all the answers

What are the rules for an anonymous inner class relating to extending classes and implementing interfaces?

<p>An anonymous inner class may extend either one class or implement one interface.</p> Signup and view all the answers

How is an argument-defined anonymous inner class declared, defined and instantiated?

<p>As part of a method invocation.</p> Signup and view all the answers

What should follow the closing } for an argument-defined anonymous inner class?

<p>) ;</p> Signup and view all the answers

What are static nested classes?

<p>Inner classes marked with the static modifier.</p> Signup and view all the answers

Is a static nested class actually an inner class?

<p>False</p> Signup and view all the answers

Do you need an instance of the outer class to instantiate a static nested class?

<p>False</p> Signup and view all the answers

What does it require to instantiate a static nested class named Nested found in an outer class named BigOuter?

<p>Just both class names.</p> Signup and view all the answers

How can a static nested class access the non-static members of its enclosing class?

<p>It can't.</p> Signup and view all the answers

Study Notes

Inner Classes

  • An inner class is declared inside the curly braces of another class, outside any method.
  • Inner classes can use modifiers like abstract, final, and access modifiers because they are members of the enclosing class.
  • Inner classes have access to all members of their enclosing class, including private members.
  • Instantiation of an inner class requires a reference to an instance of the outer class.

Instantiation

  • To instantiate an inner class named MyInner, use: MyInner mi = new MyInner();.
  • To instantiate an inner class from outside its enclosing class (MyOuter), both class names must be used along with a reference to the outer class:
    • E.g., MyOuter mo = new MyOuter();
    • MyOuter.MyInner inner = mo.new MyInner();.

Contextual Reference

  • The keyword "this" in an inner class refers to the inner class itself.
  • To access the outer class instance tied to the inner class, use: MyOuter.this.

Method-Local Inner Classes

  • A method-local inner class is defined within a method of the enclosing class.
  • It must also be instantiated to be used, and can only be instantiated within the same method after its definition.
  • Method-local inner classes can only use parameters and variables declared within the method if marked as final.

Anonymous Inner Classes

  • Anonymous inner classes do not have a specific name and are either subclasses of a named class or implement a named interface.
  • They are always created as part of a statement and must follow with a semicolon ; after the closing bracket.
  • Method calls on an anonymous inner class reference can only invoke methods defined in the reference variable’s class or interface.

Extension and Implementation Rules

  • An anonymous inner class can extend one class or implement one interface, but cannot do both or implement multiple interfaces.
  • Argument-defined anonymous inner classes are declared as part of a method invocation, with a closing parenthesis and semicolon following the class definition.

Static Nested Classes

  • Static nested classes are inner classes that are marked with the static modifier.
  • They are not true inner classes; they are top-level nested classes and do not require an instance of the outer class for instantiation.
  • Static nested classes access non-static members of their enclosing class only if an instance of the outer class is explicitly passed; otherwise, they cannot access them.

Studying That Suits You

Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

Quiz Team

Description

Test your knowledge of Java inner classes with these flashcards. Explore where inner classes can be declared, the modifiers applicable to them, and their access to the enclosing class's members.

More Like This

Java Inner Classes
18 questions

Java Inner Classes

EntertainingTuring avatar
EntertainingTuring
Java Inner Classes
18 questions

Java Inner Classes

StunnedCatSEye avatar
StunnedCatSEye
Java 8 OCP Inner Classes Flashcards
35 questions
Java SQL and Inner Classes Quiz
45 questions

Java SQL and Inner Classes Quiz

UnderstandableDivisionism avatar
UnderstandableDivisionism
Use Quizgecko on...
Browser
Browser