Nested classes, also known as inner classes, are classes defined within other classes. There are four types of nested classes in Java: member inner classes, anonymous inner classes, local inner classes, and static nested classes. Member inner classes are non-static classes defined within another class but outside any methods. Anonymous inner classes are inner classes without a name where only a single object is created. Local inner classes are defined within a method block. Static nested classes are static classes declared within another class and do not require an outer class instance to be created.
Download as PPTX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
14 views
Nested Class Java
Nested classes, also known as inner classes, are classes defined within other classes. There are four types of nested classes in Java: member inner classes, anonymous inner classes, local inner classes, and static nested classes. Member inner classes are non-static classes defined within another class but outside any methods. Anonymous inner classes are inner classes without a name where only a single object is created. Local inner classes are defined within a method block. Static nested classes are static classes declared within another class and do not require an outer class instance to be created.
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 12
Nested class
• Defining a class within another class, such classes
are known as nested classes. • The scope of a nested class is bounded by the scope of its enclosing class. • A nested class has access to the members, including private members, of the class in which it is nested. • A nested class is also a member of its enclosing class. • As a member of its enclosing class, a nested class can be declared private, public, protected, default. • To improve the readability,maintainabilty with less code Advantage of Java inner classes 1. Nested classes represent a particular type of relationship that is it can access all the members (data members and methods) of the 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. Need of Java Inner class • Sometimes users need to program a class in such a way so that no other class can access it. Therefore, it would be better if you include it within other classes. • If all the class objects are a part of the outer object then it is easier to nest that class inside the outer class. That way all the outer class can access all the objects of the inner class Nested classes are divided into two categories: 1.static nested class : Nested classes that are declared static are called static nested classes. 2.Non-static nested class: An inner class is declared as a non-static nested class. Syntax: class OuterClass { ... class NestedClass { ... } } Non-Static Nested Class (Inner Class) • A non-static nested class is a class within another class. It has access to members of the enclosing class (outer class). It is commonly known as inner class. • Since the inner class exists within the outer class, you must instantiate the outer class first, in order to instantiate the inner class. • Non-static nested class (inner class) • Member inner class • Anonymous inner class • Local inner class Member Inner class • A non-static class that is created inside a class but outside a method is called member inner class. It is also known as a regular inner class. It can be declared with access modifiers like public, default, private, and protected. Syntax: class Outer{ //code class Inner{ //code } } class Person{ String sname="abc"; String ename="xyz"; public static void main(String args[]){ class Student{ int sno=108; void display(){ System.out.println(sno+” Person p=new Person(); ”+sname); p.display(); } } Person.Student s=p.new Student(); class Employee{ int eno=34; s.display(); void display(){ System.out.println(eno+ "\ n" +ename); } Person.Employee e=p.new } Employee(); e.display(); void display(){ System.out.println("outer class"); } } Anonymous inner class • A class that has no name is known as an anonymous inner class • anonymous inner class is an inner class without a name and for which only a single object is created • It should be used if you have to override a method of class • It is only accessible in the block where it is defined. • We can use an abstract class to define the anonymous inner class. It has access to all the members of the parent class. • Basically, it merges the step of creating an object and defining the class. class Person{ void display(){ System.out.println("outer class"); } } class Student{ public static void main(String args[]){ Person p=new Person(); p.display(); //Anonymous inner class Person p1=new Person(){ void display(){ System.out.println("Anonymous class"); } }; p1.display(); Local inner class • A class i.e., created inside a method, is called local inner class. • Local Inner Classes are the inner classes that are defined inside a block. Generally, this block is a method body. • Local Inner classes are not a member of any enclosing classes. They belong to the block they are defined within, due to which local inner classes cannot have any access modifiers associated with them. • They can be marked as final or abstract. • If you want to invoke the methods of the local inner class, you must instantiate this class inside the method. class Outer{ void outerMethod(){ class Inner{ void innerMethod(){ System.out.println("Local Inner class"); } } Inner i=new Inner(); i.innerMethod(); System.out.println("outer method"); } public static void main(String args[]){ Outer o=new Outer(); o.outerMethod(); } Static Nested Class • A static class declared inside another class is known as static nested class. • Unlike inner class, a static nested class cannot access the member variables of the outer class • It is because the static nested class doesn't require you to create an instance of the outer class.
OuterClass.NestedClass obj = new OuterClass.NestedClass();
class Outer{ static class Inner{ void innerMethod(){ System.out.println("static nested class"); } } static void outerMethod(){ System.out.println("outer"); } public static void main(String args[]){ Inner i=new Inner(); i.innerMethod(); outerMethod(); }