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

presentation9 (1)

The document provides an overview of nested classes in Java, including static nested classes, inner classes, local classes, and anonymous inner classes. It explains how these classes can be defined, instantiated, and their access to outer class members. Additionally, it highlights the differences between regular classes and anonymous inner classes in terms of implementation and instantiation.

Uploaded by

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

presentation9 (1)

The document provides an overview of nested classes in Java, including static nested classes, inner classes, local classes, and anonymous inner classes. It explains how these classes can be defined, instantiated, and their access to outer class members. Additionally, it highlights the differences between regular classes and anonymous inner classes in terms of implementation and instantiation.

Uploaded by

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

IT 214

Object
Oriented
Programming
Dr. Abdulaziz Saleh Algablan
Email: [email protected]
2023
Topics
Nested Static Classes
classes
Inner Classes
Local Classes
Anonymous
classes
Nested Classes
• Can we define more than one class in one Java file?
• Can we define a new class inside class?
• Can we define a new class inside a method?
Multiple Classes in Java File
class A { }
public class B { }
class C { }
class D { }

• Can we store the above classes in one file?


• What is the name of the file?
• Where should the main method be placed?
Nested Class
• Class definition inside a class

public class B {
class B2 {

}
}
Nested Classes
• Java developers can define a class within another class.
• Such a class is called a nested class.
• There are several types of nested classes in Java:
• Nested classes are called inner classes if they were declared as non-static.
• if a nested class is static, it is called a static nested class.
Static Nested Classes

public class OuterClass {


StaticNestedClass var1;
String name;
static class StaticNestedClass {
void methodInsideStaticClass() {}

}
}
Static Nested Classes

• Nested static classes can be instantiated without the need for an


instance of the outer class (to be used outside the outer class):
public class TestOuter {
public static void main(String[] sd){
OuterClass.StaticNestedClass staticNested_i1 =
new
OuterClass.StaticNestedClass();
staticNested_i1.methodInsideStaticClass();

}
}
Static Nested Classes

• A static nested class is associated with its outer class.


• A static nested class can access static filed and static methods of the
outer class.
• Like static class methods, a static nested class cannot refer directly to
instance variables or instance methods defined in its enclosing class
• it can use them only through an object reference.
• They are accessed using the enclosing class name.
Access Outer’s Static Filed and
Methods
class OuterClass {
static int staticIntVariable = 5;
int nonStaticIntVariable = 15;
static void doStatic()
{
}
static class StaticNestedClass {
void methodInsideStaticClass()
{
staticIntVariable++;
doStatic();
}
}
}
class OuterClass {
static int staticIntVariable = 5;
int nonStaticIntVariable = 15;
static class StaticNestedClass {
void methodInsideStaticClass()
{
staticIntVariable++;
nonStaticIntVariable++; // This causes ERROR
}
}
}
public class StaticInnerTest {
public static void main(String[] args) {
OuterClass.StaticNestedClass i1 = new OuterClass.StaticNestedClass();
i1.methodInsideStaticClass();
}
}
Inner Class
• A non-static class inside a normal class.
• It has different requirement: Without an outer class object existing,
there cannot be an inner class object. That is, the inner class object is
always associated with the outer class object.
• A non-static nested class can access static and non-static fileds and
methods of the outer.
public class NonStaticInner {
public static void main(String[] args) {
OuterClassRegular outerInstance = new OuterClassRegular();
OuterClassRegular.InnerClassRegular innerInstance =
outerInstance.new InnerClassRegular();
}
}
class OuterClassRegular {
int x=5;
static int y=10;
class InnerClassRegular {
void add()
{
int w = x+y; //This is OK.
}
}
}
Non-static Method Calls
class OuterClassRegular {
int x =5;
static int y =10;

void doNonStatic()
{
}
class InnerClassRegular {
void add()
{
int w = x+y;
doNonStatic();
}
}
}
Local Class
public class LocalInnerClass {
private int data = 30;// instance variable
void display() { // start of display()
class Local {//class Definition
void msg() {
System.out.println(data);}
}

Local l = new Local();


l.msg();
} // end of display()
}
Local Class
public class LocalInnerClass {
private int data = 30;// instance variable
void display() { // start of display()
class Local {
void msg() {
System.out.println(data);}}
Local l = new Local();
l.msg();
} // end of display()
public static void main(String args[]) {
LocalInnerClass obj = new LocalInnerClass();
obj.display();
}
}
Anonymous Inner class
• Anonymous inner class: a class without a name that is defined and
instantiated in a single expression.
• Sometimes it is created inside a method’s parameter.
• It is often used for one-time use, such as event handling.
Anonymous Inner class
public class AnonymousClass {
static void doSomething(I1Interface i) {
i.myMethod();
}
public static void main(String[] args) {
doSomething(new I1Interface() {

@Override
public void myMethod() {
}});
}

}
interface I1Interface {
void myMethod();
}
Anonymous Inner class

• A normal class can implement any number of interfaces


• but the anonymous inner class can implement only one interface at a time.
• A regular class can extend a class and implement any number of
interfaces simultaneously
• But anonymous Inner class can extend a class or can implement an interface but
not both at a time.
• For regular/normal class, we can write any number of constructors
• but we can’t write any constructor for anonymous Inner class because the
anonymous class does not have any name and while defining constructor class
name and constructor name must be same.
End

You might also like