0% found this document useful (0 votes)
56 views12 pages

Object Oriented Programming Nested Classes: by Irfan Latif Memon

There are two types of nested classes in Java: static and non-static (inner) classes. Non-static nested classes can access all members of the outer class, including private members. They allow grouping of related classes for improved readability and maintainability of code. Inner classes can access private members of the outer class by defining a method in the inner class that returns the private member. The outer class must first be instantiated before instantiating the inner class using it.

Uploaded by

Kashif Mujeeb
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
0% found this document useful (0 votes)
56 views12 pages

Object Oriented Programming Nested Classes: by Irfan Latif Memon

There are two types of nested classes in Java: static and non-static (inner) classes. Non-static nested classes can access all members of the outer class, including private members. They allow grouping of related classes for improved readability and maintainability of code. Inner classes can access private members of the outer class by defining a method in the inner class that returns the private member. The outer class must first be instantiated before instantiating the inner class using it.

Uploaded by

Kashif Mujeeb
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 12

Object Oriented Programming

Nested Classes
Lecture 12
By
Irfan Latif Memon
Outline

 Nested Classes
 Inner Class within any block scope
Nested Classes or Inner Class
• Java inner class or nested class is a class which
is declared inside the class or interface.
• We use inner classes to logically group classes
and interfaces in one place so that it can be
more readable and maintainable.
• Additionally, it can access all the members of
outer class including private data members
and methods.
Two types of nested classes
• There are two types of nested classes:
• static and non-static.
• A static nested class is one that has the static
modifier applied. Because it is static, it must
access the members of its enclosing class through
an object.
• That is, it cannot refer to members of its
enclosing class directly. Because of this
restriction, static nested classes are seldom used.
Two types of nested classes
• The most important type of nested class is the
inner class. An inner class is a non-static
nested class.
• It has access to all of the variables and
methods of its outer class and may refer to
them directly in the same way that other non-
static members of the outer class do.
There are two types of nested classes:

• There are two types of nested classes:


• static and non-static
Syntax of Inner class
class Java_Outer_class{  
 //code  
 class Java_Inner_class{  
  //code  
 }  
}  
Advantage of java inner classes
• 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.
Nested Classes or Inner Class
// Demonstrate an inner class.
class Outer {
int outer_x = 100;
void test() { Inner inner = new Inner();
inner.display(); }
// this is an inner class
class Inner {
void display() {System.out.println("display: outer_x = " + outer_x);}}}
class InnerClassDemo {
public static void main(String args[]) {
Outer outer = new Outer();
outer.test(); } }
Accessing the Private Members
• Outer_Demo outer = new Outer_Demo();
• Outer_Demo.Inner_Demo inner = outer.new
Inner_Demo();
• To instantiate the inner class, initially you have to
instantiate the outer class. Thereafter, using the object
of the outer class, following is the way in which you
can instantiate the inner class.
• Step 1 : Create Outer Object
• Step 2 Outerclas.innerclass object_name=
outerobject.new Innerclass();
Practice Task
• Suppose, a Outer class is having private members to
access them.
• Write an inner class in it, return the private members from
a method within the inner class, say, getValue, and finally
from another class from which you want to access the
private members call the getValue method of the inner
class.
• To instantiate the inner class, initially you have to
instantiate the outer class. Thereafter, using the object of
the outer class, following is the way in which you can
instantiate the inner class.
Outer_Demo.java
class Outer_Demo { // private variable of the outer class
private int num = 175; // inner class
public class Inner_Demo {
public int getNum() {
System.out.println("This is the getnum method of the inner class");
return num;
} }}
public class My_class2 {
public static void main(String args[]) {
// Instantiating the outer class
Outer_Demo outer = new Outer_Demo();
// Instantiating the inner class
Outer_Demo.Inner_Demo inner = outer.new Inner_Demo();
System.out.println(inner.getNum()); }}

You might also like