In Java, an inner class is a class that is declared within another class or interface. To summarize, the same logically related classes as Java is purely object-oriented, bringing it closer to the real world.
It also has access to all the outer class’s members, including private data members and methods.
class Outer_Demo {
//statements...
class Inner_Demo {
//statements...
}
}
The following are some advantages associated with the inner classes:
class Outer {
// Simple nested inner class
class Inner {
// show() method of inner class
public void show()
{
// Print statement
for(int i=0;i<5;i++)
{
for(int j=0;j<i;j++)
{
System.out.print(j+"");
}
System.out.println();
}
System.out.println("In a nested class method");
}
}
}
// Class 2
// Main class
class Coderz {
// Main driver method
public static void main(String[] args)
{
Outer.Inner in = new Outer().new Inner();
// Calling show() method over above object created
in.show();
}
}
0
01
012
0123
In a nested class method
class Outer {
// Method inside outer class
void outerMethod()
{
// Print statement
System.out.println("inside outerMethod");
class Inner {
// Method defined inside inner class
void innerMethod()
{
System.out.println("inside innerMethod");
}
}
// Creating object of inner class
Inner y = new Inner();
// Calling over method defined inside it
y.innerMethod();
}
}
class Coderz {
// Main driver method
public static void main(String[] args)
{
Outer x = new Outer();
x.outerMethod();
}
}
inside outerMethod
inside innerMethod
import java.util.*;
class Outer {
private static void outerMethod()
{
// Print statement
System.out.println("1. inside outerMethod");
}
// Static inner class
static class Inner {
public static void display()
{
// Print statement
System.out.println("2. inside inner class Method");
// Calling method inside main() method
outerMethod();
}
}
}
// Main class
class Coderz {
// Main driver method
public static void main(String args[])
{
Outer.Inner obj = new Outer.Inner();
// Calling method via above instance created
obj.display();
}
}
2. inside inner class Method
1. inside outerMethod
import java.util.*;
class Demo {
void show()
{
// Print statement
System.out.println(
"1. show method of super class");
}
}
// Class 2
// Main class
class Flavor1Demo {
// An anonymous class with Demo as base class
static Demo d = new Demo() {
void show()
{
// Calling method show() via super keyword
// which refers to parent class
super.show();
// Print statement
System.out.println("2. Flavor1Demo class");
}
};
// Method 2
// Main driver method
public static void main(String[] args)
{
// Calling show() method inside main() method
d.show();
}
}
1. show method of super class
2. Flavor1Demo class
Note: also read about the Java connectivity to MongoDB database
If you like my post, please follow me to read my latest post on programming and technology.
https://www.instagram.com/coderz.py/
https://www.facebook.com/coderz.py
Staying up to the mark is what defines me. Hi all! I’m Rabecca Fatima a keen learner, great enthusiast, ready to take new challenges as stepping stones towards flying colors.
Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…
Given an integer A. Compute and return the square root of A. If A is…
Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…
A heap is a specialized tree-based data structure that satisfies the heap property. It is…
What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…