What is inner-outer class?
When we define B class into A class, that time A is outer class and B is inner class.
class A // Outer class
class B // inner class
Inner class can’t access from main class. For access inner class we have to reach outer class methods then we can access inner class methods and variables from outer class method.
Let’s see example on inner-outer class program in java.
void setMethod1() // How to access this method?
System.out.println(“Welcome in Inner Class”);
System.out.println(“Outer Class”); // 1st
// We can create an object of inner class
inner1 obj = new inner1();
obj.setMethod1(); // call into inner class
public static void main(String args)
// create an object of inner class
//inner1 obj = new inner1(); // Can’t able to create inner class object
outer1 ob = new outer1();
ob.setMethodOut1(); // call to outer class method
Welcome in inner class
In this blog example, we can’t able to create an object of inner class, so we have to create outer class object then call to outer class method. After that we have to create object of inner class in outer class method.
So this is most secure class structure of java programs.
Video available on YouTube.
If you have query about this blog, you can directly post comment or subscribe on website.
Share and Enjoy
289total visits,6visits today