Skip to main content

Nested Classes

Nested Classes

 
The Java programming language allows you to define a class within another class. Such a class is called a nested class.
Nested classes are divided into two categories: static and non-static.
Nested classes that are declared static are called static nested classes
Non-static nested classes are called inner classes.
 
class OuterClass {
    ...
    static class StaticNestedClass {
        ...
    }
    class InnerClass {
        ...
    }
}


A nested class is a member of its enclosing class. 
Non-static nested classes (inner classes) have access to other members of the enclosing 
class, even if they are declared private. 
Static nested classes do not have access to other members of the enclosing class.
As a member of the OuterClass, a nested class can be declared private, public, protected
or package private


Local Class
Local classes in Java are like inner classes (non-static nested classes) that are defined 
inside a method or scope block ({ ... })
inside a method.
 
Anonymous Class
 
Anonymous classes in Java are nested classes without a class name. They are typically declared as either subclasses of an existing class, or as implementations of some interface
Anonymous classes are defined when they are instantiated. Here is an example that declares an anonymous subclass of a superclass called SuperClass:
 
 public class TopClass {

  public void display() {
    System.out.println("TopClass");
  }

}


TopClass objct = new TopClass() {

    public void display() {
        System.out.println("Anonymous class");
    }
};

objct.doIt();
 
 
A Java anonymous class can also implement an interface instead of extending a class. Here an example:
public interface Intrfc { public void display(); } MyInterface objct = new MyInterface() { public void display() { System.out.println("Anonymous class"); } };objct .doIt(); 
 
You can declare fields and methods inside an anonymous class, but you cannot declare a constructor.
You can declare a static initializer for the anonymous class instead, though. 
 
 
There are basically three advantages of inner classes in java. They are as follows:
1) Nested classes represent a special type of relationship that is it can access all the members (data members and methods) of outer class including private.
2) Nested classes are used to develop more readable and maintainable code because it logically group classes and interfaces in one place only.
3) Code Optimization: It requires less code to write.

Comments

Popular posts from this blog

OBJECT class in Java

OBJECT class in Java : Object is at the top of class hierarchy in java. Every class in the Java system is a descendent (direct or indirect) of the Object class. The Object class defines the basic state and behavior that all objects must have, such as the ability to compare oneself to another object, to convert to a string, to wait on a condition variable, to notify other objects that a condition variable has changed, and to return the object's class. Mainly below methods are provided by Object class : public String toString() returns the string representation of this object. protected Object clone() throws CloneNotSupportedException creates and returns the exact copy (clone) of this object. public boolean equals(Object obj) compares the given object to this object. public int hashCode() returns the hashcode number for this object. public final Class getClass() returns the Class class object of this object. The Class class can further be used to get the metadata of ...

Java Priority Queue sort using lambda expression

Priority Queue :     In Java  Priority Queue  is a  queue  which keeps its elements sorted as per their natural order( example in ascending orders for Integer, alphabetical a-z for alphabets) or using a custom Comparator at the time of creation you can custom sort it. It has most method  similar to a queue  add, clear, poll, peek As priority queue have to compare elements to keep in order so elements must be comparable otherwise, it will throw ClassCastException .  As null can not be compared so you are not allowed to insert null too A program to sort priority queue using Lambda function in Java           c lass   PriorityQueueLambdaJava { public List<Integer> KFrequent( int [] nums, int k) { Map<Integer, Integer> map = new HashMap<>(); for ( int j: nums) { map .put(j, map .getOrDefault(j, 0 ) + 1 ); } PriorityQueue<Map.Entry<Integ...

Important Keywords: Static, Continue,Break:

Important Keywords: Static, Continue,Break:  Static: The static keyword in java is used for memory management mainly.  The static keyword members belongs to the class rather than instance of the class. Following members can be declared as static: variable (also known as class variable) method (also known as class method) block nested class  static variables : We can use static keyword with a class level variable. A static variable is a class variable and doesn’t belong to Object/instance of the class. Since static variables are shared across all the instances of Object, they are not thread safe . Usually static variables are used with final keyword for common resources or constants that can be used by all the objects. If the static variable is not private, we can access it as: ClassName.variableName //static variable example private static int a ge; public static String name; static methods: A static method belongs t...