Lecture 14 OOP
Lecture 14 OOP
Credit Hour: 3
Lec - 14
Course Teacher
Md Istakiak Adnan Palash
Lec - 14 1
Java Inner Classes
• Java inner class or nested class is a class which is declared inside
the class or interface.
Lec - 14 2
Syntax of Inner class
class Java_Outer_class{
//code
class Java_Inner_class{
//code
}
}
Lec - 14 3
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.
Lec - 14 4
Difference between nested class and
inner class in Java
• Inner class is a part of nested class. Non-static nested classes are
known as inner classes.
Types of Nested classes:
• Non-static nested class (inner class)
• Member inner class
• Anonymous inner class (Will learn later on)
• Local inner class
Lec - 14 5
Difference between nested class and inner
class in Java
Type Description
Lec - 14 6
Java Member inner class
A non-static class that is created inside a class but outside a method
is called member inner class.
Syntax:
class Outer{
//code
class Inner{
//code
}
}
Lec - 14 7
Java Member inner class
class TestMemberOuter1{
private int data=30;
class Inner{
void msg(){System.out.println("data is "+data);}
}
public static void main(String args[]){
TestMemberOuter1 obj=new TestMemberOuter1();
TestMemberOuter1.Inner in=obj.new Inner();
in.msg();
}
}
Output: data is 30
In this example, we are creating msg() method in member
inner class that is accessing the private data member of
outer class.
Lec - 14 8
Internal working of Java member inner class
• The java compiler creates two class files in case of inner class.
The class file name of inner class is "Outer$Inner".
Lec - 14 9
Java Local inner class
• A class i.e. created inside a method is called local inner class in
java. If you want to invoke the methods of local inner class, you
must instantiate this class inside the method.
Lec - 14 10
Java Local inner class
public class localInner1{
private int data=30;//instance variable
void display(){
class Local{
void msg(){System.out.println(data);}
} Output:
Local l=new Local(); 30
l.msg();
}
public static void main(String args[]){
localInner1 obj=new localInner1();
obj.display();
}
}
Lec - 14 11
Java Local inner class
Rule: Local variable can't be private, public or protected.
2) Local inner class cannot access non-final local variable till JDK 1.7.
Since JDK 1.8, it is possible to access the non-final local variable in
local inner class.
Lec - 14 12
Example of local inner class with local variable
class localInner2{
private int data=30;//instance variable
void display(){
int value=50;//local variable must be final till jdk 1.7 only
class Local{
void msg(){System.out.println(value);}
}
Output:
Local l=new Local();
50
l.msg();
}
public static void main(String args[]){
localInner2 obj=new localInner2();
obj.display();
}
}
Lec - 14 13
Java static nested class
• A static class i.e. created inside a class is called static nested class
in java. It cannot access non-static data members and methods. It
can be accessed by outer class name.
- It can access static data members of outer class including
private.
- Static nested class cannot access non-static (instance) data
member or method.
Lec - 14 14
Java static nested class example with
instance method
class TestOuter1{
static int data=30; In this example, you need to
static class Inner{ create the instance of static
void msg(){System.out.println("data is "+d
nested class because it has
ata);}
instance method msg().
}
But you don't need to create the
public static void main(String args[]){
TestOuter1.Inner obj=new TestOuter1.Inne
object of Outer class because
r(); nested class is static and static
obj.msg(); properties, methods or classes
} can be accessed without object.
}
Output:
data is 30
Lec - 14 15
Java static nested class example with static method
class TestOuter2{
static int data=30;
static class Inner{
static void msg(){System.out.println("data is "+data);}
}
public static void main(String args[]){
TestOuter2.Inner.msg();//no need to create the instance of static
nested class
}
}
Output: data is 30
If you have the static member inside static nested class, you
don't need to create instance of static nested class.
Lec - 14 16
Thank You
Lec - 14 17