Java Tutorial

Operators

Flow Control

String

Number and Date

Built-in Classes

Array

Class and Object

Inheritance and Polymorphism

Exception Handling

Collections, Generics and Enumerations

Reflection

Input/Output Stream

Annotation

Java Partial Inner Class

In Java, there is no concept of a "partial inner class" like there is for partial classes in C#. However, you can create inner classes, which are classes defined within the scope of another class, often referred to as the outer class. In this tutorial, we will cover the basics of inner classes in Java, including their types and use cases.

  • Types of inner classes:

There are four types of inner classes in Java:

a. Member inner class b. Static inner class c. Local inner class d. Anonymous inner class

  • Member inner class:

A member inner class is a regular class defined within the scope of an outer class. It has access to all the fields and methods of the outer class, even if they are private. However, it cannot have static members, as it is associated with an instance of the outer class.

Example:

class OuterClass {
    private String message = "Hello, World!";

    class InnerClass {
        void displayMessage() {
            System.out.println("Message: " + message);
        }
    }
}
  • Static inner class:

A static inner class is a class defined within the scope of an outer class with the static keyword. It behaves like a regular class but has access only to the static fields and methods of the outer class.

Example:

class OuterClass {
    private static String message = "Hello, World!";

    static class StaticInnerClass {
        void displayMessage() {
            System.out.println("Message: " + message);
        }
    }
}
  • Local inner class:

A local inner class is a class defined within a method or a block of code. It has access to the final or effectively final local variables of the method or block in which it is defined.

Example:

class OuterClass {
    void outerMethod() {
        final String message = "Hello, World!";

        class LocalInnerClass {
            void displayMessage() {
                System.out.println("Message: " + message);
            }
        }
    }
}
  • Anonymous inner class:

An anonymous inner class is an unnamed class defined within an expression, usually as an argument to a method call. Anonymous inner classes are often used to create objects with specific behavior, such as event listeners or comparators.

Example:

import java.util.Comparator;

class Main {
    public static void main(String[] args) {
        Comparator<String> stringLengthComparator = new Comparator<String>() {
            @Override
            public int compare(String s1, String s2) {
                return s1.length() - s2.length();
            }
        };
    }
}

In this tutorial, we discussed the basics of inner classes in Java, including their types and use cases. Inner classes are a powerful feature of Java, allowing you to create classes within the scope of other classes, providing encapsulation and organization for your code.

  1. Java Nested Classes Explained:

    • Nested classes are classes defined within another class. They allow you to logically group classes that are only used in one place.
    public class Outer {
        class Inner {
            // Inner class
        }
    }
    
  2. Static Inner Class in Java:

    • A static inner class is associated with its outer class but doesn't require an instance of the outer class for instantiation.
    public class Outer {
        static class StaticInner {
            // Static inner class
        }
    }
    
  3. Inner Class vs Outer Class in Java:

    • Inner classes have access to the members of the outer class, and they are used for logical grouping. Outer classes can access members of inner classes through an instance of the inner class.
    public class Outer {
        class Inner {
            // Inner class can access members of Outer
        }
    }
    
  4. Local Inner Class in Java:

    • A local inner class is defined within a method and has access to the variables of that method.
    public class Outer {
        void someMethod() {
            class LocalInner {
                // Local inner class
            }
        }
    }
    
  5. Anonymous Inner Class in Java:

    • Anonymous inner classes are defined and instantiated at the same time. They are often used for one-time use cases.
    public interface MyInterface {
        void myMethod();
    }
    
    public class Outer {
        MyInterface myObject = new MyInterface() {
            public void myMethod() {
                // Anonymous inner class implementation
            }
        };
    }
    
  6. Java Inner Class Access Modifiers:

    • Inner classes can have access modifiers like any other class. They can be public, protected, private, or package-private (default).
    public class Outer {
        private class PrivateInner {
            // Private inner class
        }
    }
    
  7. Java Inner Class and Encapsulation:

    • Inner classes can access private members of the outer class, promoting encapsulation.
    public class Outer {
        private int outerField;
    
        class Inner {
            void accessOuterField() {
                outerField = 42; // Inner class can access private field of Outer
            }
        }
    }
    
  8. Java Inner Class vs Lambda Expression:

    • Inner classes provide more flexibility and can have multiple methods, while lambda expressions are more concise and suitable for functional interfaces.
    Runnable runnable = new Runnable() {
        public void run() {
            // Inner class implementation
        }
    };
    
    Runnable lambdaRunnable = () -> {
        // Lambda expression implementation
    };
    
  9. Java Inner Class Example Code:

    • An example demonstrating the use of inner classes for logical grouping.
    public class Team {
        private String teamName;
    
        class Player {
            private String playerName;
    
            Player(String playerName) {
                this.playerName = playerName;
            }
    
            void displayPlayer() {
                System.out.println("Team: " + teamName + ", Player: " + playerName);
            }
        }
    
        Team(String teamName) {
            this.teamName = teamName;
        }
    }
    
  10. Java Inner Class Inheritance:

    • Inner classes can extend other classes and implement interfaces, just like any other class.
    public class Outer {
        class Inner extends MyBaseClass implements MyInterface {
            // Inner class inheritance
        }
    }
    
  11. Nested Interface in Java:

    • An interface can be defined inside a class or interface, creating a nested interface.
    public class Outer {
        interface NestedInterface {
            // Nested interface
        }
    }
    
  12. Inner Class Visibility and Scoping in Java:

    • Inner classes have access to the members of the outer class, and their visibility is determined by access modifiers. Local inner classes have access to local variables of the method where they are defined.
    public class Outer {
        private int outerField;
    
        void someMethod() {
            class LocalInner {
                void accessOuterField() {
                    outerField = 42; // Local inner class can access private field of Outer
                }
            }
        }
    }