Posts

Inner Class In Java

In Java, an inner class is a class that is defined inside another class. Inner classes are useful when you want to encapsulate one class inside another class, so that the inner class has access to the private members of the outer class. There are four types of inner classes in Java: Member inner class: A member inner class is defined at the member level of a class. It can access all members of the enclosing class, including private members. Static inner class: A static inner class is a nested class that is declared as static. It can access only static members of the enclosing class. Local inner class: A local inner class is defined inside a method or a block of code. It has access to the final local variables of the enclosing method or block. Anonymous inner class: An anonymous inner class is a local inner class without a name. It is typically used for creating a single object of an interface or an abstract class. Here's an example of a member inner class: code with mohi...

Final keyword in Java

  In Java, the final keyword can be applied to variables, methods, and classes. Its meaning differs slightly depending on where it is used: Final variables: A final variable cannot be reassigned a new value once it has been initialized. It is often used to declare constants. For example: java Copy code final int MAX_SIZE = 10 ; Final methods: A final method cannot be overridden by a subclass. This is useful when you want to prevent a subclass from changing the behavior of a method. For example: java Copy code class Parent   { final void doSomething () { // ... } } class Child extends Parent {  // Compiler error: Cannot override the final method from Parent void doSomething () { // ... } } Final classes: A final class cannot be subclassed. This is useful when you want to prevent other classes from extending your class. For example: java Copy code final class MyClass { // ... }  // Compiler error: Cannot extend...