Hello friends how are you, today in this blog i will teach you what is inner class, syntax of inner class , how inner class work and many programs using inner class in a very simple way.
Let's start
2.Just like nested-if and nested-loop one class inside another class is called nested class.
3.Inner class is also called Nesting of class.
Syntax
class OuterAddition { class InnerAddition { } }
Example
For better understanding of inner class see the below example.Here in the below example we are going to describe concept of inner class or nesting class using two class in which one class is inside another.
class OuterAddition { //creating outer class method public void outAdd() { int x,y=10,z=20; x=y+z; System.out.println("outAdd="+x); } //creating inner class class InnerAddition { //creating inner class method public void inAdd() { int x,y=50,z=20; x=y+z; System.out.println("inAdd="+x); } } } class Easy { public static void main(String[] args) { //Creating instance(object) of OuterAddition class OuterAddition obj=new OuterAddition(); //calling OuterAddition function obj.outAdd(); //Creating instance(object) of InnerAddition class OuterAddition.InnerAddition obj2=obj.new InnerAddition(); //calling InnerAddition function obj2.inAdd(); } } /* ### Output ### outAdd=30 inAdd=70 */
Just focus on creating the instance(object) of InnerAddition class you got something new. The object of InnerAddition is creating with the reference of OuterAddition because InnerAddition exists inside OuterAddition.
Request:-If you found this post helpful then let me know by your comment and share it with your friend.
If you want to ask a question or want to suggest then type your question or suggestion in comment box so that we could do something new for you all.
If you have not subscribed my website then please subscribe my website.
Try to learn something new and teach something new to other. Thanks.
0 Comments