Inner Class in java
A class created within class and outside
method.
|
|
A class created for implementing interface or
extending class. Its name is decided by the java compiler.
|
|
A class created within method.
|
|
A static class created within class.
|
|
An interface created within class or
interface.
|
Syntax:-
class Java_Outer_class{
//code
class Java_Inner_class{
//code
}
}
A
non-static class that is created inside a class but outside a method is called
member inner class.
Example:- Member Inner Class
package govind;
public
class MemberOuterClass {
private
String name="Govind
Ballabh Khan";
class
MemberInnerClass
{
public void
display()
{
System.out.println("Welcome
:"+name);
}
}
public
static void
main(String[] args) {
MemberOuterClass moc=new MemberOuterClass();
MemberOuterClass.MemberInnerClass
mic=moc.new MemberInnerClass();
mic.display();
}
}
o/p:-------------
Welcome :Govind Ballabh Khan
Java Anonymous inner class
A class that have no name is known as anonymous inner class in
java. It should be used if you have to override method of class or interface.
Java Anonymous inner class can be created by two ways:
- Class
(may be abstract or concrete).
- Interface
package govind;
public abstract class Employee {
public abstract void printEmployeeName();
}
class AnonymousInner
{
public static void main(String[] args) {
Employee
e=new Employee() {
@Override
public void printEmployeeName() {
System.out.println("Govind
Ballabh Khan");
}
};
e.printEmployeeName();
}
}
o/p:----------
Govind
Ballabh Khan
package govind;
public
interface Student {
public
void printStudentName();
}
class
AnonymousInnerWithInterface
{
public
static void
main(String[] args) {
Student s=new
Student() {
@Override
public void
printStudentName() {
System.out.println("Govind
Ballabh Khan");
}
};
s.printStudentName();
}
}
o/p:---------------------------
Govind Ballabh Khan
Java Local inner class
A class
i.e. created inside a method is called local inner class in java. If you want
to invoke the methods of local inner class, you must instantiate this class
inside the method.
Example :-Local
Inner Class
package govind;
public
class LocalOuterClass {
private
String name="Govind
Ballabh Khan";
public
void display()
{
class LocalInnerClass
{
public void
printName()
{
System.out.println("Welcome:"+name);
}
}
LocalInnerClass lic=new
LocalInnerClass();
lic.printName();
}
public
static void
main(String[] args) {
LocalOuterClass loc=new
LocalOuterClass();
loc.display();
}
}
o/p:----------
Welcome:Govind Ballabh Khan
Rule :-
Local
variable cannot be private, public or protected.
Rules for local
inner class:-
Local
inner class cannot be invoked from outside the method.
Local
inner class cannot access non final local variable till jdk 1.7. since jdk 1.8
it is possible to access the non final local variable in local inner class.
package govind;
public
class LocalOuterClassDemo {
private
int x=100;
public
void display()
{
//
int x=200;//local variable must be final till jdk 1.7 only //line ------1
class LocalInnerClassDemo
{
public void
printValue()
{
System.out.println("X="+x);
}
}
LocalInnerClassDemo licd=new
LocalInnerClassDemo();
licd.printValue();
}
public
static void
main(String[] args) {
LocalOuterClassDemo locd=new
LocalOuterClassDemo();
locd.display();
}
}
o/p:-------------
if
line 1 is not commented
then
X=200
if
line 1 is commented
then
X=100
Java
static nested class
A static class i.e. created inside a class is called static nested
class in java. It cannot access non-static data members and methods. It can be
accessed by outer class name.
- It
can access static data members of outer class including private.
- Static nested class cannot access non-static (instance) data
member or method.
Java static nested class example with instance
method
package govind;
public
class StaticOuter {
static
String fname="Govind";//static attribute
String mname="Ballabh";//non
static attribute
private
static String lname="Khan";////static and private
attribute
public
void displayPageName()//non static method
{
System.out.println("Java
Technology");
}
static
class
StaticInner
{
public void
display()
{
System.out.println("First
Name="+fname);
//System.out.println("Middle Name="+mname);//compile
time error
System.out.println("Last
Name="+lname);
//displayPageName();//compile time error
}
}
public
static void
main(String[] args) {
StaticOuter.StaticInner si=new
StaticOuter.StaticInner();
si.display();
}
}
o/p:-----
First Name=Govind
Last Name=Khan
Java static nested class example with static
method
If
you have the static member inside static nested class, you don't need to create
instance of static nested class.
package govind;
public class StaticOuterDemo {
static String fname="Govind";//static
attribute
String mname="Ballabh";//non static attribute
private static String lname="Khan";////static
and private attribute
public void displayPageName()//non static
method
{
System.out.println("Java Technology");
}
static class StaticInner
{
public static void
display()
{
System.out.println("First Name="+fname);
//System.out.println("Middle Name="+mname);//compile
time error
System.out.println("Last Name="+lname);
//displayPageName();//compile time error
}
}
public static void
main(String[] args) {
StaticOuterDemo.StaticInner.display();
}
}
o/p:-----
First Name=Govind
Last Name=Khan
Java Nested Interface
An interface i.e. declared within another
interface or class is known as nested interface. The nested interfaces are used
to group related interfaces so that they can be easy to maintain. The nested
interface must be referred by the outer interface or class. It can't be
accessed directly.
There are given some points that should be remembered by the java
programmer.
- Nested
interface must be public or default if it is declared inside the interface
but it can have any access modifier except private if declared within the class
- Nested
interfaces are declared static implicitly.
Syntax :-
interface inside interface
interface interface_name{
...
interface nested_interface_name{
...
}
}
Syntax :- class inside
interface
class class_name{
...
interface nested_interface_name{
...
}
}
interface inside interface example
package
govind;
public interface
NameShowable {
public void
namePrint();
interface
AddressShowable
{
public void
addressPrint();
}
}
class
NestedInterface implements NameShowable.AddressShowable
{
@Override
public void
addressPrint() {
System.out.println("Hanumannagar
Bhour Madhubani Bihar 847231");
}
public static void
main(String[] args) {
NameShowable.AddressShowable
na=new NestedInterface();
na.addressPrint();
}
}
o/p:---------
Hanumannagar Bhour Madhubani Bihar 847231
class inside interface example
package govind;
public
class EmployeeDemo {
interface
PrintEmpName
{
public void
NameShowable();
}
}
class
NestedInterfaceDemo implements EmployeeDemo.PrintEmpName
{
@Override
public
void NameShowable() {
System.out.println("Govind
Ballabh Khan");
}
public
static void
main(String[] args) {
EmployeeDemo.PrintEmpName ep=new
NestedInterfaceDemo();
ep.NameShowable();
}
}
o/p:------------
Govind
Ballabh Khan
Can we define a class inside the interface?
Yes, If we define a class inside the interface,
java compiler creates a static nested class. Let's see how can we define a
class within the interface:
interface M{
class A{}
}
No comments:
Post a Comment