In: Computer Science
Give an example of an inner and outer class. (Java)
In Java when the code is too lengthy or complicated you create nested classes or basically group your classes which makes them easy to understand and maintain.
This nesting has mainly inner and outer classes. Inner class is basically the class which is the member of another class. And the class who has other class as member is the outer class. You can understand this with a example -
class Outerclas {
##class methods and functions.
Class Innerclas {
public void nest() {
System.out.println("its the inner class of nested class method"); } } }
class Main {
public static void main(String[] args) {
Outer.Inner inside = new Outer().new Inner();
inside.nest(); } }
The output of this code will be - its the inner class of nested class method.
From the code you can see that the outerclas is the outer class where as the innerclas is the inside class as it's a member of the outerclas.