Nested Class:
Java allows creating a class inside another class. These kinds of classes are called nested Class.
The simplest example for a nested class is:
Why Use Nested Class:
There are several compelling reasons for using nested classes, among them:
Nested classes can be divided into two categories: Static & non-Static. The classes that are static are called Static Nested Class & the classes that are not static are called Inner Class.
Inner Class:
We have already discussed about the Inner classes. Now, the inner classes can be defined into 3 categories:
1. Simple Inner Class (Non-static class)(Scope : Member i.e. can be accessed by creating objects )
2. Local Class(Scope: Local)
3. Anonymous Class(Scope : Only at the point where it is defined)
Simple Inner Class: We have already discussed about the simple inner class. Once again the example for simple inner class:
Local Class:
The inner classes that has no access specifier mentioned in it.
Anonymous Class:
“Anonymous class”- the name describes the class itself .That means, the class which doesn’t have a name. More technically, an anonymous class declaration is automatically derived from a class instance creation expression by the java compiler.
Anonymous class is -
1. Never ABSTRACT
2. Always IMPLICITLY FINAL
Example:
Feel free to share!!
"There is much pleasure to be gained from useless knowledge." - Bertrand Russell
Java allows creating a class inside another class. These kinds of classes are called nested Class.
The simplest example for a nested class is:
class OuterClass {
class ClassInside
{
//do something
}
}
Why Use Nested Class:
There are several compelling reasons for using nested classes, among them:
- It is a way of logically grouping classes that are only used in one place.
- It increases encapsulation.
- Nested classes can lead to more readable and maintainable code.
Nested classes can be divided into two categories: Static & non-Static. The classes that are static are called Static Nested Class & the classes that are not static are called Inner Class.
public class
OuterClass {
String name = "Mc";
public String callName()
{
return name;
}
// Static nested class
public static class NestedClass
{
String surname = "Donalds";
public String callSurname()
{
return surname;
}
}
// Inner Class
public class InnerClass
{
String surname = "Donalds";
public String callSurname()
{
return surname;
}
}
}
//caller class
public class Caller {
public static void main(String args[]){
OuterClass oc = new OuterClass();
System.out.print(oc.callName()+" ");
// calling static class directly
OuterClass.NestedClass ic = new OuterClass.NestedClass();
System.out.println(ic.callSurname());
System.out.println("=================================================");
System.out.print(oc.callName()+" ");
// calling using object
OuterClass.InnerClass obj = oc.new InnerClass();
System.out.println(obj.callSurname());
}
}
Inner Class:
We have already discussed about the Inner classes. Now, the inner classes can be defined into 3 categories:
1. Simple Inner Class (Non-static class)(Scope : Member i.e. can be accessed by creating objects )
2. Local Class(Scope: Local)
3. Anonymous Class(Scope : Only at the point where it is defined)
Simple Inner Class: We have already discussed about the simple inner class. Once again the example for simple inner class:
public class OuterClass {
// Simple Inner Class
public class InnerClass
{
String surname = "Donalds";
public String callSurname()
{
return surname;
}
}
}
Local Class:
The inner classes that has no access specifier mentioned in it.
public class OuterClass {
// Local Class
class InnerClass
{
String surname = "Donalds";
public String callSurname()
{
return surname;
}
}
}
Anonymous Class:
“Anonymous class”- the name describes the class itself .That means, the class which doesn’t have a name. More technically, an anonymous class declaration is automatically derived from a class instance creation expression by the java compiler.
Anonymous class is -
1. Never ABSTRACT
2. Always IMPLICITLY FINAL
Example:
//SuperClass
public class SuperClass {
SuperClass()
{
//do something
}
}
//called from this class
public class AnonymousCaller{
public static void main(String args[])
{
SuperClass sc =new SuperClass(){
public void method(){
System.out.println("TREE");
}
};
sc.method();
}
}
Feel free to share!!
"There is much pleasure to be gained from useless knowledge." - Bertrand Russell
No comments :
Post a Comment