0% found this document useful (0 votes)
5 views

Nestedclass,nested Interface

The document explains nested classes in Java, which are classes defined within another class, enhancing encapsulation and code readability. It details the differences between static nested classes, inner classes, and anonymous inner classes, including their access to outer class members. Examples are provided to illustrate how to instantiate and use these classes in Java programming.

Uploaded by

khushi.agarwal21
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Nestedclass,nested Interface

The document explains nested classes in Java, which are classes defined within another class, enhancing encapsulation and code readability. It details the differences between static nested classes, inner classes, and anonymous inner classes, including their access to outer class members. Examples are provided to illustrate how to instantiate and use these classes in Java programming.

Uploaded by

khushi.agarwal21
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Nested Classes

In Java, it is possible to define a class within another class, such classes are known
as nested classes. They enable you to logically group classes that are only used in one
place, thus this increases the use of encapsulation, and creates more readable and
maintainable code.
• The scope of a nested class is bounded by the scope of its
enclosing class. Thus in below example, class NestedClass does
not exist independently of class OuterClass.
• A nested class has access to the members, including private
members, of the class in which it is nested. But the enclosing class
does not have access to the members
of the nested class.
• A nested class is also a member of its enclosing class.
• As a member of its enclosing class, a nested class can be
declared private, public, protected, or package private(default).
Syntax:
class OuterClass
{
-----
...
public class NestedClass
{
----
...
}
}
// outer class
class OuterClass
{
// static member
static int outer_x = 10;

// instance(non-static) member
int outer_y = 20;

// private member
private static int outer_private = 30;

// static nested class


static class StaticNestedClass
{
void display()
{
// can access static member of outer class
System.out.println("outer_x = " + outer_x);

// can access display private static member of outer class


System.out.println("outer_private = " + outer_private);

// The following statement will give compilation error


// as static nested class cannot directly access non-static
members
// System.out.println("outer_y = " + outer_y);

}
}
}
// Driver class
public class StaticNestedClassDemo
{
public static void main(String[] args)
{
// accessing a static nested class
OuterClass.StaticNestedClass nestedObject = new
OuterClass.StaticNestedClass();

nestedObject.display();

}
}
Output:
outer_x = 10
outer_private = 30

Inner classes

To instantiate an inner class, you must first instantiate the outer class. Then,
create the inner object within the outer object with this syntax:
OuterClass.InnerClass innerObject = outerObject.new InnerClass();

// outer class
class OuterClass
{
// static member
static int outer_x = 10;

// instance(non-static) member
int outer_y = 20;

// private member
private int outer_private = 30;

// inner class
class InnerClass
{
void display()
{
// can access static member of outer class
System.out.println("outer_x = " + outer_x);

// can also access non-static member of outer class


System.out.println("outer_y = " + outer_y);

// can also access a private member of the outer class


System.out.println("outer_private = " +
outer_private);

}
}
}

// Driver class
public class InnerClassDemo
{
public static void main(String[] args)
{
// accessing an inner class
OuterClass outerObject = new OuterClass();
OuterClass.InnerClass innerObject = outerObject.new
InnerClass();

innerObject.display();

}
}

Output:
outer_x = 10
outer_y = 20
outer_private = 30

Anonymous Inner Class


It is an inner class without a name and for which only a single object is
created. An anonymous inner class can be useful when making an instance
of an object with certain “extras” such as overriding methods of a class or
interface, without having to actually subclass a class.
Tip: Anonymous inner classes are useful in writing implementation classes
for listener interfaces in graphics programming.
// Test can be interface,abstract/concrete class
Test t = new Test()
{
// data members and methods
public void test_method()
{
........
........
}
};

// Interface
interface Age {

// Defining variables and methods


int x = 21;
void getAge();
}

// Class 1
// Helper class implementing methods of Age Interface
class MyClass implements Age {

// Overriding getAge() method


@Override public void getAge()
{
// Print statement
System.out.print("Age is " + x);
}
}

// Class 2
// Main class
// AnonymousDemo
class GFG {
// Main driver method
public static void main(String[] args)
{
// Class 1 is implementation class of Age interface
MyClass obj = new MyClass();

// calling getage() method implemented at Class1


// inside main() method
obj.getAge();
}
}

Output:
Age oj1 = new Age()
{
@Override
public void getAge()
{
System.out.print("Age is " + x);
}
};

// Interface
interface Age {
int x = 21;
void getAge();
}

// Main class
class AnonymousDemo {

// Main driver method


public static void main(String[] args)
{

// Myclass is hidden inner class of Age interface


// whose name is not written but an object to it
// is created.
Age oj1 = new Age() {

@Override public void getAge()


{
// printing age
System.out.print("Age is " + x);
}
};

oj1.getAge();
}
}

Output
Age is 21

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy