Programming Pandit

c/c++/c#/Javav/Python


Latest Update

Friday, February 28, 2025

Object cloning, Inner classes


In Java, the final keyword can be used with classes, methods, and variables to restrict their behavior.

1. Final Class

A class declared as final cannot be inherited by any other class. This is useful when you want to prevent modifications to the class by subclassing.

Example:

final class FinalClass {

    void display() {

        System.out.println("This is a final class.");

    }

}

 

// This will cause an error because FinalClass cannot be inherited

// class SubClass extends FinalClass {} // Compilation error


2. Final Method

A method declared as final cannot be overridden in a subclass. This is useful when you want to prevent specific behavior from being changed.

Example:

class Parent {

    final void show() {

        System.out.println("This is a final method.");

    }

}

 

class Child extends Parent {

    // void show() {  // This will cause a compilation error

    //    System.out.println("Trying to override.");

    // }

}

In the example above, if you try to override the show() method in the Child class, it will result in a compilation error.

 

 

 

Inner Classes in Java

An inner class in Java is a class that is defined within another class. Inner classes have access to the members (including private members) of the outer class, making them useful for encapsulation and logically grouping related functionality.


Types of Inner Classes

1. Member Inner Class

A member inner class is a non-static class defined inside another class. It can access all members of the outer class, including private ones.

Example:

class Outer {

    private String message = "Hello from Outer class";

 

    // Inner class

    class Inner {

        void display() {

            System.out.println(message); // Can access private members of Outer class

        }

    }

 

    public static void main(String[] args) {

        Outer outer = new Outer();

        Outer.Inner inner = outer.new Inner(); // Creating an instance of inner class

        inner.display();

    }

}


2. Static Nested Class

A static nested class is a static member of the outer class. Unlike member inner classes, it cannot access non-static members of the outer class directly.

Example:

class Outer {

    static String message = "Static Nested Class Example";

 

    // Static nested class

    static class Nested {

        void display() {

            System.out.println(message); // Can access static members of Outer class

        }

    }

 

    public static void main(String[] args) {

        Outer.Nested nested = new Outer.Nested(); // No need to create an instance of Outer

        nested.display();

    }

}


3. Local Inner Class

A local inner class is defined within a method or a block and can only be accessed within that method/block.

Example:

class Outer {

    void outerMethod() {

        class LocalInner {

            void display() {

                System.out.println("Inside Local Inner Class");

            }

        }

        LocalInner inner = new LocalInner();

        inner.display();

    }

 

    public static void main(String[] args) {

        Outer outer = new Outer();

        outer.outerMethod();

    }

}


4. Anonymous Inner Class

An anonymous inner class is a class without a name, typically used when instantiating an interface or an abstract class.

Example using an interface:

interface Greeting {

    void sayHello();

}

 

class Main {

    public static void main(String[] args) {

        Greeting greet = new Greeting() {

            public void sayHello() {

                System.out.println("Hello from Anonymous Inner Class");

            }

        };

        greet.sayHello();

    }

}


Key Points

  • Member Inner Class: Non-static class inside another class, can access all members of the outer class.
  • Static Nested Class: A static class inside another class, can only access static members of the outer class.
  • Local Inner Class: Defined inside a method, only accessible within that method.
  • Anonymous Inner Class: A class without a name, mainly used to implement interfaces or extend abstract classes in a concise way.

 

No comments:

Post a Comment