Core java - Advance Topics
  • Welcome
  • Schedule
  • 1) Exception Handling
    • 1) Introduction to Exception Handling
    • 2) Categories of Exceptions
    • 3) Creating a method that throws an exception
    • 4) Creating Custom Exception Classes
    • 5)What happens when an exception is thrown?
      • 5.1) Creating try-catch-finally blocks
      • 5.2) Using a method that throws a checked exception
      • 5.3) Using a method that throws a runtime exception
      • 5.4) Using a method that throws an error
      • 5.5) Will a finally block execute even if the catch block defines a return statement?
      • 5.6) What happens if both a catch and a finally block define return statement?
      • 5.7) What happens if a finally block modifies the value returned from a catch block?
      • 5.8) Can a try block be followed only by a finally block?
      • 5.9) Does the order of the exceptions caught in the catch blocks matter?
      • 5.10) Can I rethrow an exception or the error I catch?
      • 5.11) Can I declare my methods to throw a checked exception instead of handling it?
      • 5.12) I can create nested loops, so can I create nested try-catch blocks too?
      • 5.13) Should I handle errors?
    • 6) Best Practices
    • 7) Cheat Sheet
    • 8) Problems
  • 2) Wrapper Classes and Enums
    • 2.1) Creating objects of the wrapper classes
    • Enums
  • 3) Inner Classes
    • 3.1) Static nested class (also called static inner class)
    • 3.2) Inner class (also called member class)
    • 3.3) Anonymous inner class
    • 3.4) Method local inner classes
    • CheatSheet
  • 4) Generics
    • Multiple Type parameters in Generic classes
    • Inheritance using Generics
    • Generic interfaces
    • Generic Methods
    • Bounded type parameters
    • Applications
  • 5) Equals and Hashcode
    • Problems
  • CompareTo method overview
  • Basic DS
    • 1) Simple Array List
    • 2) Simple HashMap
  • 5) Collections Framework - Part 1
    • Introducing the collections framework
    • Working with the Collection interface
      • The core Collection interface
      • Methods of the Collection interface
    • Creating and using List, Set, and Deque implementations
      • List interface and its implementations
      • Iterators
      • Sorting List using custom sorting technique
      • Comparable Interface
      • Custom Sorting using comparator
      • ArrayList - Examples and practice problems
    • Stack
    • Linked List
    • LinkedList Operations
  • 6) Collections Framework - Part 2
    • Sets
      • Set Types
      • Array to Set (vice versa)
    • Maps
    • TreeMap
    • Autoboxing And Unboxing
  • Collections Framework - Part 3
    • Basics : DS , Number System
    • Internal Working
      • HashMap
      • HashSet
  • 7) Reflection API
  • 8) Annotations
  • 9) Reading Input From Various Sources
    • File Handling
    • Reading From Xml
    • Reading From JSON
  • 10) Multi-threading (Concurrency)
    • Protect shared data
    • Thread-safe access to shared data
  • 11) Design Patterns
    • Singleton
    • DI
  • 12) Internal Working of JVM
  • 13) Garbage Collection
  • 14) More on Strings (Buffer and Builder)
  • 15) Cloning and Immutable Class
    • 16) Serialization And Deserialization
    • Untitled
  • JAVA 8
    • Interface Changes
    • Lambda
    • Method Ref
    • Optional
    • Streams
    • Predicates
  • Practice Tests
    • Test - Collections
    • OOPS
    • S-OOPS
Powered by GitBook
On this page
  • Introduction:
  • CHARACTERISTICS OF INNER CLASSES :
  • CREATION OF AN INNER CLASS :
  • WHAT CAN AN INNER CLASS ACCESS? :
  • CAN AN INNER CLASS COEXIST WITH ONLY ITS OUTER CLASS?
  • Problem :

Was this helpful?

  1. 3) Inner Classes

3.2) Inner class (also called member class)

Previous3.1) Static nested class (also called static inner class)Next3.3) Anonymous inner class

Last updated 6 years ago

Was this helpful?

Introduction:

The definition of an inner class is enclosed within another class, also referred to as an outer class. An inner class is an instance member of its outer class. An instance of an inner class shares a special bond with its outer class and can’t exist without its instance

You’d usually create an inner class to encapsulate partial functionality of your main class such that the existence of the inner class instance isn’t possible without its outer class instance. This is in contrast to a nested static class, which can be used without an instance of its outer class.

For example, the following code defines a class Tree and an inner class TreeSort. Tree defines operations to add, remove, and sort objects based on a condition. Instead of defining methods and variables to sort the tree elements within class Tree, it encapsulates sorting functionality within class TreeSort. Class TreeSort would always work with Tree and might not be needed without class Tree:

CHARACTERISTICS OF INNER CLASSES :

Because an inner class is a member of its outer class, an inner class can be defined using any of the four access levels: public, protected, default access, and private. Like a regular top-level class, an inner class can also define constructors, variables, and methods. But an inner class can’t define nonfinal static variables or methods, as shown in figure

CREATION OF AN INNER CLASS :

Whenever you instantiate an inner class, remember that an instance of an inner class can’t exist without an instance of the outer class in which it’s defined. Let’s look at cre- ating an inner class:

  • Within an outer class, as an instance member

  • Within a method of an outer class

  • Within a static method of an outer class

  • Outside the outer class

You must have an outer class instance to create an inner class instance.

The accessibility of an inner class outside its outer class depends on the access modifier used to define the inner class. For example, an inner class with default access can’t be accessed by classes in different packages than the outer class.

WHAT CAN AN INNER CLASS ACCESS? :

An inner class is a part of its outer class. Therefore an inner class can access all vari- ables and methods of an outer class, including its private members and the ones that it inherits from its base classes. An inner class can also define members with the same name as its outer class, as shown in figure

An object uses the reference this to refer to its own object. An inner class can use the reference this to refer to its own object, and the name of its outer class followed by .this to refer to the object of its outer class, as shown in figure.

CAN AN INNER CLASS COEXIST WITH ONLY ITS OUTER CLASS?

Yes, an inner class can exist only with an object of its outer class. When a compiler compiles an inner class, it seems to insert code in the inner class, which defines an instance variable of its outer class, initialized using its constructor, as illustrated in figure :

Problem :

Characteristics of an inner class: it can be defined using any access modifier, can define constructors, and can define instance variables and methods. An inner class can define static members variables but not static methods.
An inner class can access all the members of its outer class, including its private members. Outer class members with the same name as inner class members can be accessed using Outer.this, where Outer is the name of the outer class.
An inner class uses this to refer to its own object and <name_of_its_outer_class>.this to refer to its outer class’s object.
Java instantiates an inner class by passing it an outer class instance.